PostgreSQL Source Code git master
pgtypeslib-dt_test2.c
Go to the documentation of this file.
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
28char *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 */
60static 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
67char *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
76int
77main(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);
111
112 printf("timestamp: %s\n", text);
114
115 date1 = PGTYPESdate_from_timestamp(ts1);
116 dc = PGTYPESdate_new();
117 *dc = date1;
119 printf("Date of timestamp: %s\n", text);
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');
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);
148 printf("TS[%d,%d]: %s\n",
149 i, j, errno ? "-" : text);
151 free(t);
152 }
153 }
154 }
155
156 ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
157
158 for (i = 0; intervals[i]; i++)
159 {
160 interval *ic;
161 i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
162 if (*endptr)
163 printf("endptr set to %s\n", endptr);
164 if (!i1)
165 {
166 printf("Error parsing interval %d\n", i);
167 continue;
168 }
169 j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
170 if (j < 0)
171 continue;
173 printf("interval[%d]: %s\n", i, text ? text : "-");
175
176 ic = PGTYPESinterval_new();
177 PGTYPESinterval_copy(i1, ic);
179 printf("interval_copy[%d]: %s\n", i, text ? text : "-");
183 }
184
185 return 0;
186}
struct varlena text
Definition: c.h:657
void err(int eval, const char *fmt,...)
Definition: err.c:43
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
int j
Definition: isn.c:73
int i
Definition: isn.c:72
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
date PGTYPESdate_from_timestamp(timestamp dt)
Definition: datetime.c:31
char * PGTYPESdate_to_asc(date dDate)
Definition: datetime.c:101
long date
Definition: pgtypes_date.h:9
date PGTYPESdate_from_asc(char *str, char **endptr)
Definition: datetime.c:47
void PGTYPESdate_free(date *d)
Definition: datetime.c:25
date * PGTYPESdate_new(void)
Definition: datetime.c:15
interval * PGTYPESinterval_new(void)
Definition: interval.c:987
void PGTYPESinterval_free(interval *intvl)
Definition: interval.c:997
int PGTYPESinterval_copy(interval *intvlsrc, interval *intvldest)
Definition: interval.c:1082
interval * PGTYPESinterval_from_asc(char *str, char **endptr)
Definition: interval.c:1003
char * PGTYPESinterval_to_asc(interval *span)
Definition: interval.c:1062
timestamp PGTYPEStimestamp_from_asc(char *str, char **endptr)
Definition: timestamp.c:202
int64 timestamp
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:858
char * PGTYPEStimestamp_to_asc(timestamp tstamp)
Definition: timestamp.c:268
char * dates[]
int main(void)
static char * times[]
#define ECPGdebug(X, Y)
char * intervals[]
#define sprintf
Definition: port.h:241
#define printf(...)
Definition: port.h:245
Definition: c.h:644