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