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