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 8exec sql include ../regression; 9 10int 11main(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 PGTYPESchar_free(text); 43 44 text = PGTYPEStimestamp_to_asc(ts1); 45 printf ("timestamp: %s\n", text); 46 PGTYPESchar_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 PGTYPESchar_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 PGTYPESchar_free(text); 74 free(out); 75 76 out = (char*) malloc(48); 77 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 47, "Which is day number %j in %Y."); 78 printf("%s\n", out); 79 free(out); 80 81 82 /* rdate_defmt_asc() */ 83 84 date1 = 0; text = ""; 85 fmt = "yy/mm/dd"; 86 in = "In the year 1995, the month of December, it is the 25th day"; 87 /* 0123456789012345678901234567890123456789012345678901234567890 88 * 0 1 2 3 4 5 6 89 */ 90 PGTYPESdate_defmt_asc(&date1, fmt, in); 91 text = PGTYPESdate_to_asc(date1); 92 printf("date_defmt_asc1: %s\n", text); 93 PGTYPESchar_free(text); 94 95 date1 = 0; text = ""; 96 fmt = "mmmm. dd. yyyy"; 97 in = "12/25/95"; 98 PGTYPESdate_defmt_asc(&date1, fmt, in); 99 text = PGTYPESdate_to_asc(date1); 100 printf("date_defmt_asc2: %s\n", text); 101 PGTYPESchar_free(text); 102 103 date1 = 0; text = ""; 104 fmt = "yy/mm/dd"; 105 in = "95/12/25"; 106 PGTYPESdate_defmt_asc(&date1, fmt, in); 107 text = PGTYPESdate_to_asc(date1); 108 printf("date_defmt_asc3: %s\n", text); 109 PGTYPESchar_free(text); 110 111 date1 = 0; text = ""; 112 fmt = "yy/mm/dd"; 113 in = "1995, December 25th"; 114 PGTYPESdate_defmt_asc(&date1, fmt, in); 115 text = PGTYPESdate_to_asc(date1); 116 printf("date_defmt_asc4: %s\n", text); 117 PGTYPESchar_free(text); 118 119 date1 = 0; text = ""; 120 fmt = "dd-mm-yy"; 121 in = "This is 25th day of December, 1995"; 122 PGTYPESdate_defmt_asc(&date1, fmt, in); 123 text = PGTYPESdate_to_asc(date1); 124 printf("date_defmt_asc5: %s\n", text); 125 PGTYPESchar_free(text); 126 127 date1 = 0; text = ""; 128 fmt = "mmddyy"; 129 in = "Dec. 25th, 1995"; 130 PGTYPESdate_defmt_asc(&date1, fmt, in); 131 text = PGTYPESdate_to_asc(date1); 132 printf("date_defmt_asc6: %s\n", text); 133 PGTYPESchar_free(text); 134 135 date1 = 0; text = ""; 136 fmt = "mmm. dd. yyyy"; 137 in = "dec 25th 1995"; 138 PGTYPESdate_defmt_asc(&date1, fmt, in); 139 text = PGTYPESdate_to_asc(date1); 140 printf("date_defmt_asc7: %s\n", text); 141 PGTYPESchar_free(text); 142 143 date1 = 0; text = ""; 144 fmt = "mmm. dd. yyyy"; 145 in = "DEC-25-1995"; 146 PGTYPESdate_defmt_asc(&date1, fmt, in); 147 text = PGTYPESdate_to_asc(date1); 148 printf("date_defmt_asc8: %s\n", text); 149 PGTYPESchar_free(text); 150 151 date1 = 0; text = ""; 152 fmt = "mm yy dd."; 153 in = "12199525"; 154 PGTYPESdate_defmt_asc(&date1, fmt, in); 155 text = PGTYPESdate_to_asc(date1); 156 printf("date_defmt_asc9: %s\n", text); 157 PGTYPESchar_free(text); 158 159 date1 = 0; text = ""; 160 fmt = "yyyy fierj mm dd."; 161 in = "19951225"; 162 PGTYPESdate_defmt_asc(&date1, fmt, in); 163 text = PGTYPESdate_to_asc(date1); 164 printf("date_defmt_asc10: %s\n", text); 165 PGTYPESchar_free(text); 166 167 date1 = 0; text = ""; 168 fmt = "mm/dd/yy"; 169 in = "122595"; 170 PGTYPESdate_defmt_asc(&date1, fmt, in); 171 text = PGTYPESdate_to_asc(date1); 172 printf("date_defmt_asc12: %s\n", text); 173 PGTYPESchar_free(text); 174 175 PGTYPEStimestamp_current(&ts1); 176 text = PGTYPEStimestamp_to_asc(ts1); 177 /* can't output this in regression mode */ 178 /* printf("timestamp_current: Now: %s\n", text); */ 179 PGTYPESchar_free(text); 180 181 ts1 = PGTYPEStimestamp_from_asc("96-02-29", NULL); 182 text = PGTYPEStimestamp_to_asc(ts1); 183 printf("timestamp_to_asc1: %s\n", text); 184 PGTYPESchar_free(text); 185 186 ts1 = PGTYPEStimestamp_from_asc("1994-02-11 3:10:35", NULL); 187 text = PGTYPEStimestamp_to_asc(ts1); 188 printf("timestamp_to_asc2: %s\n", text); 189 PGTYPESchar_free(text); 190 191 ts1 = PGTYPEStimestamp_from_asc("1994-02-11 26:10:35", NULL); 192 text = PGTYPEStimestamp_to_asc(ts1); 193 printf("timestamp_to_asc3: %s\n", text); 194 PGTYPESchar_free(text); 195 196/* abc-03:10:35-def-02/11/94-gh */ 197/* 12345678901234567890123456789 */ 198 199 out = (char*) malloc(32); 200 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 31, "abc-%X-def-%x-ghi%%"); 201 printf("timestamp_fmt_asc: %d: %s\n", i, out); 202 free(out); 203 204 fmt = "This is a %m/%d/%y %H-%Ml%Stest"; 205 in = "This is a 4/12/80 3-39l12test"; 206 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 207 text = PGTYPEStimestamp_to_asc(ts1); 208 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 209 PGTYPESchar_free(text); 210 211 fmt = "%a %b %d %H:%M:%S %z %Y"; 212 in = "Tue Jul 22 17:28:44 +0200 2003"; 213 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 214 text = PGTYPEStimestamp_to_asc(ts1); 215 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 216 PGTYPESchar_free(text); 217 218 fmt = "%a %b %d %H:%M:%S %z %Y"; 219 in = "Tue Feb 29 17:28:44 +0200 2000"; 220 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 221 text = PGTYPEStimestamp_to_asc(ts1); 222 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 223 PGTYPESchar_free(text); 224 225 fmt = "%a %b %d %H:%M:%S %z %Y"; 226 in = "Tue Feb 29 17:28:44 +0200 1900"; 227 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 228 text = PGTYPEStimestamp_to_asc(ts1); 229 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 230 PGTYPESchar_free(text); 231 232 fmt = "%a %b %d %H:%M:%S %z %Y"; 233 in = "Tue Feb 29 17:28:44 +0200 1996"; 234 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 235 text = PGTYPEStimestamp_to_asc(ts1); 236 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 237 PGTYPESchar_free(text); 238 239 fmt = "%b %d %H:%M:%S %z %Y"; 240 in = " Jul 31 17:28:44 +0200 1996"; 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 PGTYPESchar_free(text); 245 246 fmt = "%b %d %H:%M:%S %z %Y"; 247 in = " Jul 32 17:28:44 +0200 1996"; 248 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 249 text = PGTYPEStimestamp_to_asc(ts1); 250 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 251 PGTYPESchar_free(text); 252 253 fmt = "%a %b %d %H:%M:%S %z %Y"; 254 in = "Tue Feb 29 17:28:44 +0200 1997"; 255 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 256 text = PGTYPEStimestamp_to_asc(ts1); 257 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 258 PGTYPESchar_free(text); 259 260 fmt = "%"; 261 in = "Tue Jul 22 17:28:44 +0200 2003"; 262 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 263 text = PGTYPEStimestamp_to_asc(ts1); 264 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 265 PGTYPESchar_free(text); 266 267 fmt = "a %"; 268 in = "Tue Jul 22 17:28:44 +0200 2003"; 269 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 270 text = PGTYPEStimestamp_to_asc(ts1); 271 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 272 PGTYPESchar_free(text); 273 274 fmt = "%b, %d %H_%M`%S %z %Y"; 275 in = " Jul, 22 17_28 `44 +0200 2003 "; 276 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 277 text = PGTYPEStimestamp_to_asc(ts1); 278 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 279 PGTYPESchar_free(text); 280 281 fmt = "%a %b %%%d %H:%M:%S %Z %Y"; 282 in = "Tue Jul %22 17:28:44 CEST 2003"; 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 PGTYPESchar_free(text); 287 288 fmt = "%a %b %%%d %H:%M:%S %Z %Y"; 289 in = "Tue Jul %22 17:28:44 CEST 2003"; 290 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 291 text = PGTYPEStimestamp_to_asc(ts1); 292 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 293 PGTYPESchar_free(text); 294 295 fmt = "abc%n %C %B %%%d %H:%M:%S %Z %Y"; 296 in = "abc\n 19 October %22 17:28:44 CEST 2003"; 297 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 298 text = PGTYPEStimestamp_to_asc(ts1); 299 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 300 PGTYPESchar_free(text); 301 302 fmt = "abc%n %C %B %%%d %H:%M:%S %Z %y"; 303 in = "abc\n 18 October %34 17:28:44 CEST 80"; 304 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 305 text = PGTYPEStimestamp_to_asc(ts1); 306 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 307 PGTYPESchar_free(text); 308 309 fmt = ""; 310 in = "abc\n 18 October %34 17:28:44 CEST 80"; 311 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 312 text = PGTYPEStimestamp_to_asc(ts1); 313 printf("timestamp_defmt_asc(%s, %s) = %s, error (should be error!): %d\n", in, fmt, text, i); 314 PGTYPESchar_free(text); 315 316 fmt = NULL; 317 in = "1980-04-12 3:49:44 "; 318 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 319 text = PGTYPEStimestamp_to_asc(ts1); 320 printf("timestamp_defmt_asc(%s, NULL) = %s, error: %d\n", in, text, i); 321 PGTYPESchar_free(text); 322 323 fmt = "%B %d, %Y. Time: %I:%M%p"; 324 in = "July 14, 1988. Time: 9:15am"; 325 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 326 text = PGTYPEStimestamp_to_asc(ts1); 327 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 328 PGTYPESchar_free(text); 329 330 in = "September 6 at 01:30 pm in the year 1983"; 331 fmt = "%B %d at %I:%M %p in the year %Y"; 332 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 333 text = PGTYPEStimestamp_to_asc(ts1); 334 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 335 PGTYPESchar_free(text); 336 337 in = " 1976, July 14. Time: 9:15am"; 338 fmt = "%Y, %B %d. Time: %I:%M %p"; 339 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 340 text = PGTYPEStimestamp_to_asc(ts1); 341 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 342 PGTYPESchar_free(text); 343 344 in = " 1976, July 14. Time: 9:15 am"; 345 fmt = "%Y, %B %d. Time: %I:%M%p"; 346 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 347 text = PGTYPEStimestamp_to_asc(ts1); 348 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 349 PGTYPESchar_free(text); 350 351 in = " 1976, P.M. July 14. Time: 9:15"; 352 fmt = "%Y, %P %B %d. Time: %I:%M"; 353 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 354 text = PGTYPEStimestamp_to_asc(ts1); 355 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 356 PGTYPESchar_free(text); 357 358 in = "1234567890"; 359 fmt = "%s"; 360 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 361 text = PGTYPEStimestamp_to_asc(ts1); 362 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, text, i); 363 PGTYPESchar_free(text); 364 365 out = (char*) malloc(64); 366 fmt = "%a %b %d %H:%M:%S %Y"; 367 in = "Mon Dec 30 17:28:44 2019"; 368 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 369 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt); 370 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i); 371 free(out); 372 373 out = (char*) malloc(64); 374 fmt = "%a %b %d %H:%M:%S %Y"; 375 in = "Mon December 30 17:28:44 2019"; 376 i = PGTYPEStimestamp_defmt_asc(in, fmt, &ts1); 377 i = PGTYPEStimestamp_fmt_asc(&ts1, out, 63, fmt); 378 printf("timestamp_defmt_asc(%s, %s) = %s, error: %d\n", in, fmt, out, i); 379 free(out); 380 381 exec sql rollback; 382 exec sql disconnect; 383 384 return (0); 385} 386