~ubuntu-branches/debian/sid/postgresql-9.3/sid

« back to all changes in this revision

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

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