Line data Source code
1 : #include <stdio.h>
2 : #include <string.h>
3 : #include <stdlib.h>
4 : #include <limits.h>
5 : #include <pgtypes_date.h>
6 : #include <pgtypes_timestamp.h>
7 :
8 : exec sql include ../regression;
9 :
10 : char *dates[] = { "19990108foobar",
11 : "19990108 foobar",
12 : "1999-01-08 foobar",
13 : "January 8, 1999",
14 : "1999-01-08",
15 : "1/8/1999",
16 : "1/18/1999",
17 : "01/02/03",
18 : "1999-Jan-08",
19 : "Jan-08-1999",
20 : "08-Jan-1999",
21 : "99-Jan-08",
22 : "08-Jan-99",
23 : "08-Jan-06",
24 : "Jan-08-99",
25 : "19990108",
26 : "990108",
27 : "1999.008",
28 : "J2451187",
29 : "January 8, 99 BC",
30 : /*
31 : * Maximize space usage in ParseDateTime() with 25
32 : * (MAXDATEFIELDS) fields and 128 (MAXDATELEN) total length.
33 : */
34 : "........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
35 : "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
36 : /* 26 fields */
37 : ".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
38 : "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
39 : NULL };
40 :
41 : /* do not conflict with libc "times" symbol */
42 : static char *times[] = { "0:04",
43 : "1:59 PDT",
44 : "13:24:40 -8:00",
45 : "13:24:40.495+3",
46 : "13:24:40.123456123+3",
47 : NULL };
48 :
49 : char *intervals[] = { "1 minute",
50 : "1 12:59:10",
51 : "2 day 12 hour 59 minute 10 second",
52 : "1 days 12 hrs 59 mins 10 secs",
53 : "1 days 1 hours 1 minutes 1 seconds",
54 : "1 year 59 mins",
55 : "1 year 59 mins foobar",
56 : NULL };
57 :
58 : int
59 2 : main(void)
60 : {
61 : exec sql begin declare section;
62 : date date1;
63 : timestamp ts1, ts2;
64 : char *text;
65 : interval *i1;
66 : date *dc;
67 : exec sql end declare section;
68 :
69 : int i, j;
70 : char *endptr;
71 :
72 2 : ECPGdebug(1, stderr);
73 :
74 2 : ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
75 2 : text = PGTYPEStimestamp_to_asc(ts1);
76 :
77 2 : printf("timestamp: %s\n", text);
78 2 : PGTYPESchar_free(text);
79 :
80 2 : date1 = PGTYPESdate_from_timestamp(ts1);
81 2 : dc = PGTYPESdate_new();
82 2 : *dc = date1;
83 2 : text = PGTYPESdate_to_asc(*dc);
84 2 : printf("Date of timestamp: %s\n", text);
85 2 : PGTYPESchar_free(text);
86 2 : PGTYPESdate_free(dc);
87 :
88 46 : for (i = 0; dates[i]; i++)
89 : {
90 44 : bool err = false;
91 44 : date1 = PGTYPESdate_from_asc(dates[i], &endptr);
92 44 : if (date1 == INT_MIN) {
93 10 : err = true;
94 : }
95 44 : text = PGTYPESdate_to_asc(date1);
96 88 : printf("Date[%d]: %s (%c - %c)\n",
97 : i, err ? "-" : text,
98 44 : endptr ? 'N' : 'Y',
99 : err ? 'T' : 'F');
100 44 : PGTYPESchar_free(text);
101 44 : if (!err)
102 : {
103 204 : for (j = 0; times[j]; j++)
104 : {
105 170 : int length = strlen(dates[i])
106 : + 1
107 170 : + strlen(times[j])
108 170 : + 1;
109 170 : char* t = malloc(length);
110 170 : sprintf(t, "%s %s", dates[i], times[j]);
111 170 : ts1 = PGTYPEStimestamp_from_asc(t, NULL);
112 170 : text = PGTYPEStimestamp_to_asc(ts1);
113 170 : printf("TS[%d,%d]: %s\n",
114 170 : i, j, errno ? "-" : text);
115 170 : PGTYPESchar_free(text);
116 170 : free(t);
117 : }
118 : }
119 : }
120 :
121 2 : ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
122 :
123 16 : for (i = 0; intervals[i]; i++)
124 : {
125 : interval *ic;
126 14 : i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
127 14 : if (*endptr)
128 0 : printf("endptr set to %s\n", endptr);
129 14 : if (!i1)
130 : {
131 2 : printf("Error parsing interval %d\n", i);
132 2 : continue;
133 : }
134 12 : j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
135 12 : if (j < 0)
136 0 : continue;
137 12 : text = PGTYPESinterval_to_asc(i1);
138 12 : printf("interval[%d]: %s\n", i, text ? text : "-");
139 12 : PGTYPESchar_free(text);
140 :
141 12 : ic = PGTYPESinterval_new();
142 12 : PGTYPESinterval_copy(i1, ic);
143 12 : text = PGTYPESinterval_to_asc(i1);
144 12 : printf("interval_copy[%d]: %s\n", i, text ? text : "-");
145 12 : PGTYPESchar_free(text);
146 12 : PGTYPESinterval_free(ic);
147 12 : PGTYPESinterval_free(i1);
148 : }
149 :
150 2 : return 0;
151 : }
|