Header And Logo

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

pgtypeslib-dt_test.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_test.pgc"
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <pgtypes_date.h>
00014 #include <pgtypes_timestamp.h>
00015 #include <pgtypes_interval.h>
00016 
00017 
00018 #line 1 "regression.h"
00019 
00020 
00021 
00022 
00023 
00024 
00025 #line 8 "dt_test.pgc"
00026 
00027 
00028 int
00029 main(void)
00030 {
00031     /* exec sql begin declare section */
00032          
00033          
00034           
00035          
00036     
00037 #line 14 "dt_test.pgc"
00038  date date1 ;
00039  
00040 #line 15 "dt_test.pgc"
00041  timestamp ts1 ;
00042  
00043 #line 16 "dt_test.pgc"
00044  interval * iv1 , iv2 ;
00045  
00046 #line 17 "dt_test.pgc"
00047  char * text ;
00048 /* exec sql end declare section */
00049 #line 18 "dt_test.pgc"
00050 
00051     date date2;
00052     int mdy[3] = { 4, 19, 1998 };
00053     char *fmt, *out, *in;
00054     char *d1 = "Mon Jan 17 1966";
00055     char *t1 = "2000-7-12 17:34:29";
00056     int i;
00057 
00058     ECPGdebug(1, stderr);
00059     /* exec sql whenever sqlerror  do sqlprint ( ) ; */
00060 #line 27 "dt_test.pgc"
00061 
00062     { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); 
00063 #line 28 "dt_test.pgc"
00064 
00065 if (sqlca.sqlcode < 0) sqlprint ( );}
00066 #line 28 "dt_test.pgc"
00067 
00068     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table date_test ( d date , ts timestamp )", ECPGt_EOIT, ECPGt_EORT);
00069 #line 29 "dt_test.pgc"
00070 
00071 if (sqlca.sqlcode < 0) sqlprint ( );}
00072 #line 29 "dt_test.pgc"
00073 
00074     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
00075 #line 30 "dt_test.pgc"
00076 
00077 if (sqlca.sqlcode < 0) sqlprint ( );}
00078 #line 30 "dt_test.pgc"
00079 
00080     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set intervalstyle to postgres_verbose", ECPGt_EOIT, ECPGt_EORT);
00081 #line 31 "dt_test.pgc"
00082 
00083 if (sqlca.sqlcode < 0) sqlprint ( );}
00084 #line 31 "dt_test.pgc"
00085 
00086 
00087     date1 = PGTYPESdate_from_asc(d1, NULL);
00088     ts1 = PGTYPEStimestamp_from_asc(t1, NULL);
00089 
00090     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into date_test ( d , ts ) values ( $1  , $2  )", 
00091     ECPGt_date,&(date1),(long)1,(long)1,sizeof(date), 
00092     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
00093     ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp), 
00094     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
00095 #line 36 "dt_test.pgc"
00096 
00097 if (sqlca.sqlcode < 0) sqlprint ( );}
00098 #line 36 "dt_test.pgc"
00099 
00100 
00101     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select * from date_test where d = $1 ", 
00102     ECPGt_date,&(date1),(long)1,(long)1,sizeof(date), 
00103     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
00104     ECPGt_date,&(date1),(long)1,(long)1,sizeof(date), 
00105     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
00106     ECPGt_timestamp,&(ts1),(long)1,(long)1,sizeof(timestamp), 
00107     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
00108 #line 38 "dt_test.pgc"
00109 
00110 if (sqlca.sqlcode < 0) sqlprint ( );}
00111 #line 38 "dt_test.pgc"
00112 
00113 
00114     text = PGTYPESdate_to_asc(date1);
00115     printf ("Date: %s\n", text);
00116     free(text);
00117 
00118     text = PGTYPEStimestamp_to_asc(ts1);
00119     printf ("timestamp: %s\n", text);
00120     free(text);
00121 
00122     iv1 = PGTYPESinterval_from_asc("13556 days 12 hours 34 minutes 14 seconds ", NULL);
00123     PGTYPESinterval_copy(iv1, &iv2);
00124     text = PGTYPESinterval_to_asc(&iv2);
00125     printf ("interval: %s\n", text);
00126     PGTYPESinterval_free(iv1);
00127     free(text);
00128 
00129     PGTYPESdate_mdyjul(mdy, &date2);
00130     printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
00131     /* reset */
00132     mdy[0] = mdy[1] = mdy[2] = 0;
00133 
00134     printf("date seems to get encoded to julian %ld\n", date2);
00135 
00136     PGTYPESdate_julmdy(date2, mdy);
00137     printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
00138 
00139     ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
00140     text = PGTYPEStimestamp_to_asc(ts1);
00141     fmt = "(ddd), mmm. dd, yyyy, repeat: (ddd), mmm. dd, yyyy. end";
00142     out = (char*) malloc(strlen(fmt) + 1);
00143     date1 = PGTYPESdate_from_timestamp(ts1);
00144     PGTYPESdate_fmt_asc(date1, fmt, out);
00145     printf("date_day of %s is %d\n", text, PGTYPESdate_dayofweek(date1));
00146     printf("Above date in format \"%s\" is \"%s\"\n", fmt, out);
00147     free(text);
00148     free(out);
00149 
00150     /* rdate_defmt_asc() */
00151 
00152     date1 = 0; text = "";
00153     fmt = "yy/mm/dd";
00154     in = "In the year 1995, the month of December, it is the 25th day";
00155     /*    0123456789012345678901234567890123456789012345678901234567890
00156      *    0         1         2         3         4         5         6
00157      */
00158     PGTYPESdate_defmt_asc(&date1, fmt, in);
00159     text = PGTYPESdate_to_asc(date1);
00160     printf("date_defmt_asc1: %s\n", text);
00161     free(text);
00162 
00163     date1 = 0; text = "";
00164     fmt = "mmmm. dd. yyyy";
00165     in = "12/25/95";
00166     PGTYPESdate_defmt_asc(&date1, fmt, in);
00167     text = PGTYPESdate_to_asc(date1);
00168     printf("date_defmt_asc2: %s\n", text);
00169     free(text);
00170 
00171     date1 = 0; text = "";
00172     fmt = "yy/mm/dd";
00173     in = "95/12/25";
00174     PGTYPESdate_defmt_asc(&date1, fmt, in);
00175     text = PGTYPESdate_to_asc(date1);
00176     printf("date_defmt_asc3: %s\n", text);
00177     free(text);
00178 
00179     date1 = 0; text = "";
00180     fmt = "yy/mm/dd";
00181     in = "1995, December 25th";
00182     PGTYPESdate_defmt_asc(&date1, fmt, in);
00183     text = PGTYPESdate_to_asc(date1);
00184     printf("date_defmt_asc4: %s\n", text);
00185     free(text);
00186 
00187     date1 = 0; text = "";
00188     fmt = "dd-mm-yy";
00189     in = "This is 25th day of December, 1995";
00190     PGTYPESdate_defmt_asc(&date1, fmt, in);
00191     text = PGTYPESdate_to_asc(date1);
00192     printf("date_defmt_asc5: %s\n", text);
00193     free(text);
00194 
00195     date1 = 0; text = "";
00196     fmt = "mmddyy";
00197     in = "Dec. 25th, 1995";
00198     PGTYPESdate_defmt_asc(&date1, fmt, in);
00199     text = PGTYPESdate_to_asc(date1);
00200     printf("date_defmt_asc6: %s\n", text);
00201     free(text);
00202 
00203     date1 = 0; text = "";
00204     fmt = "mmm. dd. yyyy";
00205     in = "dec 25th 1995";
00206     PGTYPESdate_defmt_asc(&date1, fmt, in);
00207     text = PGTYPESdate_to_asc(date1);
00208     printf("date_defmt_asc7: %s\n", text);
00209     free(text);
00210 
00211     date1 = 0; text = "";
00212     fmt = "mmm. dd. yyyy";
00213     in = "DEC-25-1995";
00214     PGTYPESdate_defmt_asc(&date1, fmt, in);
00215     text = PGTYPESdate_to_asc(date1);
00216     printf("date_defmt_asc8: %s\n", text);
00217     free(text);
00218 
00219     date1 = 0; text = "";
00220     fmt = "mm yy   dd.";
00221     in = "12199525";
00222     PGTYPESdate_defmt_asc(&date1, fmt, in);
00223     text = PGTYPESdate_to_asc(date1);
00224     printf("date_defmt_asc9: %s\n", text);
00225     free(text);
00226 
00227     date1 = 0; text = "";
00228     fmt = "yyyy fierj mm   dd.";
00229     in = "19951225";
00230     PGTYPESdate_defmt_asc(&date1, fmt, in);
00231     text = PGTYPESdate_to_asc(date1);
00232     printf("date_defmt_asc10: %s\n", text);
00233     free(text);
00234 
00235     date1 = 0; text = "";
00236     fmt = "mm/dd/yy";
00237     in = "122595";
00238     PGTYPESdate_defmt_asc(&date1, fmt, in);
00239     text = PGTYPESdate_to_asc(date1);
00240     printf("date_defmt_asc12: %s\n", text);
00241     free(text);
00242 
00243     PGTYPEStimestamp_current(&ts1);
00244     text = PGTYPEStimestamp_to_asc(ts1);
00245     /* can't output this in regression mode */
00246     /* printf("timestamp_current: Now: %s\n", text); */
00247     free(text);
00248 
00249     ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL);
00250     text = PGTYPEStimestamp_to_asc(ts1);
00251     printf("timestamp_to_asc1: %s\n", text);
00252     free(text);
00253 
00254     ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL);
00255     text = PGTYPEStimestamp_to_asc(ts1);
00256     printf("timestamp_to_asc2: %s\n", text);
00257     free(text);
00258 
00259     ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL);
00260     text = PGTYPEStimestamp_to_asc(ts1);
00261     printf("timestamp_to_asc3: %s\n", text);
00262     free(text);
00263 
00264 /*  abc-03:10:35-def-02/11/94-gh  */
00265 /*      12345678901234567890123456789 */
00266 
00267     out = (char*) malloc(32);
00268     i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%");
00269     printf("timestamp_fmt_asc: %d: %s\n", i, out);
00270     free(out);
00271 
00272     fmt = "This is a %m/%d/%y %H-%Ml%Stest";
00273     in =  "This is a 4/12/80 3-39l12test";
00274     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00275     text = PGTYPEStimestamp_to_asc(ts1);
00276     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00277     free(text);
00278 
00279     fmt = "%a %b %d %H:%M:%S %z %Y";
00280     in =  "Tue Jul 22 17:28:44 +0200 2003";
00281     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00282     text = PGTYPEStimestamp_to_asc(ts1);
00283     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00284     free(text);
00285 
00286     fmt = "%a %b %d %H:%M:%S %z %Y";
00287     in =  "Tue Feb 29 17:28:44 +0200 2000";
00288     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00289     text = PGTYPEStimestamp_to_asc(ts1);
00290     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00291     free(text);
00292 
00293     fmt = "%a %b %d %H:%M:%S %z %Y";
00294     in =  "Tue Feb 29 17:28:44 +0200 1900";
00295     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00296     text = PGTYPEStimestamp_to_asc(ts1);
00297     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00298     free(text);
00299 
00300     fmt = "%a %b %d %H:%M:%S %z %Y";
00301     in =  "Tue Feb 29 17:28:44 +0200 1996";
00302     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00303     text = PGTYPEStimestamp_to_asc(ts1);
00304     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00305     free(text);
00306 
00307     fmt = "%b %d %H:%M:%S %z %Y";
00308     in =  "      Jul 31 17:28:44 +0200 1996";
00309     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00310     text = PGTYPEStimestamp_to_asc(ts1);
00311     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00312     free(text);
00313 
00314     fmt = "%b %d %H:%M:%S %z %Y";
00315     in =  "      Jul 32 17:28:44 +0200 1996";
00316     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00317     text = PGTYPEStimestamp_to_asc(ts1);
00318     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00319     free(text);
00320 
00321     fmt = "%a %b %d %H:%M:%S %z %Y";
00322     in =  "Tue Feb 29 17:28:44 +0200 1997";
00323     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00324     text = PGTYPEStimestamp_to_asc(ts1);
00325     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00326     free(text);
00327 
00328     fmt = "%";
00329     in =  "Tue Jul 22 17:28:44 +0200 2003";
00330     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00331     text = PGTYPEStimestamp_to_asc(ts1);
00332     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00333     free(text);
00334 
00335     fmt = "a %";
00336     in =  "Tue Jul 22 17:28:44 +0200 2003";
00337     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00338     text = PGTYPEStimestamp_to_asc(ts1);
00339     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00340     free(text);
00341 
00342     fmt = "%b, %d %H_%M`%S %z %Y";
00343     in =  "    Jul, 22 17_28 `44 +0200  2003  ";
00344     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00345     text = PGTYPEStimestamp_to_asc(ts1);
00346     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00347     free(text);
00348 
00349     fmt = "%a %b %%%d %H:%M:%S %Z %Y";
00350     in =  "Tue Jul %22 17:28:44 CEST 2003";
00351     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00352     text = PGTYPEStimestamp_to_asc(ts1);
00353     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00354     free(text);
00355 
00356     fmt = "%a %b %%%d %H:%M:%S %Z %Y";
00357     in =  "Tue Jul %22 17:28:44 CEST 2003";
00358     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00359     text = PGTYPEStimestamp_to_asc(ts1);
00360     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00361     free(text);
00362 
00363     fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y";
00364     in =  "abc\n   19 October %22 17:28:44 CEST 2003";
00365     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00366     text = PGTYPEStimestamp_to_asc(ts1);
00367     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00368     free(text);
00369 
00370     fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y";
00371     in =  "abc\n   18 October %34 17:28:44 CEST 80";
00372     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00373     text = PGTYPEStimestamp_to_asc(ts1);
00374     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00375     free(text);
00376 
00377     fmt = "";
00378     in =  "abc\n   18 October %34 17:28:44 CEST 80";
00379     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00380     text = PGTYPEStimestamp_to_asc(ts1);
00381     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
00382     free(text);
00383 
00384     fmt = NULL;
00385     in =  "1980-04-12 3:49:44      ";
00386     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00387     text = PGTYPEStimestamp_to_asc(ts1);
00388     printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i);
00389     free(text);
00390 
00391     fmt = "%B %d, %Y. Time: %I:%M%p";
00392     in =  "July 14, 1988. Time: 9:15am";
00393     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00394     text = PGTYPEStimestamp_to_asc(ts1);
00395     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00396     free(text);
00397 
00398     in = "September 6 at 01:30 pm in the year 1983";
00399     fmt = "%B %d at %I:%M %p in the year %Y";
00400     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00401     text = PGTYPEStimestamp_to_asc(ts1);
00402     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00403     free(text);
00404 
00405     in = "  1976, July 14. Time: 9:15am";
00406     fmt = "%Y,   %B %d. Time: %I:%M %p";
00407     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00408     text = PGTYPEStimestamp_to_asc(ts1);
00409     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00410     free(text);
00411 
00412     in = "  1976, July 14. Time: 9:15 am";
00413     fmt = "%Y,   %B %d. Time: %I:%M%p";
00414     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00415     text = PGTYPEStimestamp_to_asc(ts1);
00416     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00417     free(text);
00418 
00419     in = "  1976, P.M. July 14. Time: 9:15";
00420     fmt = "%Y, %P  %B %d. Time: %I:%M";
00421     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00422     text = PGTYPEStimestamp_to_asc(ts1);
00423     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00424     free(text);
00425 
00426     in = "1234567890";
00427     fmt = "%s";
00428     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
00429     text = PGTYPEStimestamp_to_asc(ts1);
00430     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
00431     free(text);
00432 
00433     { ECPGtrans(__LINE__, NULL, "rollback");
00434 #line 359 "dt_test.pgc"
00435 
00436 if (sqlca.sqlcode < 0) sqlprint ( );}
00437 #line 359 "dt_test.pgc"
00438 
00439         { ECPGdisconnect(__LINE__, "CURRENT");
00440 #line 360 "dt_test.pgc"
00441 
00442 if (sqlca.sqlcode < 0) sqlprint ( );}
00443 #line 360 "dt_test.pgc"
00444 
00445 
00446     return (0);
00447 }