10
10
#include <stdio.h>
11
11
#include <string.h>
12
12
#include <stdlib.h>
13
+ #include <limits.h>
13
14
#include <pgtypes_date.h>
14
15
#include <pgtypes_timestamp.h>
15
16
21
22
22
23
23
24
24
- #line 7 "dt_test2.pgc"
25
+ #line 8 "dt_test2.pgc"
25
26
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
+ NULL };
49
+
50
+ char * times [] = { "0:04" ,
51
+ "1:59 PDT" ,
52
+ "13:24:40 -8:00" ,
53
+ "13:24:40.495+3" ,
54
+ NULL };
55
+
56
+ char * intervals [] = { "1 minute" ,
57
+ "1 12:59:10" ,
58
+ "2 day 12 hour 59 minute 10 second" ,
59
+ "1 days 12 hrs 59 mins 10 secs" ,
60
+ "1 days 1 hours 1 minutes 1 seconds" ,
61
+ "1 year 59 mins" ,
62
+ "1 year 59 mins foobar" ,
63
+ NULL };
64
+
27
65
int
28
66
main (void )
29
67
{
30
68
/* exec sql begin declare section */
31
69
70
+
32
71
33
72
34
73
35
- #line 13 "dt_test2.pgc"
74
+ #line 51 "dt_test2.pgc"
36
75
date date1 ;
37
76
38
- #line 14 "dt_test2.pgc"
39
- timestamp ts1 ;
77
+ #line 52 "dt_test2.pgc"
78
+ timestamp ts1 , ts2 ;
40
79
41
- #line 15 "dt_test2.pgc"
80
+ #line 53 "dt_test2.pgc"
42
81
char * text ;
82
+
83
+ #line 54 "dt_test2.pgc"
84
+ interval * i1 ;
43
85
/* exec sql end declare section */
44
- #line 16 "dt_test2.pgc"
86
+ #line 55 "dt_test2.pgc"
87
+
45
88
89
+ int i , j ;
90
+ char * endptr ;
46
91
47
92
ECPGdebug (1 , stderr );
48
93
@@ -57,6 +102,58 @@ main(void)
57
102
printf ("Date of timestamp: %s\n" , text );
58
103
free (text );
59
104
105
+ for (i = 0 ; dates [i ]; i ++ )
106
+ {
107
+ bool err = false;
108
+ date1 = PGTYPESdate_from_asc (dates [i ], & endptr );
109
+ if (date1 == INT_MIN ) {
110
+ err = true;
111
+ }
112
+ text = PGTYPESdate_to_asc (date1 );
113
+ printf ("Date[%d]: %s (%c - %c)\n" ,
114
+ i , err ? "-" : text ,
115
+ endptr ? 'N' : 'Y' ,
116
+ err ? 'T' : 'F' );
117
+ free (text );
118
+ if (!err )
119
+ {
120
+ for (j = 0 ; times [j ]; j ++ )
121
+ {
122
+ int length = strlen (dates [i ])
123
+ + 1
124
+ + strlen (times [j ])
125
+ + 1 ;
126
+ char * t = malloc (length );
127
+ sprintf (t , "%s %s" , dates [i ], times [j ]);
128
+ ts1 = PGTYPEStimestamp_from_asc (t , NULL );
129
+ text = PGTYPEStimestamp_to_asc (ts1 );
130
+ printf ("TS[%d,%d]: %s\n" ,
131
+ i , j , errno ? "-" : text );
132
+ free (text );
133
+ }
134
+ }
135
+ }
136
+
137
+ ts1 = PGTYPEStimestamp_from_asc ("2004-04-04 23:23:23" , NULL );
138
+
139
+ for (i = 0 ; intervals [i ]; i ++ )
140
+ {
141
+ i1 = PGTYPESinterval_from_asc (intervals [i ], & endptr );
142
+ if (* endptr )
143
+ printf ("endptr set to %s\n" , endptr );
144
+ if (!i1 )
145
+ {
146
+ printf ("Error parsing interval %d\n" , i );
147
+ continue ;
148
+ }
149
+ j = PGTYPEStimestamp_add_interval (& ts1 , i1 , & ts2 );
150
+ if (j < 0 )
151
+ continue ;
152
+ text = PGTYPESinterval_to_asc (i1 );
153
+ printf ("interval[%d]: %s\n" , i , text ? text : "-" );
154
+ free (text );
155
+ }
156
+
60
157
return (0 );
61
158
}
62
159
0 commit comments