LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/pgtypeslib - dt_test.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 300 300 100.0 %
Date: 2024-04-25 04:11:22 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <stdio.h>
       2             : #include <string.h>
       3             : #include <stdlib.h>
       4             : #include <pgtypes_date.h>
       5             : #include <pgtypes_timestamp.h>
       6             : #include <pgtypes_interval.h>
       7             : 
       8             : exec sql include ../regression;
       9             : 
      10             : int
      11           4 : main(void)
      12             : {
      13             :     exec sql begin declare section;
      14             :         date date1;
      15             :         timestamp ts1;
      16             :         interval *iv1, iv2;
      17             :         char *text;
      18             :     exec sql end declare section;
      19             :     date date2;
      20           4 :     int mdy[3] = { 4, 19, 1998 };
      21             :     char *fmt, *out, *in;
      22           4 :     char *d1 = "Mon Jan 17 1966";
      23           4 :     char *t1 = "2000-7-12 17:34:29";
      24             :     int i;
      25             : 
      26           4 :     ECPGdebug(1, stderr);
      27             :     exec sql whenever sqlerror do sqlprint();
      28           4 :     exec sql connect to REGRESSDB1;
      29           4 :     exec sql create table date_test (d date, ts timestamp);
      30           4 :     exec sql set datestyle to iso;
      31           4 :     exec sql set intervalstyle to postgres_verbose;
      32           4 : 
      33           4 :     date1 = PGTYPESdate_from_asc(d1, NULL);
      34           4 :     ts1 = PGTYPEStimestamp_from_asc(t1, NULL);
      35             : 
      36           4 :     exec sql insert into date_test(d, ts) values (:date1, :ts1);
      37           4 : 
      38           4 :     exec sql select * into :date1, :ts1 from date_test where d=:date1;
      39           4 : 
      40           4 :     text = PGTYPESdate_to_asc(date1);
      41           4 :     printf ("Date: %s\n", text);
      42           4 :     PGTYPESchar_free(text);
      43             : 
      44           4 :     text = PGTYPEStimestamp_to_asc(ts1);
      45           4 :     printf ("timestamp: %s\n", text);
      46           4 :     PGTYPESchar_free(text);
      47             : 
      48           4 :     iv1 = PGTYPESinterval_from_asc("13556 days 12 hours 34 minutes 14 seconds ", NULL);
      49           4 :     PGTYPESinterval_copy(iv1, &iv2);
      50           4 :     text = PGTYPESinterval_to_asc(&iv2);
      51           4 :     printf ("interval: %s\n", text);
      52           4 :     PGTYPESinterval_free(iv1);
      53           4 :     PGTYPESchar_free(text);
      54             : 
      55           4 :     PGTYPESdate_mdyjul(mdy, &date2);
      56           4 :     printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
      57             :     /* reset */
      58           4 :     mdy[0] = mdy[1] = mdy[2] = 0;
      59             : 
      60           4 :     printf("date seems to get encoded to julian %ld\n", date2);
      61             : 
      62           4 :     PGTYPESdate_julmdy(date2, mdy);
      63           4 :     printf("m: %d, d: %d, y: %d\n", mdy[0], mdy[1], mdy[2]);
      64             : 
      65           4 :     ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
      66           4 :     text = PGTYPEStimestamp_to_asc(ts1);
      67           4 :     fmt = "(ddd), mmm. dd, yyyy, repeat: (ddd), mmm. dd, yyyy. end";
      68           4 :     out = (char*) malloc(strlen(fmt) + 1);
      69           4 :     date1 = PGTYPESdate_from_timestamp(ts1);
      70           4 :     PGTYPESdate_fmt_asc(date1, fmt, out);
      71           4 :     printf("date_day of %s is %d\n", text, PGTYPESdate_dayofweek(date1));
      72           4 :     printf("Above date in format \"%s\" is \"%s\"\n", fmt, out);
      73           4 :     PGTYPESchar_free(text);
      74           4 :     free(out);
      75             : 
      76           4 :     out = (char*) malloc(48);
      77           4 :     i = PGTYPEStimestamp_fmt_asc(&ts1, out, 47, "Which is day number %j in %Y.");
      78           4 :     printf("%s\n", out);
      79           4 :     free(out);
      80             : 
      81             : 
      82             :     /* rdate_defmt_asc() */
      83             : 
      84           4 :     date1 = 0;
      85           4 :     fmt = "yy/mm/dd";
      86           4 :     in = "In the year 1995, the month of December, it is the 25th day";
      87             :     /*    0123456789012345678901234567890123456789012345678901234567890
      88             :      *    0         1         2         3         4         5         6
      89             :      */
      90           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
      91           4 :     text = PGTYPESdate_to_asc(date1);
      92           4 :     printf("date_defmt_asc1: %s\n", text);
      93           4 :     PGTYPESchar_free(text);
      94             : 
      95           4 :     date1 = 0;
      96           4 :     fmt = "mmmm. dd. yyyy";
      97           4 :     in = "12/25/95";
      98           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
      99           4 :     text = PGTYPESdate_to_asc(date1);
     100           4 :     printf("date_defmt_asc2: %s\n", text);
     101           4 :     PGTYPESchar_free(text);
     102             : 
     103           4 :     date1 = 0;
     104           4 :     fmt = "yy/mm/dd";
     105           4 :     in = "95/12/25";
     106           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     107           4 :     text = PGTYPESdate_to_asc(date1);
     108           4 :     printf("date_defmt_asc3: %s\n", text);
     109           4 :     PGTYPESchar_free(text);
     110             : 
     111           4 :     date1 = 0;
     112           4 :     fmt = "yy/mm/dd";
     113           4 :     in = "1995, December 25th";
     114           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     115           4 :     text = PGTYPESdate_to_asc(date1);
     116           4 :     printf("date_defmt_asc4: %s\n", text);
     117           4 :     PGTYPESchar_free(text);
     118             : 
     119           4 :     date1 = 0;
     120           4 :     fmt = "dd-mm-yy";
     121           4 :     in = "This is 25th day of December, 1995";
     122           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     123           4 :     text = PGTYPESdate_to_asc(date1);
     124           4 :     printf("date_defmt_asc5: %s\n", text);
     125           4 :     PGTYPESchar_free(text);
     126             : 
     127           4 :     date1 = 0;
     128           4 :     fmt = "mmddyy";
     129           4 :     in = "Dec. 25th, 1995";
     130           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     131           4 :     text = PGTYPESdate_to_asc(date1);
     132           4 :     printf("date_defmt_asc6: %s\n", text);
     133           4 :     PGTYPESchar_free(text);
     134             : 
     135           4 :     date1 = 0;
     136           4 :     fmt = "mmm. dd. yyyy";
     137           4 :     in = "dec 25th 1995";
     138           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     139           4 :     text = PGTYPESdate_to_asc(date1);
     140           4 :     printf("date_defmt_asc7: %s\n", text);
     141           4 :     PGTYPESchar_free(text);
     142             : 
     143           4 :     date1 = 0;
     144           4 :     fmt = "mmm. dd. yyyy";
     145           4 :     in = "DEC-25-1995";
     146           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     147           4 :     text = PGTYPESdate_to_asc(date1);
     148           4 :     printf("date_defmt_asc8: %s\n", text);
     149           4 :     PGTYPESchar_free(text);
     150             : 
     151           4 :     date1 = 0;
     152           4 :     fmt = "mm yy   dd.";
     153           4 :     in = "12199525";
     154           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     155           4 :     text = PGTYPESdate_to_asc(date1);
     156           4 :     printf("date_defmt_asc9: %s\n", text);
     157           4 :     PGTYPESchar_free(text);
     158             : 
     159           4 :     date1 = 0;
     160           4 :     fmt = "yyyy fierj mm   dd.";
     161           4 :     in = "19951225";
     162           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     163           4 :     text = PGTYPESdate_to_asc(date1);
     164           4 :     printf("date_defmt_asc10: %s\n", text);
     165           4 :     PGTYPESchar_free(text);
     166             : 
     167           4 :     date1 = 0;
     168           4 :     fmt = "mm/dd/yy";
     169           4 :     in = "122595";
     170           4 :     PGTYPESdate_defmt_asc(&date1, fmt, in);
     171           4 :     text = PGTYPESdate_to_asc(date1);
     172           4 :     printf("date_defmt_asc12: %s\n", text);
     173           4 :     PGTYPESchar_free(text);
     174             : 
     175           4 :     PGTYPEStimestamp_current(&ts1);
     176           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     177             :     /* can't output this in regression mode */
     178             :     /* printf("timestamp_current: Now: %s\n", text); */
     179           4 :     PGTYPESchar_free(text);
     180             : 
     181           4 :     ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL);
     182           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     183           4 :     printf("timestamp_to_asc1: %s\n", text);
     184           4 :     PGTYPESchar_free(text);
     185             : 
     186           4 :     ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL);
     187           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     188           4 :     printf("timestamp_to_asc2: %s\n", text);
     189           4 :     PGTYPESchar_free(text);
     190             : 
     191           4 :     ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL);
     192           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     193           4 :     printf("timestamp_to_asc3: %s\n", text);
     194           4 :     PGTYPESchar_free(text);
     195             : 
     196             : /*  abc-03:10:35-def-02/11/94-gh  */
     197             : /*      12345678901234567890123456789 */
     198             : 
     199           4 :     out = (char*) malloc(32);
     200           4 :     i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%");
     201           4 :     printf("timestamp_fmt_asc: %d: %s\n", i, out);
     202           4 :     free(out);
     203             : 
     204           4 :     fmt = "This is a %m/%d/%y %H-%Ml%Stest";
     205           4 :     in =  "This is a 4/12/80 3-39l12test";
     206           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     207           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     208           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     209           4 :     PGTYPESchar_free(text);
     210             : 
     211           4 :     fmt = "%a %b %d %H:%M:%S %z %Y";
     212           4 :     in =  "Tue Jul 22 17:28:44 +0200 2003";
     213           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     214           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     215           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     216           4 :     PGTYPESchar_free(text);
     217             : 
     218           4 :     fmt = "%a %b %d %H:%M:%S %z %Y";
     219           4 :     in =  "Tue Feb 29 17:28:44 +0200 2000";
     220           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     221           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     222           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     223           4 :     PGTYPESchar_free(text);
     224             : 
     225           4 :     fmt = "%a %b %d %H:%M:%S %z %Y";
     226           4 :     in =  "Tue Feb 29 17:28:44 +0200 1900";
     227           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     228           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     229           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     230           4 :     PGTYPESchar_free(text);
     231             : 
     232           4 :     fmt = "%a %b %d %H:%M:%S %z %Y";
     233           4 :     in =  "Tue Feb 29 17:28:44 +0200 1996";
     234           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     235           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     236           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     237           4 :     PGTYPESchar_free(text);
     238             : 
     239           4 :     fmt = "%b %d %H:%M:%S %z %Y";
     240           4 :     in =  "      Jul 31 17:28:44 +0200 1996";
     241           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     242           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     243           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     244           4 :     PGTYPESchar_free(text);
     245             : 
     246           4 :     fmt = "%b %d %H:%M:%S %z %Y";
     247           4 :     in =  "      Jul 32 17:28:44 +0200 1996";
     248           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     249           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     250           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     251           4 :     PGTYPESchar_free(text);
     252             : 
     253           4 :     fmt = "%a %b %d %H:%M:%S %z %Y";
     254           4 :     in =  "Tue Feb 29 17:28:44 +0200 1997";
     255           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     256           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     257           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     258           4 :     PGTYPESchar_free(text);
     259             : 
     260           4 :     fmt = "%";
     261           4 :     in =  "Tue Jul 22 17:28:44 +0200 2003";
     262           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     263           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     264           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     265           4 :     PGTYPESchar_free(text);
     266             : 
     267           4 :     fmt = "a %";
     268           4 :     in =  "Tue Jul 22 17:28:44 +0200 2003";
     269           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     270           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     271           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     272           4 :     PGTYPESchar_free(text);
     273             : 
     274           4 :     fmt = "%b, %d %H_%M`%S %z %Y";
     275           4 :     in =  "    Jul, 22 17_28 `44 +0200  2003  ";
     276           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     277           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     278           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     279           4 :     PGTYPESchar_free(text);
     280             : 
     281           4 :     fmt = "%a %b %%%d %H:%M:%S %Z %Y";
     282           4 :     in =  "Tue Jul %22 17:28:44 CEST 2003";
     283           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     284           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     285           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     286           4 :     PGTYPESchar_free(text);
     287             : 
     288           4 :     fmt = "%a %b %%%d %H:%M:%S %Z %Y";
     289           4 :     in =  "Tue Jul %22 17:28:44 CEST 2003";
     290           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     291           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     292           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     293           4 :     PGTYPESchar_free(text);
     294             : 
     295           4 :     fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y";
     296           4 :     in =  "abc\n   19 October %22 17:28:44 CEST 2003";
     297           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     298           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     299           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     300           4 :     PGTYPESchar_free(text);
     301             : 
     302           4 :     fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y";
     303           4 :     in =  "abc\n   18 October %34 17:28:44 CEST 80";
     304           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     305           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     306           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     307           4 :     PGTYPESchar_free(text);
     308             : 
     309           4 :     fmt = "";
     310           4 :     in =  "abc\n   18 October %34 17:28:44 CEST 80";
     311           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     312           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     313           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i);
     314           4 :     PGTYPESchar_free(text);
     315             : 
     316           4 :     fmt = NULL;
     317           4 :     in =  "1980-04-12 3:49:44      ";
     318           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     319           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     320           4 :     printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i);
     321           4 :     PGTYPESchar_free(text);
     322             : 
     323           4 :     fmt = "%B %d, %Y. Time: %I:%M%p";
     324           4 :     in =  "July 14, 1988. Time: 9:15am";
     325           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     326           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     327           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     328           4 :     PGTYPESchar_free(text);
     329             : 
     330           4 :     in = "September 6 at 01:30 pm in the year 1983";
     331           4 :     fmt = "%B %d at %I:%M %p in the year %Y";
     332           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     333           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     334           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     335           4 :     PGTYPESchar_free(text);
     336             : 
     337           4 :     in = "  1976, July 14. Time: 9:15am";
     338           4 :     fmt = "%Y,   %B %d. Time: %I:%M %p";
     339           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     340           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     341           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     342           4 :     PGTYPESchar_free(text);
     343             : 
     344           4 :     in = "  1976, July 14. Time: 9:15 am";
     345           4 :     fmt = "%Y,   %B %d. Time: %I:%M%p";
     346           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     347           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     348           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     349           4 :     PGTYPESchar_free(text);
     350             : 
     351           4 :     in = "  1976, P.M. July 14. Time: 9:15";
     352           4 :     fmt = "%Y, %P  %B %d. Time: %I:%M";
     353           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     354           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     355           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     356           4 :     PGTYPESchar_free(text);
     357             : 
     358           4 :     in = "1234567890";
     359           4 :     fmt = "%s";
     360           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     361           4 :     text = PGTYPEStimestamp_to_asc(ts1);
     362           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i);
     363           4 :     PGTYPESchar_free(text);
     364             : 
     365           4 :     out = (char*) malloc(64);
     366           4 :     fmt = "%a %b %d %H:%M:%S %Y";
     367           4 :     in =  "Mon Dec 30 17:28:44 2019";
     368           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     369           4 :     i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
     370           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
     371           4 :     free(out);
     372             : 
     373           4 :     out = (char*) malloc(64);
     374           4 :     fmt = "%a %b %d %H:%M:%S %Y";
     375           4 :     in =  "Mon December 30 17:28:44 2019";
     376           4 :     i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1);
     377           4 :     i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt);
     378           4 :     printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i);
     379           4 :     free(out);
     380             : 
     381           4 :     exec sql rollback;
     382           4 :         exec sql disconnect;
     383           4 : 
     384           4 :     return 0;
     385             : }

Generated by: LCOV version 1.14