PostgreSQL Source Code  git master
pgtypeslib-dt_test.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_test.pgc"
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <pgtypes_date.h>
14 #include <pgtypes_timestamp.h>
15 #include <pgtypes_interval.h>
16 
17 
18 #line 1 "regression.h"
19 
20 
21 
22 
23 
24 
25 #line 8 "dt_test.pgc"
26 
27 
28 int
29 main(void)
30 {
31  /* exec sql begin declare section */
32 
33 
34 
35 
36 
37 #line 14 "dt_test.pgc"
38  date date1 ;
39 
40 #line 15 "dt_test.pgc"
41  timestamp ts1 ;
42 
43 #line 16 "dt_test.pgc"
44  interval * iv1 , iv2 ;
45 
46 #line 17 "dt_test.pgc"
47  char * text ;
48 /* exec sql end declare section */
49 #line 18 "dt_test.pgc"
50 
51  date date2;
52  int mdy[3] = { 4, 19, 1998 };
53  char *fmt, *out, *in;
54  char *d1 = "Mon Jan 17 1966";
55  char *t1 = "2000-7-12 17:34:29";
56  int i;
57 
58  ECPGdebug(1, stderr);
59  /* exec sql whenever sqlerror do sqlprint ( ) ; */
60 #line 27 "dt_test.pgc"
61 
62  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
63 #line 28 "dt_test.pgc"
64 
65 if (sqlca.sqlcode < 0) sqlprint ( );}
66 #line 28 "dt_test.pgc"
67 
68  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table date_test ( d date , ts timestamp )", ECPGt_EOIT, ECPGt_EORT);
69 #line 29 "dt_test.pgc"
70 
71 if (sqlca.sqlcode < 0) sqlprint ( );}
72 #line 29 "dt_test.pgc"
73 
74  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
75 #line 30 "dt_test.pgc"
76 
77 if (sqlca.sqlcode < 0) sqlprint ( );}
78 #line 30 "dt_test.pgc"
79 
80  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set intervalstyle to postgres_verbose", ECPGt_EOIT, ECPGt_EORT);
81 #line 31 "dt_test.pgc"
82 
83 if (sqlca.sqlcode < 0) sqlprint ( );}
84 #line 31 "dt_test.pgc"
85 
86 
87  date1 = PGTYPESdate_from_asc(d1, NULL);
88  ts1 = PGTYPEStimestamp_from_asc(t1, NULL);
89 
90  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into date_test ( d , ts ) values ( $1 , $2 )",
91  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
92  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
93  ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
94  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
95 #line 36 "dt_test.pgc"
96 
97 if (sqlca.sqlcode < 0) sqlprint ( );}
98 #line 36 "dt_test.pgc"
99 
100 
101  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from date_test where d = $1 ",
102  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
103  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
104  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
105  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
106  ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
107  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
108 #line 38 "dt_test.pgc"
109 
110 if (sqlca.sqlcode < 0) sqlprint ( );}
111 #line 38 "dt_test.pgc"
112 
113 
114  text = PGTYPESdate_to_asc(date1);
115  printf ("Date: %s\n", text);
117 
119  printf ("timestamp: %s\n", text);
121 
122  iv1 = PGTYPESinterval_from_asc("13556 days 12 hours 34 minutes 14 seconds ", NULL);
123  PGTYPESinterval_copy(iv1, &iv2);
125  printf ("interval: %s\n", text);
128 
129  PGTYPESdate_mdyjul(mdy, &date2);
130  printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
131  /* reset */
132  mdy[0] = mdy[1] = mdy[2] = 0;
133 
134  printf("date seems to get encoded to julian %ld\n", date2);
135 
136  PGTYPESdate_julmdy(date2, mdy);
137  printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
138 
139  ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
141  fmt = "(ddd), mmm. dd, yyyy, repeat: (ddd), mmm. dd, yyyy. end";
142  out = (char*) malloc(strlen(fmt) + 1);
143  date1 = PGTYPESdate_from_timestamp(ts1);
144  PGTYPESdate_fmt_asc(date1, fmt, out);
145  printf("date_day of %s is %d\n", text, PGTYPESdate_dayofweek(date1));
146  printf("Above date in format \"%s\" is \"%s\"\n", fmt, out);
148  free(out);
149 
150  out = (char*) malloc(48);
151  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 47, "Which is day number %j in %Y.");
152  printf("%s\n", out);
153  free(out);
154 
155 
156  /* rdate_defmt_asc() */
157 
158  date1 = 0;
159  fmt = "yy/mm/dd";
160  in = "In the year 1995, the month of December, it is the 25th day";
161  /* 0123456789012345678901234567890123456789012345678901234567890
162  * 0 1 2 3 4 5 6
163  */
164  PGTYPESdate_defmt_asc(&date1, fmt, in);
165  text = PGTYPESdate_to_asc(date1);
166  printf("date_defmt_asc1: %s\n", text);
168 
169  date1 = 0;
170  fmt = "mmmm. dd. yyyy";
171  in = "12/25/95";
172  PGTYPESdate_defmt_asc(&date1, fmt, in);
173  text = PGTYPESdate_to_asc(date1);
174  printf("date_defmt_asc2: %s\n", text);
176 
177  date1 = 0;
178  fmt = "yy/mm/dd";
179  in = "95/12/25";
180  PGTYPESdate_defmt_asc(&date1, fmt, in);
181  text = PGTYPESdate_to_asc(date1);
182  printf("date_defmt_asc3: %s\n", text);
184 
185  date1 = 0;
186  fmt = "yy/mm/dd";
187  in = "1995, December 25th";
188  PGTYPESdate_defmt_asc(&date1, fmt, in);
189  text = PGTYPESdate_to_asc(date1);
190  printf("date_defmt_asc4: %s\n", text);
192 
193  date1 = 0;
194  fmt = "dd-mm-yy";
195  in = "This is 25th day of December, 1995";
196  PGTYPESdate_defmt_asc(&date1, fmt, in);
197  text = PGTYPESdate_to_asc(date1);
198  printf("date_defmt_asc5: %s\n", text);
200 
201  date1 = 0;
202  fmt = "mmddyy";
203  in = "Dec. 25th, 1995";
204  PGTYPESdate_defmt_asc(&date1, fmt, in);
205  text = PGTYPESdate_to_asc(date1);
206  printf("date_defmt_asc6: %s\n", text);
208 
209  date1 = 0;
210  fmt = "mmm. dd. yyyy";
211  in = "dec 25th 1995";
212  PGTYPESdate_defmt_asc(&date1, fmt, in);
213  text = PGTYPESdate_to_asc(date1);
214  printf("date_defmt_asc7: %s\n", text);
216 
217  date1 = 0;
218  fmt = "mmm. dd. yyyy";
219  in = "DEC-25-1995";
220  PGTYPESdate_defmt_asc(&date1, fmt, in);
221  text = PGTYPESdate_to_asc(date1);
222  printf("date_defmt_asc8: %s\n", text);
224 
225  date1 = 0;
226  fmt = "mm yy dd.";
227  in = "12199525";
228  PGTYPESdate_defmt_asc(&date1, fmt, in);
229  text = PGTYPESdate_to_asc(date1);
230  printf("date_defmt_asc9: %s\n", text);
232 
233  date1 = 0;
234  fmt = "yyyy fierj mm dd.";
235  in = "19951225";
236  PGTYPESdate_defmt_asc(&date1, fmt, in);
237  text = PGTYPESdate_to_asc(date1);
238  printf("date_defmt_asc10: %s\n", text);
240 
241  date1 = 0;
242  fmt = "mm/dd/yy";
243  in = "122595";
244  PGTYPESdate_defmt_asc(&date1, fmt, in);
245  text = PGTYPESdate_to_asc(date1);
246  printf("date_defmt_asc12: %s\n", text);
248 
251  /* can't output this in regression mode */
252  /* printf("timestamp_current: Now: %s\n", text); */
254 
255  ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL);
257  printf("timestamp_to_asc1: %s\n", text);
259 
260  ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL);
262  printf("timestamp_to_asc2: %s\n", text);
264 
265  ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL);
267  printf("timestamp_to_asc3: %s\n", text);
269 
270 /* abc-03:10:35-def-02/11/94-gh */
271 /* 12345678901234567890123456789 */
272 
273  out = (char*) malloc(32);
274  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%");
275  printf("timestamp_fmt_asc: %d: %s\n", i, out);
276  free(out);
277 
278  fmt = "This is a %m/%d/%y %H-%Ml%Stest";
279  in = "This is a 4/12/80 3-39l12test";
280  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
282  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
284 
285  fmt = "%a %b %d %H:%M:%S %z %Y";
286  in = "Tue Jul 22 17:28:44 +0200 2003";
287  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
289  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
291 
292  fmt = "%a %b %d %H:%M:%S %z %Y";
293  in = "Tue Feb 29 17:28:44 +0200 2000";
294  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
296  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
298 
299  fmt = "%a %b %d %H:%M:%S %z %Y";
300  in = "Tue Feb 29 17:28:44 +0200 1900";
301  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
303  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
305 
306  fmt = "%a %b %d %H:%M:%S %z %Y";
307  in = "Tue Feb 29 17:28:44 +0200 1996";
308  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
310  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
312 
313  fmt = "%b %d %H:%M:%S %z %Y";
314  in = " Jul 31 17:28:44 +0200 1996";
315  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
317  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
319 
320  fmt = "%b %d %H:%M:%S %z %Y";
321  in = " Jul 32 17:28:44 +0200 1996";
322  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
324  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
326 
327  fmt = "%a %b %d %H:%M:%S %z %Y";
328  in = "Tue Feb 29 17:28:44 +0200 1997";
329  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
331  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
333 
334  fmt = "%";
335  in = "Tue Jul 22 17:28:44 +0200 2003";
336  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
338  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
340 
341  fmt = "a %";
342  in = "Tue Jul 22 17:28:44 +0200 2003";
343  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
345  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
347 
348  fmt = "%b, %d %H_%M`%S %z %Y";
349  in = " Jul, 22 17_28 `44 +0200 2003 ";
350  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
352  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
354 
355  fmt = "%a %b %%%d %H:%M:%S %Z %Y";
356  in = "Tue Jul %22 17:28:44 CEST 2003";
357  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
359  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
361 
362  fmt = "%a %b %%%d %H:%M:%S %Z %Y";
363  in = "Tue Jul %22 17:28:44 CEST 2003";
364  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
366  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
368 
369  fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y";
370  in = "abc\n 19 October %22 17:28:44 CEST 2003";
371  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
373  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
375 
376  fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y";
377  in = "abc\n 18 October %34 17:28:44 CEST 80";
378  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
380  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
382 
383  fmt = "";
384  in = "abc\n 18 October %34 17:28:44 CEST 80";
385  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
387  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
389 
390  fmt = NULL;
391  in = "1980-04-12 3:49:44 ";
392  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
394  printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i);
396 
397  fmt = "%B %d, %Y. Time: %I:%M%p";
398  in = "July 14, 1988. Time: 9:15am";
399  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
401  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
403 
404  in = "September 6 at 01:30 pm in the year 1983";
405  fmt = "%B %d at %I:%M %p in the year %Y";
406  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
408  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
410 
411  in = " 1976, July 14. Time: 9:15am";
412  fmt = "%Y, %B %d. Time: %I:%M %p";
413  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
415  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
417 
418  in = " 1976, July 14. Time: 9:15 am";
419  fmt = "%Y, %B %d. Time: %I:%M%p";
420  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
422  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
424 
425  in = " 1976, P.M. July 14. Time: 9:15";
426  fmt = "%Y, %P %B %d. Time: %I:%M";
427  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
429  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
431 
432  in = "1234567890";
433  fmt = "%s";
434  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
436  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
438 
439  out = (char*) malloc(64);
440  fmt = "%a %b %d %H:%M:%S %Y";
441  in = "Mon Dec 30 17:28:44 2019";
442  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
443  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
444  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
445  free(out);
446 
447  out = (char*) malloc(64);
448  fmt = "%a %b %d %H:%M:%S %Y";
449  in = "Mon December 30 17:28:44 2019";
450  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
451  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
452  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
453  free(out);
454 
455  { ECPGtrans(__LINE__, NULL, "rollback");
456 #line 381 "dt_test.pgc"
457 
458 if (sqlca.sqlcode < 0) sqlprint ( );}
459 #line 381 "dt_test.pgc"
460 
461  { ECPGdisconnect(__LINE__, "CURRENT");
462 #line 382 "dt_test.pgc"
463 
464 if (sqlca.sqlcode < 0) sqlprint ( );}
465 #line 382 "dt_test.pgc"
466 
467 
468  return 0;
469 }
struct varlena text
Definition: c.h:700
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:673
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:255
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_timestamp
Definition: ecpgtype.h:54
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
@ ECPGt_date
Definition: ecpgtype.h:53
void sqlprint(void)
Definition: error.c:334
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2277
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
int i
Definition: isn.c:73
static void const char * fmt
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
date PGTYPESdate_from_timestamp(timestamp dt)
Definition: datetime.c:31
int PGTYPESdate_dayofweek(date dDate)
Definition: datetime.c:138
long date
Definition: pgtypes_date.h:9
date PGTYPESdate_from_asc(char *str, char **endptr)
Definition: datetime.c:47
int PGTYPESdate_fmt_asc(date dDate, const char *fmtstring, char *outbuf)
Definition: datetime.c:167
int PGTYPESdate_defmt_asc(date *d, const char *fmt, const char *str)
Definition: datetime.c:329
void PGTYPESdate_julmdy(date jd, int *mdy)
Definition: datetime.c:115
void PGTYPESdate_mdyjul(int *mdy, date *jdate)
Definition: datetime.c:128
char * PGTYPESdate_to_asc(date dDate)
Definition: datetime.c:101
void PGTYPESinterval_free(interval *intvl)
Definition: interval.c:997
char * PGTYPESinterval_to_asc(interval *span)
Definition: interval.c:1062
int PGTYPESinterval_copy(interval *intvlsrc, interval *intvldest)
Definition: interval.c:1082
interval * PGTYPESinterval_from_asc(char *str, char **endptr)
Definition: interval.c:1003
timestamp PGTYPEStimestamp_from_asc(char *str, char **endptr)
Definition: timestamp.c:206
int64 timestamp
void PGTYPEStimestamp_current(timestamp *ts)
Definition: timestamp.c:294
char * PGTYPEStimestamp_to_asc(timestamp tstamp)
Definition: timestamp.c:272
int PGTYPEStimestamp_fmt_asc(timestamp *ts, char *output, int str_len, const char *fmtstr)
Definition: timestamp.c:782
int PGTYPEStimestamp_defmt_asc(const char *str, const char *fmt, timestamp *d)
Definition: timestamp.c:810
int main(void)
#define ECPGdebug(X, Y)
#define printf(...)
Definition: port.h:244
#define sqlca
Definition: sqlca.h:59
Definition: c.h:687