PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 
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
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  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  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  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]);
147  text = PGTYPEStimestamp_to_asc(ts1);
148  printf("TS[%d,%d]: %s\n",
149  i, j, errno ? "-" : text);
150  free(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;
172  text = PGTYPESinterval_to_asc(i1);
173  printf("interval[%d]: %s\n", i, text ? text : "-");
174  free(text);
175 
176  ic = PGTYPESinterval_new();
177  PGTYPESinterval_copy(i1, ic);
178  text = PGTYPESinterval_to_asc(i1);
179  printf("interval_copy[%d]: %s\n", i, text ? text : "-");
180  free(text);
183  }
184 
185  return (0);
186 }
char * intervals[]
date PGTYPESdate_from_asc(char *, char **)
Definition: datetime.c:48
int length(const List *list)
Definition: list.c:1271
int PGTYPESinterval_copy(interval *, interval *)
Definition: interval.c:1104
int64 timestamp
long date
Definition: pgtypes_date.h:8
char * PGTYPESdate_to_asc(date)
Definition: datetime.c:102
#define malloc(a)
Definition: header.h:45
void PGTYPESinterval_free(interval *)
Definition: interval.c:1015
#define ECPGdebug(X, Y)
interval * PGTYPESinterval_from_asc(char *, char **)
Definition: interval.c:1021
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:870
static char * times[]
struct varlena text
Definition: c.h:449
void PGTYPESdate_free(date *)
Definition: datetime.c:26
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
char * PGTYPEStimestamp_to_asc(timestamp)
Definition: timestamp.c:279
date * PGTYPESdate_new(void)
Definition: datetime.c:16
char * PGTYPESinterval_to_asc(interval *)
Definition: interval.c:1080
int i
interval * PGTYPESinterval_new(void)
Definition: interval.c:1005
int main(void)
date PGTYPESdate_from_timestamp(timestamp)
Definition: datetime.c:32
timestamp PGTYPEStimestamp_from_asc(char *, char **)
Definition: timestamp.c:209
char * dates[]