LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/pgtypeslib - dt_test2.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 55 57 96.5 %
Date: 2024-11-21 08:14:44 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 <limits.h>
       5             : #include <pgtypes_date.h>
       6             : #include <pgtypes_timestamp.h>
       7             : 
       8             : exec sql include ../regression;
       9             : 
      10             : char *dates[] = { "19990108foobar",
      11             :                   "19990108 foobar",
      12             :                   "1999-01-08 foobar",
      13             :                   "January 8, 1999",
      14             :                   "1999-01-08",
      15             :                   "1/8/1999",
      16             :                   "1/18/1999",
      17             :                   "01/02/03",
      18             :                   "1999-Jan-08",
      19             :                   "Jan-08-1999",
      20             :                   "08-Jan-1999",
      21             :                   "99-Jan-08",
      22             :                   "08-Jan-99",
      23             :                   "08-Jan-06",
      24             :                   "Jan-08-99",
      25             :                   "19990108",
      26             :                   "990108",
      27             :                   "1999.008",
      28             :                   "J2451187",
      29             :                   "January 8, 99 BC",
      30             :                   /*
      31             :                    * Maximize space usage in ParseDateTime() with 25
      32             :                    * (MAXDATEFIELDS) fields and 128 (MAXDATELEN) total length.
      33             :                    */
      34             :                   "........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      35             :                   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      36             :                   /* 26 fields */
      37             :                   ".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      38             :                   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
      39             :                   NULL };
      40             : 
      41             : /* do not conflict with libc "times" symbol */
      42             : static char *times[] = { "0:04",
      43             :                   "1:59 PDT",
      44             :                   "13:24:40 -8:00",
      45             :                   "13:24:40.495+3",
      46             :                   "13:24:40.123456123+3",
      47             :                   NULL };
      48             : 
      49             : char *intervals[] = { "1 minute",
      50             :                       "1 12:59:10",
      51             :                       "2 day 12 hour 59 minute 10 second",
      52             :                       "1 days 12 hrs 59 mins 10 secs",
      53             :                       "1 days 1 hours 1 minutes 1 seconds",
      54             :                       "1 year 59 mins",
      55             :                       "1 year 59 mins foobar",
      56             :                       NULL };
      57             : 
      58             : int
      59           4 : main(void)
      60             : {
      61             :     exec sql begin declare section;
      62             :         date date1;
      63             :         timestamp ts1, ts2;
      64             :         char *text;
      65             :         interval *i1;
      66             :         date *dc;
      67             :     exec sql end declare section;
      68             : 
      69             :     int i, j;
      70             :     char *endptr;
      71             : 
      72           4 :     ECPGdebug(1, stderr);
      73             : 
      74           4 :     ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
      75           4 :     text = PGTYPEStimestamp_to_asc(ts1);
      76             : 
      77           4 :     printf("timestamp: %s\n", text);
      78           4 :     PGTYPESchar_free(text);
      79             : 
      80           4 :     date1 = PGTYPESdate_from_timestamp(ts1);
      81           4 :     dc = PGTYPESdate_new();
      82           4 :     *dc = date1;
      83           4 :     text = PGTYPESdate_to_asc(*dc);
      84           4 :     printf("Date of timestamp: %s\n", text);
      85           4 :     PGTYPESchar_free(text);
      86           4 :     PGTYPESdate_free(dc);
      87             : 
      88          92 :     for (i = 0; dates[i]; i++)
      89             :     {
      90          88 :         bool err = false;
      91          88 :         date1 = PGTYPESdate_from_asc(dates[i], &endptr);
      92          88 :         if (date1 == INT_MIN) {
      93          20 :             err = true;
      94             :         }
      95          88 :         text = PGTYPESdate_to_asc(date1);
      96         176 :         printf("Date[%d]: %s (%c - %c)\n",
      97             :                     i, err ? "-" : text,
      98          88 :                     endptr ? 'N' : 'Y',
      99             :                     err ? 'T' : 'F');
     100          88 :         PGTYPESchar_free(text);
     101          88 :         if (!err)
     102             :         {
     103         408 :             for (j = 0; times[j]; j++)
     104             :             {
     105         340 :                 int length = strlen(dates[i])
     106             :                         + 1
     107         340 :                         + strlen(times[j])
     108         340 :                         + 1;
     109         340 :                 char* t = malloc(length);
     110         340 :                 sprintf(t, "%s %s", dates[i], times[j]);
     111         340 :                 ts1 = PGTYPEStimestamp_from_asc(t, NULL);
     112         340 :                 text = PGTYPEStimestamp_to_asc(ts1);
     113         340 :                 printf("TS[%d,%d]: %s\n",
     114         340 :                        i, j, errno ? "-" : text);
     115         340 :                 PGTYPESchar_free(text);
     116         340 :                 free(t);
     117             :             }
     118             :         }
     119             :     }
     120             : 
     121           4 :     ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
     122             : 
     123          32 :     for (i = 0; intervals[i]; i++)
     124             :     {
     125             :         interval *ic;
     126          28 :         i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
     127          28 :         if (*endptr)
     128           0 :             printf("endptr set to %s\n", endptr);
     129          28 :         if (!i1)
     130             :         {
     131           4 :             printf("Error parsing interval %d\n", i);
     132           4 :             continue;
     133             :         }
     134          24 :         j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
     135          24 :         if (j < 0)
     136           0 :             continue;
     137          24 :         text = PGTYPESinterval_to_asc(i1);
     138          24 :         printf("interval[%d]: %s\n", i, text ? text : "-");
     139          24 :         PGTYPESchar_free(text);
     140             : 
     141          24 :         ic = PGTYPESinterval_new();
     142          24 :         PGTYPESinterval_copy(i1, ic);
     143          24 :         text = PGTYPESinterval_to_asc(i1);
     144          24 :         printf("interval_copy[%d]: %s\n", i, text ? text : "-");
     145          24 :         PGTYPESchar_free(text);
     146          24 :         PGTYPESinterval_free(ic);
     147          24 :         PGTYPESinterval_free(i1);
     148             :     }
     149             : 
     150           4 :     return 0;
     151             : }

Generated by: LCOV version 1.14