~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

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

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

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