1 /* Processed by ecpg (regression mode) */
2 /* These include files are added by the preprocessor */
3 #include <ecpglib.h>
4 #include <ecpgerrno.h>
5 #include <sqlca.h>
6 /* End of automatic include section */
7 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
8
9 #line 1 "dt_test.pgc"
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <pgtypes_date.h>
14 #include <pgtypes_timestamp.h>
15 #include <pgtypes_interval.h>
16
17
18 #line 1 "regression.h"
19
20
21
22
23
24
25 #line 8 "dt_test.pgc"
26
27
28 int
main(void)29 main(void)
30 {
31 /* exec sql begin declare section */
32
33
34
35
36
37 #line 14 "dt_test.pgc"
38 date date1 ;
39
40 #line 15 "dt_test.pgc"
41 timestamp ts1 ;
42
43 #line 16 "dt_test.pgc"
44 interval * iv1 , iv2 ;
45
46 #line 17 "dt_test.pgc"
47 char * text ;
48 /* exec sql end declare section */
49 #line 18 "dt_test.pgc"
50
51 date date2;
52 int mdy[3] = { 4, 19, 1998 };
53 char *fmt, *out, *in;
54 char *d1 = "Mon Jan 17 1966";
55 char *t1 = "2000-7-12 17:34:29";
56 int i;
57
58 ECPGdebug(1, stderr);
59 /* exec sql whenever sqlerror do sqlprint ( ) ; */
60 #line 27 "dt_test.pgc"
61
62 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
63 #line 28 "dt_test.pgc"
64
65 if (sqlca.sqlcode < 0) sqlprint ( );}
66 #line 28 "dt_test.pgc"
67
68 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table date_test ( d date , ts timestamp )", ECPGt_EOIT, ECPGt_EORT);
69 #line 29 "dt_test.pgc"
70
71 if (sqlca.sqlcode < 0) sqlprint ( );}
72 #line 29 "dt_test.pgc"
73
74 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
75 #line 30 "dt_test.pgc"
76
77 if (sqlca.sqlcode < 0) sqlprint ( );}
78 #line 30 "dt_test.pgc"
79
80 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set intervalstyle to postgres_verbose", ECPGt_EOIT, ECPGt_EORT);
81 #line 31 "dt_test.pgc"
82
83 if (sqlca.sqlcode < 0) sqlprint ( );}
84 #line 31 "dt_test.pgc"
85
86
87 date1 = PGTYPESdate_from_asc(d1, NULL);
88 ts1 = PGTYPEStimestamp_from_asc(t1, NULL);
89
90 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into date_test ( d , ts ) values ( $1 , $2 )",
91 ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
92 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
93 ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
94 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
95 #line 36 "dt_test.pgc"
96
97 if (sqlca.sqlcode < 0) sqlprint ( );}
98 #line 36 "dt_test.pgc"
99
100
101 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from date_test where d = $1 ",
102 ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
103 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
104 ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
105 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
106 ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
107 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
108 #line 38 "dt_test.pgc"
109
110 if (sqlca.sqlcode < 0) sqlprint ( );}
111 #line 38 "dt_test.pgc"
112
113
114 text = PGTYPESdate_to_asc(date1);
115 printf ("Date: %s\n", text);
116 PGTYPESchar_free(text);
117
118 text = PGTYPEStimestamp_to_asc(ts1);
119 printf ("timestamp: %s\n", text);
120 PGTYPESchar_free(text);
121
122 iv1 = PGTYPESinterval_from_asc("13556 days 12 hours 34 minutes 14 seconds ", NULL);
123 PGTYPESinterval_copy(iv1, &iv2);
124 text = PGTYPESinterval_to_asc(&iv2);
125 printf ("interval: %s\n", text);
126 PGTYPESinterval_free(iv1);
127 PGTYPESchar_free(text);
128
129 PGTYPESdate_mdyjul(mdy, &date2);
130 printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
131 /* reset */
132 mdy[0] = mdy[1] = mdy[2] = 0;
133
134 printf("date seems to get encoded to julian %ld\n", date2);
135
136 PGTYPESdate_julmdy(date2, mdy);
137 printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
138
139 ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
140 text = PGTYPEStimestamp_to_asc(ts1);
141 fmt = "(ddd), mmm. dd, yyyy, repeat: (ddd), mmm. dd, yyyy. end";
142 out = (char*) malloc(strlen(fmt) + 1);
143 date1 = PGTYPESdate_from_timestamp(ts1);
144 PGTYPESdate_fmt_asc(date1, fmt, out);
145 printf("date_day of %s is %d\n", text, PGTYPESdate_dayofweek(date1));
146 printf("Above date in format \"%s\" is \"%s\"\n", fmt, out);
147 PGTYPESchar_free(text);
148 free(out);
149
150 out = (char*) malloc(48);
151 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 47, "Which is day number %j in %Y.");
152 printf("%s\n", out);
153 free(out);
154
155
156 /* rdate_defmt_asc() */
157
158 date1 = 0; text = "";
159 fmt = "yy/mm/dd";
160 in = "In the year 1995, the month of December, it is the 25th day";
161 /* 0123456789012345678901234567890123456789012345678901234567890
162 * 0 1 2 3 4 5 6
163 */
164 PGTYPESdate_defmt_asc(&date1, fmt, in);
165 text = PGTYPESdate_to_asc(date1);
166 printf("date_defmt_asc1: %s\n", text);
167 PGTYPESchar_free(text);
168
169 date1 = 0; text = "";
170 fmt = "mmmm. dd. yyyy";
171 in = "12/25/95";
172 PGTYPESdate_defmt_asc(&date1, fmt, in);
173 text = PGTYPESdate_to_asc(date1);
174 printf("date_defmt_asc2: %s\n", text);
175 PGTYPESchar_free(text);
176
177 date1 = 0; text = "";
178 fmt = "yy/mm/dd";
179 in = "95/12/25";
180 PGTYPESdate_defmt_asc(&date1, fmt, in);
181 text = PGTYPESdate_to_asc(date1);
182 printf("date_defmt_asc3: %s\n", text);
183 PGTYPESchar_free(text);
184
185 date1 = 0; text = "";
186 fmt = "yy/mm/dd";
187 in = "1995, December 25th";
188 PGTYPESdate_defmt_asc(&date1, fmt, in);
189 text = PGTYPESdate_to_asc(date1);
190 printf("date_defmt_asc4: %s\n", text);
191 PGTYPESchar_free(text);
192
193 date1 = 0; text = "";
194 fmt = "dd-mm-yy";
195 in = "This is 25th day of December, 1995";
196 PGTYPESdate_defmt_asc(&date1, fmt, in);
197 text = PGTYPESdate_to_asc(date1);
198 printf("date_defmt_asc5: %s\n", text);
199 PGTYPESchar_free(text);
200
201 date1 = 0; text = "";
202 fmt = "mmddyy";
203 in = "Dec. 25th, 1995";
204 PGTYPESdate_defmt_asc(&date1, fmt, in);
205 text = PGTYPESdate_to_asc(date1);
206 printf("date_defmt_asc6: %s\n", text);
207 PGTYPESchar_free(text);
208
209 date1 = 0; text = "";
210 fmt = "mmm. dd. yyyy";
211 in = "dec 25th 1995";
212 PGTYPESdate_defmt_asc(&date1, fmt, in);
213 text = PGTYPESdate_to_asc(date1);
214 printf("date_defmt_asc7: %s\n", text);
215 PGTYPESchar_free(text);
216
217 date1 = 0; text = "";
218 fmt = "mmm. dd. yyyy";
219 in = "DEC-25-1995";
220 PGTYPESdate_defmt_asc(&date1, fmt, in);
221 text = PGTYPESdate_to_asc(date1);
222 printf("date_defmt_asc8: %s\n", text);
223 PGTYPESchar_free(text);
224
225 date1 = 0; text = "";
226 fmt = "mm yy dd.";
227 in = "12199525";
228 PGTYPESdate_defmt_asc(&date1, fmt, in);
229 text = PGTYPESdate_to_asc(date1);
230 printf("date_defmt_asc9: %s\n", text);
231 PGTYPESchar_free(text);
232
233 date1 = 0; text = "";
234 fmt = "yyyy fierj mm dd.";
235 in = "19951225";
236 PGTYPESdate_defmt_asc(&date1, fmt, in);
237 text = PGTYPESdate_to_asc(date1);
238 printf("date_defmt_asc10: %s\n", text);
239 PGTYPESchar_free(text);
240
241 date1 = 0; text = "";
242 fmt = "mm/dd/yy";
243 in = "122595";
244 PGTYPESdate_defmt_asc(&date1, fmt, in);
245 text = PGTYPESdate_to_asc(date1);
246 printf("date_defmt_asc12: %s\n", text);
247 PGTYPESchar_free(text);
248
249 PGTYPEStimestamp_current(&ts1);
250 text = PGTYPEStimestamp_to_asc(ts1);
251 /* can't output this in regression mode */
252 /* printf("timestamp_current: Now: %s\n", text); */
253 PGTYPESchar_free(text);
254
255 ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL);
256 text = PGTYPEStimestamp_to_asc(ts1);
257 printf("timestamp_to_asc1: %s\n", text);
258 PGTYPESchar_free(text);
259
260 ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL);
261 text = PGTYPEStimestamp_to_asc(ts1);
262 printf("timestamp_to_asc2: %s\n", text);
263 PGTYPESchar_free(text);
264
265 ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL);
266 text = PGTYPEStimestamp_to_asc(ts1);
267 printf("timestamp_to_asc3: %s\n", text);
268 PGTYPESchar_free(text);
269
270 /* abc-03:10:35-def-02/11/94-gh */
271 /* 12345678901234567890123456789 */
272
273 out = (char*) malloc(32);
274 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%");
275 printf("timestamp_fmt_asc: %d: %s\n", i, out);
276 free(out);
277
278 fmt = "This is a %m/%d/%y %H-%Ml%Stest";
279 in = "This is a 4/12/80 3-39l12test";
280 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
281 text = PGTYPEStimestamp_to_asc(ts1);
282 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
283 PGTYPESchar_free(text);
284
285 fmt = "%a %b %d %H:%M:%S %z %Y";
286 in = "Tue Jul 22 17:28:44 +0200 2003";
287 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
288 text = PGTYPEStimestamp_to_asc(ts1);
289 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
290 PGTYPESchar_free(text);
291
292 fmt = "%a %b %d %H:%M:%S %z %Y";
293 in = "Tue Feb 29 17:28:44 +0200 2000";
294 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
295 text = PGTYPEStimestamp_to_asc(ts1);
296 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
297 PGTYPESchar_free(text);
298
299 fmt = "%a %b %d %H:%M:%S %z %Y";
300 in = "Tue Feb 29 17:28:44 +0200 1900";
301 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
302 text = PGTYPEStimestamp_to_asc(ts1);
303 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
304 PGTYPESchar_free(text);
305
306 fmt = "%a %b %d %H:%M:%S %z %Y";
307 in = "Tue Feb 29 17:28:44 +0200 1996";
308 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
309 text = PGTYPEStimestamp_to_asc(ts1);
310 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
311 PGTYPESchar_free(text);
312
313 fmt = "%b %d %H:%M:%S %z %Y";
314 in = " Jul 31 17:28:44 +0200 1996";
315 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
316 text = PGTYPEStimestamp_to_asc(ts1);
317 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
318 PGTYPESchar_free(text);
319
320 fmt = "%b %d %H:%M:%S %z %Y";
321 in = " Jul 32 17:28:44 +0200 1996";
322 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
323 text = PGTYPEStimestamp_to_asc(ts1);
324 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
325 PGTYPESchar_free(text);
326
327 fmt = "%a %b %d %H:%M:%S %z %Y";
328 in = "Tue Feb 29 17:28:44 +0200 1997";
329 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
330 text = PGTYPEStimestamp_to_asc(ts1);
331 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
332 PGTYPESchar_free(text);
333
334 fmt = "%";
335 in = "Tue Jul 22 17:28:44 +0200 2003";
336 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
337 text = PGTYPEStimestamp_to_asc(ts1);
338 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
339 PGTYPESchar_free(text);
340
341 fmt = "a %";
342 in = "Tue Jul 22 17:28:44 +0200 2003";
343 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
344 text = PGTYPEStimestamp_to_asc(ts1);
345 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
346 PGTYPESchar_free(text);
347
348 fmt = "%b, %d %H_%M`%S %z %Y";
349 in = " Jul, 22 17_28 `44 +0200 2003 ";
350 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
351 text = PGTYPEStimestamp_to_asc(ts1);
352 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
353 PGTYPESchar_free(text);
354
355 fmt = "%a %b %%%d %H:%M:%S %Z %Y";
356 in = "Tue Jul %22 17:28:44 CEST 2003";
357 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
358 text = PGTYPEStimestamp_to_asc(ts1);
359 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
360 PGTYPESchar_free(text);
361
362 fmt = "%a %b %%%d %H:%M:%S %Z %Y";
363 in = "Tue Jul %22 17:28:44 CEST 2003";
364 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
365 text = PGTYPEStimestamp_to_asc(ts1);
366 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
367 PGTYPESchar_free(text);
368
369 fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y";
370 in = "abc\n 19 October %22 17:28:44 CEST 2003";
371 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
372 text = PGTYPEStimestamp_to_asc(ts1);
373 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
374 PGTYPESchar_free(text);
375
376 fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y";
377 in = "abc\n 18 October %34 17:28:44 CEST 80";
378 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
379 text = PGTYPEStimestamp_to_asc(ts1);
380 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
381 PGTYPESchar_free(text);
382
383 fmt = "";
384 in = "abc\n 18 October %34 17:28:44 CEST 80";
385 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
386 text = PGTYPEStimestamp_to_asc(ts1);
387 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
388 PGTYPESchar_free(text);
389
390 fmt = NULL;
391 in = "1980-04-12 3:49:44 ";
392 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
393 text = PGTYPEStimestamp_to_asc(ts1);
394 printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i);
395 PGTYPESchar_free(text);
396
397 fmt = "%B %d, %Y. Time: %I:%M%p";
398 in = "July 14, 1988. Time: 9:15am";
399 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
400 text = PGTYPEStimestamp_to_asc(ts1);
401 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
402 PGTYPESchar_free(text);
403
404 in = "September 6 at 01:30 pm in the year 1983";
405 fmt = "%B %d at %I:%M %p in the year %Y";
406 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
407 text = PGTYPEStimestamp_to_asc(ts1);
408 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
409 PGTYPESchar_free(text);
410
411 in = " 1976, July 14. Time: 9:15am";
412 fmt = "%Y, %B %d. Time: %I:%M %p";
413 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
414 text = PGTYPEStimestamp_to_asc(ts1);
415 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
416 PGTYPESchar_free(text);
417
418 in = " 1976, July 14. Time: 9:15 am";
419 fmt = "%Y, %B %d. Time: %I:%M%p";
420 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
421 text = PGTYPEStimestamp_to_asc(ts1);
422 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
423 PGTYPESchar_free(text);
424
425 in = " 1976, P.M. July 14. Time: 9:15";
426 fmt = "%Y, %P %B %d. Time: %I:%M";
427 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
428 text = PGTYPEStimestamp_to_asc(ts1);
429 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
430 PGTYPESchar_free(text);
431
432 in = "1234567890";
433 fmt = "%s";
434 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
435 text = PGTYPEStimestamp_to_asc(ts1);
436 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
437 PGTYPESchar_free(text);
438
439 out = (char*) malloc(64);
440 fmt = "%a %b %d %H:%M:%S %Y";
441 in = "Mon Dec 30 17:28:44 2019";
442 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
443 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
444 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
445 free(out);
446
447 out = (char*) malloc(64);
448 fmt = "%a %b %d %H:%M:%S %Y";
449 in = "Mon December 30 17:28:44 2019";
450 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
451 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
452 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
453 free(out);
454
455 { ECPGtrans(__LINE__, NULL, "rollback");
456 #line 381 "dt_test.pgc"
457
458 if (sqlca.sqlcode < 0) sqlprint ( );}
459 #line 381 "dt_test.pgc"
460
461 { ECPGdisconnect(__LINE__, "CURRENT");
462 #line 382 "dt_test.pgc"
463
464 if (sqlca.sqlcode < 0) sqlprint ( );}
465 #line 382 "dt_test.pgc"
466
467
468 return (0);
469 }
470