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
8exec sql include ../regression;
9
10char *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 */
42static 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
49char *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
58int
59main(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	ECPGdebug(1, stderr);
73
74	ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
75	text = PGTYPEStimestamp_to_asc(ts1);
76
77	printf("timestamp: %s\n", text);
78	PGTYPESchar_free(text);
79
80	date1 = PGTYPESdate_from_timestamp(ts1);
81	dc = PGTYPESdate_new();
82	*dc = date1;
83	text = PGTYPESdate_to_asc(*dc);
84	printf("Date of timestamp: %s\n", text);
85	PGTYPESchar_free(text);
86	PGTYPESdate_free(dc);
87
88	for (i = 0; dates[i]; i++)
89	{
90		bool err = false;
91		date1 = PGTYPESdate_from_asc(dates[i], &endptr);
92		if (date1 == INT_MIN) {
93			err = true;
94		}
95		text = PGTYPESdate_to_asc(date1);
96		printf("Date[%d]: %s (%c - %c)\n",
97					i, err ? "-" : text,
98					endptr ? 'N' : 'Y',
99					err ? 'T' : 'F');
100		PGTYPESchar_free(text);
101		if (!err)
102		{
103			for (j = 0; times[j]; j++)
104			{
105				int length = strlen(dates[i])
106						+ 1
107						+ strlen(times[j])
108						+ 1;
109				char* t = malloc(length);
110				sprintf(t, "%s %s", dates[i], times[j]);
111				ts1 = PGTYPEStimestamp_from_asc(t, NULL);
112				text = PGTYPEStimestamp_to_asc(ts1);
113				printf("TS[%d,%d]: %s\n",
114				       i, j, errno ? "-" : text);
115				PGTYPESchar_free(text);
116				free(t);
117			}
118		}
119	}
120
121	ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
122
123	for (i = 0; intervals[i]; i++)
124	{
125		interval *ic;
126		i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
127		if (*endptr)
128			printf("endptr set to %s\n", endptr);
129		if (!i1)
130		{
131			printf("Error parsing interval %d\n", i);
132			continue;
133		}
134		j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
135		if (j < 0)
136			continue;
137		text = PGTYPESinterval_to_asc(i1);
138		printf("interval[%d]: %s\n", i, text ? text : "-");
139		PGTYPESchar_free(text);
140
141		ic = PGTYPESinterval_new();
142		PGTYPESinterval_copy(i1, ic);
143		text = PGTYPESinterval_to_asc(i1);
144		printf("interval_copy[%d]: %s\n", i, text ? text : "-");
145		PGTYPESchar_free(text);
146		PGTYPESinterval_free(ic);
147		PGTYPESinterval_free(i1);
148	}
149
150	return (0);
151}
152