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