Header And Logo

PostgreSQL
| The world's most advanced open source database.

pgtypeslib-dt_test2.c

Go to the documentation of this file.
00001 /* Processed by ecpg (regression mode) */
00002 /* These include files are added by the preprocessor */
00003 #include <ecpglib.h>
00004 #include <ecpgerrno.h>
00005 #include <sqlca.h>
00006 /* End of automatic include section */
00007 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
00008 
00009 #line 1 "dt_test2.pgc"
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <limits.h>
00014 #include <pgtypes_date.h>
00015 #include <pgtypes_timestamp.h>
00016 
00017 
00018 #line 1 "regression.h"
00019 
00020 
00021 
00022 
00023 
00024 
00025 #line 8 "dt_test2.pgc"
00026 
00027 
00028 char *dates[] = { "19990108foobar",
00029                   "19990108 foobar",
00030                   "1999-01-08 foobar",
00031                   "January 8, 1999",
00032                   "1999-01-08",
00033                   "1/8/1999",
00034                   "1/18/1999",
00035                   "01/02/03",
00036                   "1999-Jan-08",
00037                   "Jan-08-1999",
00038                   "08-Jan-1999",
00039                   "99-Jan-08",
00040                   "08-Jan-99",
00041                   "08-Jan-06",
00042                   "Jan-08-99",
00043                   "19990108",
00044                   "990108",
00045                   "1999.008",
00046                   "J2451187",
00047                   "January 8, 99 BC",
00048                   NULL };
00049 
00050 /* do not conflict with libc "times" symbol */
00051 static char *times[] = { "0:04",
00052                   "1:59 PDT",
00053                   "13:24:40 -8:00",
00054                   "13:24:40.495+3",
00055                   NULL };
00056 
00057 char *intervals[] = { "1 minute",
00058                       "1 12:59:10",
00059                       "2 day 12 hour 59 minute 10 second",
00060                       "1 days 12 hrs 59 mins 10 secs",
00061                       "1 days 1 hours 1 minutes 1 seconds",
00062                       "1 year 59 mins",
00063                       "1 year 59 mins foobar",
00064                       NULL };
00065 
00066 int
00067 main(void)
00068 {
00069     /* exec sql begin declare section */
00070          
00071           
00072          
00073          
00074          
00075     
00076 #line 52 "dt_test2.pgc"
00077  date date1 ;
00078  
00079 #line 53 "dt_test2.pgc"
00080  timestamp ts1 , ts2 ;
00081  
00082 #line 54 "dt_test2.pgc"
00083  char * text ;
00084  
00085 #line 55 "dt_test2.pgc"
00086  interval * i1 ;
00087  
00088 #line 56 "dt_test2.pgc"
00089  date * dc ;
00090 /* exec sql end declare section */
00091 #line 57 "dt_test2.pgc"
00092 
00093 
00094     int i, j;
00095     char *endptr;
00096 
00097     ECPGdebug(1, stderr);
00098 
00099     ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
00100     text = PGTYPEStimestamp_to_asc(ts1);
00101 
00102     printf("timestamp: %s\n", text);
00103     free(text);
00104 
00105     date1 = PGTYPESdate_from_timestamp(ts1);
00106     dc = PGTYPESdate_new();
00107     *dc = date1;
00108     text = PGTYPESdate_to_asc(*dc);
00109     printf("Date of timestamp: %s\n", text);
00110     free(text);
00111     PGTYPESdate_free(dc);
00112 
00113     for (i = 0; dates[i]; i++)
00114     {
00115         bool err = false;
00116         date1 = PGTYPESdate_from_asc(dates[i], &endptr);
00117         if (date1 == INT_MIN) {
00118             err = true;
00119         }
00120         text = PGTYPESdate_to_asc(date1);
00121         printf("Date[%d]: %s (%c - %c)\n",
00122                     i, err ? "-" : text,
00123                     endptr ? 'N' : 'Y',
00124                     err ? 'T' : 'F');
00125         free(text);
00126         if (!err)
00127         {
00128             for (j = 0; times[j]; j++)
00129             {
00130                 int length = strlen(dates[i])
00131                         + 1
00132                         + strlen(times[j])
00133                         + 1;
00134                 char* t = malloc(length);
00135                 sprintf(t, "%s %s", dates[i], times[j]);
00136                 ts1 = PGTYPEStimestamp_from_asc(t, NULL);
00137                 text = PGTYPEStimestamp_to_asc(ts1);
00138                 if (i != 19 || j != 3) /* timestamp as integer or double differ for this case */
00139                     printf("TS[%d,%d]: %s\n",
00140                         i, j, errno ? "-" : text);
00141                 free(text);
00142                 free(t);
00143             }
00144         }
00145     }
00146 
00147     ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
00148 
00149     for (i = 0; intervals[i]; i++)
00150     {
00151         interval *ic;
00152         i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
00153         if (*endptr)
00154             printf("endptr set to %s\n", endptr);
00155         if (!i1)
00156         {
00157             printf("Error parsing interval %d\n", i);
00158             continue;
00159         }
00160         j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
00161         if (j < 0)
00162             continue;
00163         text = PGTYPESinterval_to_asc(i1);
00164         printf("interval[%d]: %s\n", i, text ? text : "-");
00165         free(text);
00166 
00167         ic = PGTYPESinterval_new();
00168         PGTYPESinterval_copy(i1, ic);
00169         text = PGTYPESinterval_to_asc(i1);
00170         printf("interval_copy[%d]: %s\n", i, text ? text : "-");
00171         free(text);
00172         PGTYPESinterval_free(ic);
00173         PGTYPESinterval_free(i1);
00174     }
00175 
00176     return (0);
00177 }