~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                                  NULL };
 
31
 
 
32
/* do not conflict with libc "times" symbol */
 
33
static char *times[] = { "0:04",
 
34
                                  "1:59 PDT",
 
35
                                  "13:24:40 -8:00",
 
36
                                  "13:24:40.495+3",
 
37
                                  NULL };
 
38
 
 
39
char *intervals[] = { "1 minute",
 
40
                                          "1 12:59:10",
 
41
                                          "2 day 12 hour 59 minute 10 second",
 
42
                                          "1 days 12 hrs 59 mins 10 secs",
 
43
                                          "1 days 1 hours 1 minutes 1 seconds",
 
44
                                          "1 year 59 mins",
 
45
                                          "1 year 59 mins foobar",
 
46
                                          NULL };
 
47
 
 
48
int
 
49
main(void)
 
50
{
 
51
        exec sql begin declare section;
 
52
                date date1;
 
53
                timestamp ts1, ts2;
 
54
                char *text;
 
55
                interval *i1;
 
56
                date *dc;
 
57
        exec sql end declare section;
 
58
 
 
59
        int i, j;
 
60
        char *endptr;
 
61
 
 
62
        ECPGdebug(1, stderr);
 
63
 
 
64
        ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
 
65
        text = PGTYPEStimestamp_to_asc(ts1);
 
66
 
 
67
        printf("timestamp: %s\n", text);
 
68
        free(text);
 
69
 
 
70
        date1 = PGTYPESdate_from_timestamp(ts1);
 
71
        dc = PGTYPESdate_new();
 
72
        *dc = date1;
 
73
        text = PGTYPESdate_to_asc(*dc);
 
74
        printf("Date of timestamp: %s\n", text);
 
75
        free(text);
 
76
        PGTYPESdate_free(dc);
 
77
 
 
78
        for (i = 0; dates[i]; i++)
 
79
        {
 
80
                bool err = false;
 
81
                date1 = PGTYPESdate_from_asc(dates[i], &endptr);
 
82
                if (date1 == INT_MIN) {
 
83
                        err = true;
 
84
                }
 
85
                text = PGTYPESdate_to_asc(date1);
 
86
                printf("Date[%d]: %s (%c - %c)\n",
 
87
                                        i, err ? "-" : text,
 
88
                                        endptr ? 'N' : 'Y',
 
89
                                        err ? 'T' : 'F');
 
90
                free(text);
 
91
                if (!err)
 
92
                {
 
93
                        for (j = 0; times[j]; j++)
 
94
                        {
 
95
                                int length = strlen(dates[i])
 
96
                                                + 1
 
97
                                                + strlen(times[j])
 
98
                                                + 1;
 
99
                                char* t = malloc(length);
 
100
                                sprintf(t, "%s %s", dates[i], times[j]);
 
101
                                ts1 = PGTYPEStimestamp_from_asc(t, NULL);
 
102
                                text = PGTYPEStimestamp_to_asc(ts1);
 
103
                                if (i != 19 || j != 3) /* timestamp as integer or double differ for this case */
 
104
                                        printf("TS[%d,%d]: %s\n",
 
105
                                                i, j, errno ? "-" : text);
 
106
                                free(text);
 
107
                                free(t);
 
108
                        }
 
109
                }
 
110
        }
 
111
 
 
112
        ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);
 
113
 
 
114
        for (i = 0; intervals[i]; i++)
 
115
        {
 
116
                interval *ic;
 
117
                i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
 
118
                if (*endptr)
 
119
                        printf("endptr set to %s\n", endptr);
 
120
                if (!i1)
 
121
                {
 
122
                        printf("Error parsing interval %d\n", i);
 
123
                        continue;
 
124
                }
 
125
                j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
 
126
                if (j < 0)
 
127
                        continue;
 
128
                text = PGTYPESinterval_to_asc(i1);
 
129
                printf("interval[%d]: %s\n", i, text ? text : "-");
 
130
                free(text);
 
131
 
 
132
                ic = PGTYPESinterval_new();
 
133
                PGTYPESinterval_copy(i1, ic);
 
134
                text = PGTYPESinterval_to_asc(i1);
 
135
                printf("interval_copy[%d]: %s\n", i, text ? text : "-");
 
136
                free(text);
 
137
                PGTYPESinterval_free(ic);
 
138
                PGTYPESinterval_free(i1);
 
139
        }
 
140
 
 
141
        return (0);
 
142
}