PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_error.h>
15 #include <pgtypes_timestamp.h>
16 #include <pgtypes_interval.h>
17 
18 
19 #line 1 "regression.h"
20 
21 
22 
23 
24 
25 
26 #line 9 "dt_test.pgc"
27 
28 
29 static void check_errno(void);
30 
31 int
32 main(void)
33 {
34  /* exec sql begin declare section */
35 
36 
37 
38 
39 
40 #line 17 "dt_test.pgc"
41  date date1 ;
42 
43 #line 18 "dt_test.pgc"
44  timestamp ts1 ;
45 
46 #line 19 "dt_test.pgc"
47  interval * iv1 , iv2 ;
48 
49 #line 20 "dt_test.pgc"
50  char * text ;
51 /* exec sql end declare section */
52 #line 21 "dt_test.pgc"
53 
54  date date2;
55  int mdy[3] = { 4, 19, 1998 };
56  char *fmt, *out, *in;
57  char *d1 = "Mon Jan 17 1966";
58  char *t1 = "2000-7-12 17:34:29";
59  int i;
60 
61  ECPGdebug(1, stderr);
62  /* exec sql whenever sqlerror do sqlprint ( ) ; */
63 #line 30 "dt_test.pgc"
64 
65  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
66 #line 31 "dt_test.pgc"
67 
68 if (sqlca.sqlcode < 0) sqlprint ( );}
69 #line 31 "dt_test.pgc"
70 
71  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table date_test ( d date , ts timestamp )", ECPGt_EOIT, ECPGt_EORT);
72 #line 32 "dt_test.pgc"
73 
74 if (sqlca.sqlcode < 0) sqlprint ( );}
75 #line 32 "dt_test.pgc"
76 
77  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
78 #line 33 "dt_test.pgc"
79 
80 if (sqlca.sqlcode < 0) sqlprint ( );}
81 #line 33 "dt_test.pgc"
82 
83  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set intervalstyle to postgres_verbose", ECPGt_EOIT, ECPGt_EORT);
84 #line 34 "dt_test.pgc"
85 
86 if (sqlca.sqlcode < 0) sqlprint ( );}
87 #line 34 "dt_test.pgc"
88 
89 
90  date1 = PGTYPESdate_from_asc(d1, NULL);
91  ts1 = PGTYPEStimestamp_from_asc(t1, NULL);
92 
93  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into date_test ( d , ts ) values ( $1 , $2 )",
94  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
95  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
96  ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
97  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
98 #line 39 "dt_test.pgc"
99 
100 if (sqlca.sqlcode < 0) sqlprint ( );}
101 #line 39 "dt_test.pgc"
102 
103 
104  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from date_test where d = $1 ",
105  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
106  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
107  ECPGt_date,&(date1),(long)1,(long)1,sizeof(date),
108  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
109  ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp),
110  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
111 #line 41 "dt_test.pgc"
112 
113 if (sqlca.sqlcode < 0) sqlprint ( );}
114 #line 41 "dt_test.pgc"
115 
116 
117  text = PGTYPESdate_to_asc(date1);
118  printf ("Date: %s\n", text);
120 
122  printf ("timestamp: %s\n", text);
124 
125  iv1 = PGTYPESinterval_from_asc("13556 days 12 hours 34 minutes 14 seconds ", NULL);
126  PGTYPESinterval_copy(iv1, &iv2);
128  printf ("interval: %s\n", text);
131 
132  PGTYPESdate_mdyjul(mdy, &date2);
133  printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
134  /* reset */
135  mdy[0] = mdy[1] = mdy[2] = 0;
136 
137  printf("date seems to get encoded to julian %ld\n", date2);
138 
139  PGTYPESdate_julmdy(date2, mdy);
140  printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
141 
142  ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
144  fmt = "(ddd), mmm. dd, yyyy, repeat: (ddd), mmm. dd, yyyy. end";
145  out = (char*) malloc(strlen(fmt) + 1);
146  date1 = PGTYPESdate_from_timestamp(ts1);
147  PGTYPESdate_fmt_asc(date1, fmt, out);
148  printf("date_day of %s is %d\n", text, PGTYPESdate_dayofweek(date1));
149  printf("Above date in format \"%s\" is \"%s\"\n", fmt, out);
151  free(out);
152 
153  out = (char*) malloc(48);
154  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 47, "Which is day number %j in %Y.");
155  printf("%s\n", out);
156  free(out);
157 
158 
159  /* rdate_defmt_asc() */
160 
161  date1 = 0;
162  fmt = "yy/mm/dd";
163  in = "In the year 1995, the month of December, it is the 25th day";
164  /* 0123456789012345678901234567890123456789012345678901234567890
165  * 0 1 2 3 4 5 6
166  */
167  PGTYPESdate_defmt_asc(&date1, fmt, in);
168  text = PGTYPESdate_to_asc(date1);
169  printf("date_defmt_asc1: %s\n", text);
171 
172  date1 = 0;
173  fmt = "mmmm. dd. yyyy";
174  in = "12/25/95";
175  PGTYPESdate_defmt_asc(&date1, fmt, in);
176  text = PGTYPESdate_to_asc(date1);
177  printf("date_defmt_asc2: %s\n", text);
179 
180  date1 = 0;
181  fmt = "yy/mm/dd";
182  in = "95/12/25";
183  PGTYPESdate_defmt_asc(&date1, fmt, in);
184  text = PGTYPESdate_to_asc(date1);
185  printf("date_defmt_asc3: %s\n", text);
187 
188  date1 = 0;
189  fmt = "yy/mm/dd";
190  in = "1995, December 25th";
191  PGTYPESdate_defmt_asc(&date1, fmt, in);
192  text = PGTYPESdate_to_asc(date1);
193  printf("date_defmt_asc4: %s\n", text);
195 
196  date1 = 0;
197  fmt = "dd-mm-yy";
198  in = "This is 25th day of December, 1995";
199  PGTYPESdate_defmt_asc(&date1, fmt, in);
200  text = PGTYPESdate_to_asc(date1);
201  printf("date_defmt_asc5: %s\n", text);
203 
204  date1 = 0;
205  fmt = "mmddyy";
206  in = "Dec. 25th, 1995";
207  PGTYPESdate_defmt_asc(&date1, fmt, in);
208  text = PGTYPESdate_to_asc(date1);
209  printf("date_defmt_asc6: %s\n", text);
211 
212  date1 = 0;
213  fmt = "mmm. dd. yyyy";
214  in = "dec 25th 1995";
215  PGTYPESdate_defmt_asc(&date1, fmt, in);
216  text = PGTYPESdate_to_asc(date1);
217  printf("date_defmt_asc7: %s\n", text);
219 
220  date1 = 0;
221  fmt = "mmm. dd. yyyy";
222  in = "DEC-25-1995";
223  PGTYPESdate_defmt_asc(&date1, fmt, in);
224  text = PGTYPESdate_to_asc(date1);
225  printf("date_defmt_asc8: %s\n", text);
227 
228  date1 = 0;
229  fmt = "mm yy dd.";
230  in = "12199525";
231  PGTYPESdate_defmt_asc(&date1, fmt, in);
232  text = PGTYPESdate_to_asc(date1);
233  printf("date_defmt_asc9: %s\n", text);
235 
236  date1 = 0;
237  fmt = "yyyy fierj mm dd.";
238  in = "19951225";
239  PGTYPESdate_defmt_asc(&date1, fmt, in);
240  text = PGTYPESdate_to_asc(date1);
241  printf("date_defmt_asc10: %s\n", text);
243 
244  date1 = 0;
245  fmt = "mm/dd/yy";
246  in = "122595";
247  PGTYPESdate_defmt_asc(&date1, fmt, in);
248  text = PGTYPESdate_to_asc(date1);
249  printf("date_defmt_asc12: %s\n", text);
251 
254  /* can't output this in regression mode */
255  /* printf("timestamp_current: Now: %s\n", text); */
257 
258  ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL);
260  printf("timestamp_to_asc1: %s\n", text);
262 
263  ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL);
265  printf("timestamp_to_asc2: %s\n", text);
267 
268  ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL);
269  /* failure, check error code */
270  check_errno();
272  printf("timestamp_to_asc3: %s\n", text);
274 
275  ts1 = PGTYPEStimestamp_from_asc("AM95000062", NULL);
276  /* failure, check error code */
277  check_errno();
279  printf("timestamp_to_asc4: %s\n", text);
281 
282 /* abc-03:10:35-def-02/11/94-gh */
283 /* 12345678901234567890123456789 */
284 
285  out = (char*) malloc(32);
286  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%");
287  printf("timestamp_fmt_asc: %d: %s\n", i, out);
288  free(out);
289 
290  fmt = "This is a %m/%d/%y %H-%Ml%Stest";
291  in = "This is a 4/12/80 3-39l12test";
292  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
294  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
296 
297  fmt = "%a %b %d %H:%M:%S %z %Y";
298  in = "Tue Jul 22 17:28:44 +0200 2003";
299  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
301  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
303 
304  fmt = "%a %b %d %H:%M:%S %z %Y";
305  in = "Tue Feb 29 17:28:44 +0200 2000";
306  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
308  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
310 
311  fmt = "%a %b %d %H:%M:%S %z %Y";
312  in = "Tue Feb 29 17:28:44 +0200 1900";
313  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
315  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
317 
318  fmt = "%a %b %d %H:%M:%S %z %Y";
319  in = "Tue Feb 29 17:28:44 +0200 1996";
320  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
322  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
324 
325  fmt = "%b %d %H:%M:%S %z %Y";
326  in = " Jul 31 17:28:44 +0200 1996";
327  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
329  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
331 
332  fmt = "%b %d %H:%M:%S %z %Y";
333  in = " Jul 32 17:28:44 +0200 1996";
334  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
336  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
338 
339  fmt = "%a %b %d %H:%M:%S %z %Y";
340  in = "Tue Feb 29 17:28:44 +0200 1997";
341  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
343  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
345 
346  fmt = "%";
347  in = "Tue Jul 22 17:28:44 +0200 2003";
348  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
350  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
352 
353  fmt = "a %";
354  in = "Tue Jul 22 17:28:44 +0200 2003";
355  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
357  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
359 
360  fmt = "%b, %d %H_%M`%S %z %Y";
361  in = " Jul, 22 17_28 `44 +0200 2003 ";
362  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
364  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
366 
367  fmt = "%a %b %%%d %H:%M:%S %Z %Y";
368  in = "Tue Jul %22 17:28:44 CEST 2003";
369  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
371  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
373 
374  fmt = "%a %b %%%d %H:%M:%S %Z %Y";
375  in = "Tue Jul %22 17:28:44 CEST 2003";
376  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
378  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
380 
381  fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y";
382  in = "abc\n 19 October %22 17:28:44 CEST 2003";
383  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
385  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
387 
388  fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y";
389  in = "abc\n 18 October %34 17:28:44 CEST 80";
390  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
392  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
394 
395  fmt = "";
396  in = "abc\n 18 October %34 17:28:44 CEST 80";
397  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
399  printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
401 
402  fmt = NULL;
403  in = "1980-04-12 3:49:44 ";
404  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
406  printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i);
408 
409  fmt = "%B %d, %Y. Time: %I:%M%p";
410  in = "July 14, 1988. Time: 9:15am";
411  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
413  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
415 
416  in = "September 6 at 01:30 pm in the year 1983";
417  fmt = "%B %d at %I:%M %p in the year %Y";
418  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
420  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
422 
423  in = " 1976, July 14. Time: 9:15am";
424  fmt = "%Y, %B %d. Time: %I:%M %p";
425  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
427  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
429 
430  in = " 1976, July 14. Time: 9:15 am";
431  fmt = "%Y, %B %d. Time: %I:%M%p";
432  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
434  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
436 
437  in = " 1976, P.M. July 14. Time: 9:15";
438  fmt = "%Y, %P %B %d. Time: %I:%M";
439  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
441  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
443 
444  in = "1234567890";
445  fmt = "%s";
446  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
448  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
450 
451  out = (char*) malloc(64);
452  fmt = "%a %b %d %H:%M:%S %Y";
453  in = "Mon Dec 30 17:28:44 2019";
454  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
455  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
456  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
457  free(out);
458 
459  out = (char*) malloc(64);
460  fmt = "%a %b %d %H:%M:%S %Y";
461  in = "Mon December 30 17:28:44 2019";
462  i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
463  i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
464  printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
465  free(out);
466 
467  { ECPGtrans(__LINE__, NULL, "rollback");
468 #line 393 "dt_test.pgc"
469 
470 if (sqlca.sqlcode < 0) sqlprint ( );}
471 #line 393 "dt_test.pgc"
472 
473  { ECPGdisconnect(__LINE__, "CURRENT");
474 #line 394 "dt_test.pgc"
475 
476 if (sqlca.sqlcode < 0) sqlprint ( );}
477 #line 394 "dt_test.pgc"
478 
479 
480  return 0;
481 }
482 
483 static void
485 {
486  switch(errno)
487  {
488  case 0:
489  printf("(no errno set) - ");
490  break;
492  printf("(errno == PGTYPES_TS_BAD_TIMESTAMP) - ");
493  break;
494  default:
495  printf("(unknown errno (%d))\n", errno);
496  printf("(libc: (%s)) ", strerror(errno));
497  break;
498  }
499 }
struct varlena text
Definition: c.h:679
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:676
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:2275
#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:72
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
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15
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:202
int64 timestamp
void PGTYPEStimestamp_current(timestamp *ts)
Definition: timestamp.c:290
char * PGTYPEStimestamp_to_asc(timestamp tstamp)
Definition: timestamp.c:268
int PGTYPEStimestamp_fmt_asc(timestamp *ts, char *output, int str_len, const char *fmtstr)
Definition: timestamp.c:778
int PGTYPEStimestamp_defmt_asc(const char *str, const char *fmt, timestamp *d)
Definition: timestamp.c:806
static void check_errno(void)
int main(void)
#define ECPGdebug(X, Y)
#define strerror
Definition: port.h:251
#define printf(...)
Definition: port.h:244
#define sqlca
Definition: sqlca.h:59
Definition: c.h:666