PostgreSQL Source Code  git master
pgtypeslib-dt_test2.c File Reference
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <pgtypes_date.h>
#include <pgtypes_timestamp.h>
Include dependency graph for pgtypeslib-dt_test2.c:

Go to the source code of this file.

Macros

#define ECPGdebug(X, Y)   ECPGdebug((X)+100,(Y))
 

Functions

int main (void)
 

Variables

char * dates []
 
static char * times []
 
char * intervals []
 

Macro Definition Documentation

◆ ECPGdebug

#define ECPGdebug (   X,
 
)    ECPGdebug((X)+100,(Y))

Definition at line 7 of file pgtypeslib-dt_test2.c.

Referenced by main().

Function Documentation

◆ main()

int main ( void  )

Definition at line 77 of file pgtypeslib-dt_test2.c.

References dates, ECPGdebug, free, i, intervals, malloc, PGTYPESchar_free(), PGTYPESdate_free(), PGTYPESdate_from_asc(), PGTYPESdate_from_timestamp(), PGTYPESdate_new(), PGTYPESdate_to_asc(), PGTYPESinterval_copy(), PGTYPESinterval_free(), PGTYPESinterval_from_asc(), PGTYPESinterval_new(), PGTYPESinterval_to_asc(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_from_asc(), PGTYPEStimestamp_to_asc(), printf, sprintf, and times.

78 {
79  /* exec sql begin declare section */
80 
81 
82 
83 
84 
85 
86 #line 62 "dt_test2.pgc"
87  date date1 ;
88 
89 #line 63 "dt_test2.pgc"
90  timestamp ts1 , ts2 ;
91 
92 #line 64 "dt_test2.pgc"
93  char * text ;
94 
95 #line 65 "dt_test2.pgc"
96  interval * i1 ;
97 
98 #line 66 "dt_test2.pgc"
99  date * dc ;
100 /* exec sql end declare section */
101 #line 67 "dt_test2.pgc"
102 
103 
104  int i, j;
105  char *endptr;
106 
107  ECPGdebug(1, stderr);
108 
109  ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
110  text = PGTYPEStimestamp_to_asc(ts1);
111 
112  printf("timestamp: %s\n", text);
113  PGTYPESchar_free(text);
114 
115  date1 = PGTYPESdate_from_timestamp(ts1);
116  dc = PGTYPESdate_new();
117  *dc = date1;
118  text = PGTYPESdate_to_asc(*dc);
119  printf("Date of timestamp: %s\n", text);
120  PGTYPESchar_free(text);
121  PGTYPESdate_free(dc);
122 
123  for (i = 0; dates[i]; i++)
124  {
125  bool err = false;
126  date1 = PGTYPESdate_from_asc(dates[i], &endptr);
127  if (date1 == INT_MIN) {
128  err = true;
129  }
130  text = PGTYPESdate_to_asc(date1);
131  printf("Date[%d]: %s (%c - %c)\n",
132  i, err ? "-" : text,
133  endptr ? 'N' : 'Y',
134  err ? 'T' : 'F');
135  PGTYPESchar_free(text);
136  if (!err)
137  {
138  for (j = 0; times[j]; j++)
139  {
140  int length = strlen(dates[i])
141  + 1
142  + strlen(times[j])
143  + 1;
144  char* t = malloc(length);
145  sprintf(t, "%s %s", dates[i], times[j]);
146  ts1 = PGTYPEStimestamp_from_asc(t, NULL);
147  text = PGTYPEStimestamp_to_asc(ts1);
148  printf("TS[%d,%d]: %s\n",
149  i, j, errno ? "-" : text);
150  PGTYPESchar_free(text);
151  free(t);
152  }
153  }
154  }
155 
156  ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
157 
158  for (i = 0; intervals[i]; i++)
159  {
160  interval *ic;
161  i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
162  if (*endptr)
163  printf("endptr set to %s\n", endptr);
164  if (!i1)
165  {
166  printf("Error parsing interval %d\n", i);
167  continue;
168  }
169  j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
170  if (j < 0)
171  continue;
172  text = PGTYPESinterval_to_asc(i1);
173  printf("interval[%d]: %s\n", i, text ? text : "-");
174  PGTYPESchar_free(text);
175 
176  ic = PGTYPESinterval_new();
177  PGTYPESinterval_copy(i1, ic);
178  text = PGTYPESinterval_to_asc(i1);
179  printf("interval_copy[%d]: %s\n", i, text ? text : "-");
180  PGTYPESchar_free(text);
183  }
184 
185  return 0;
186 }
char * intervals[]
date PGTYPESdate_from_asc(char *, char **)
Definition: datetime.c:47
int PGTYPESinterval_copy(interval *, interval *)
Definition: interval.c:1086
int64 timestamp
#define printf(...)
Definition: port.h:198
long date
Definition: pgtypes_date.h:9
char * PGTYPESdate_to_asc(date)
Definition: datetime.c:101
#define malloc(a)
Definition: header.h:50
void PGTYPESinterval_free(interval *)
Definition: interval.c:1001
#define sprintf
Definition: port.h:194
#define ECPGdebug(X, Y)
interval * PGTYPESinterval_from_asc(char *, char **)
Definition: interval.c:1007
int PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
Definition: timestamp.c:863
static char * times[]
struct varlena text
Definition: c.h:563
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
void PGTYPESdate_free(date *)
Definition: datetime.c:25
#define free(a)
Definition: header.h:65
char * PGTYPEStimestamp_to_asc(timestamp)
Definition: timestamp.c:272
date * PGTYPESdate_new(void)
Definition: datetime.c:15
char * PGTYPESinterval_to_asc(interval *)
Definition: interval.c:1066
int i
interval * PGTYPESinterval_new(void)
Definition: interval.c:991
date PGTYPESdate_from_timestamp(timestamp)
Definition: datetime.c:31
timestamp PGTYPEStimestamp_from_asc(char *, char **)
Definition: timestamp.c:206
char * dates[]

Variable Documentation

◆ dates

char* dates[]
Initial value:
= { "19990108foobar",
"19990108 foobar",
"1999-01-08 foobar",
"January 8, 1999",
"1999-01-08",
"1/8/1999",
"1/18/1999",
"01/02/03",
"1999-Jan-08",
"Jan-08-1999",
"08-Jan-1999",
"99-Jan-08",
"08-Jan-99",
"08-Jan-06",
"Jan-08-99",
"19990108",
"990108",
"1999.008",
"J2451187",
"January 8, 99 BC",
"........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
NULL }

Definition at line 28 of file pgtypeslib-dt_test2.c.

Referenced by main().

◆ intervals

char* intervals[]
Initial value:
= { "1 minute",
"1 12:59:10",
"2 day 12 hour 59 minute 10 second",
"1 days 12 hrs 59 mins 10 secs",
"1 days 1 hours 1 minutes 1 seconds",
"1 year 59 mins",
"1 year 59 mins foobar",
NULL }

Definition at line 67 of file pgtypeslib-dt_test2.c.

Referenced by main().

◆ times

char* times[]
static
Initial value:
= { "0:04",
"1:59 PDT",
"13:24:40 -8:00",
"13:24:40.495+3",
"13:24:40.123456123+3",
NULL }

Definition at line 60 of file pgtypeslib-dt_test2.c.

Referenced by getrusage(), and main().