|
29 | 29 | #include <funcapi.h> |
30 | 30 |
|
31 | 31 |
|
| 32 | +#define NUMBER_OF_TIME_ZONES 18 |
| 33 | + |
| 34 | + |
32 | 35 | PG_FUNCTION_INFO_V1(ora_fromtz); |
33 | 36 | PG_FUNCTION_INFO_V1(ora_systimestamp); |
34 | 37 | PG_FUNCTION_INFO_V1(ora_sys_extract_utc); |
35 | 38 | PG_FUNCTION_INFO_V1(ora_days_between); |
36 | 39 | PG_FUNCTION_INFO_V1(ora_days_between_tmtz); |
| 40 | +PG_FUNCTION_INFO_V1(new_time); |
37 | 41 |
|
38 | 42 |
|
39 | 43 |
|
@@ -353,3 +357,152 @@ Datum ora_days_between_tmtz(PG_FUNCTION_ARGS) |
353 | 357 |
|
354 | 358 | } |
355 | 359 |
|
| 360 | + |
| 361 | +/******************************************************************** |
| 362 | + * |
| 363 | + * FUnction:oratimestamptz2timestamp |
| 364 | + * |
| 365 | + * Description:recover timestamp with zone to timestamp without zone |
| 366 | + * |
| 367 | + * Return:timestamp with out zone |
| 368 | + * |
| 369 | + * Note:oracle compatible timestamp transfer |
| 370 | + * |
| 371 | + ********************************************************************/ |
| 372 | +static Timestamp |
| 373 | +oratimestamptz2timestamp(TimestampTz timestamp) |
| 374 | +{ |
| 375 | + Timestamp result; |
| 376 | + struct pg_tm tt, |
| 377 | + *tm = &tt; |
| 378 | + fsec_t fsec; |
| 379 | + int tz; |
| 380 | + |
| 381 | + if (TIMESTAMP_NOT_FINITE(timestamp)) |
| 382 | + result = timestamp; |
| 383 | + else |
| 384 | + { |
| 385 | + if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) |
| 386 | + ereport(ERROR, |
| 387 | + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 388 | + errmsg("timestamp out of range"))); |
| 389 | + if (tm2timestamp(tm, fsec, NULL, &result) != 0) |
| 390 | + ereport(ERROR, |
| 391 | + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 392 | + errmsg("timestamp out of range"))); |
| 393 | + } |
| 394 | + return result; |
| 395 | +} |
| 396 | + |
| 397 | + |
| 398 | +/******************************************************************** |
| 399 | + * |
| 400 | + * FUnction:new_time |
| 401 | + * |
| 402 | + * Description:convert the time in the specified time zone to another time zone |
| 403 | + * |
| 404 | + * Return:a new timestamp |
| 405 | + * |
| 406 | + * Note:there is olney support timestamp without time zone |
| 407 | + * |
| 408 | + ********************************************************************/ |
| 409 | +Datum |
| 410 | +new_time(PG_FUNCTION_ARGS) |
| 411 | +{ |
| 412 | + TimestampTz timestamptz = PG_GETARG_TIMESTAMP(0); |
| 413 | + text *arg1 = PG_GETARG_TEXT_P(1); |
| 414 | + text *arg2 = PG_GETARG_TEXT_P(2); |
| 415 | + int tz, i, zone1 = 0, zone2 = 0, tmp1 = 0, tmp2 = 0, day; |
| 416 | + struct pg_tm tt, *tm = &tt; |
| 417 | + fsec_t fsec; |
| 418 | + const char *tzn = NULL; |
| 419 | + char *pre_zone = NULL, *post_zone = NULL ; |
| 420 | + char *zone[NUMBER_OF_TIME_ZONES] = {"ast", "adt", "bst", "bdt", "cst", "cdt", "est", "edt", "gmt", "hst", "hdt", "mst", "mdt", "nst", "pst", "pdt", "yst", "ydt"}; |
| 421 | + int zone_num[NUMBER_OF_TIME_ZONES] = { -4, -3, -11, -10, -6, -5, -5, -4, 0, -10, -9, -7, -6, -3, -8, -7, -9, -8}; |
| 422 | + Timestamp result; |
| 423 | + |
| 424 | + Timestamp timestamp; |
| 425 | + |
| 426 | + timestamp = oratimestamptz2timestamp(timestamptz); |
| 427 | + |
| 428 | + /*ignore input parameter case*/ |
| 429 | + pre_zone = str_tolower(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1), PG_GET_COLLATION()); |
| 430 | + post_zone = str_tolower(VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2), PG_GET_COLLATION()); |
| 431 | + |
| 432 | + //timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, pg_tzset("GMT")); |
| 433 | + timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL); |
| 434 | + |
| 435 | + for (i = 0; i < NUMBER_OF_TIME_ZONES; i++) |
| 436 | + { |
| 437 | + if (!strcmp(zone[i], pre_zone)) |
| 438 | + { |
| 439 | + zone1 = zone_num[i]; |
| 440 | + tmp1 = 1; |
| 441 | + } |
| 442 | + |
| 443 | + if (!strcmp(zone[i], post_zone)) |
| 444 | + { |
| 445 | + zone2 = zone_num[i]; |
| 446 | + tmp2 = 1; |
| 447 | + } |
| 448 | + } |
| 449 | + |
| 450 | + if ((tmp1 == 0) || (tmp2 == 0)) |
| 451 | + ereport(ERROR, |
| 452 | + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 453 | + errmsg("illegal timezone!"))); |
| 454 | + |
| 455 | + /*transfer date into Julian day*/ |
| 456 | + day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday); |
| 457 | + tm->tm_hour = tm->tm_hour + zone2 - zone1; |
| 458 | + |
| 459 | + /* if the zone is nst, there need to deal minutes, becaus nst is 3.5, is not 3 */ |
| 460 | + if (0 == strcmp("nst", post_zone)) |
| 461 | + { |
| 462 | + if (tm->tm_min > 29) |
| 463 | + { |
| 464 | + tm->tm_min = tm->tm_min - 30; |
| 465 | + } |
| 466 | + else |
| 467 | + { |
| 468 | + tm->tm_min = tm->tm_min + 30; |
| 469 | + tm->tm_hour = tm->tm_hour - 1; |
| 470 | + } |
| 471 | + } |
| 472 | + |
| 473 | + if(0 == strcmp("nst",pre_zone)) |
| 474 | + { |
| 475 | + if (tm->tm_min < 29) |
| 476 | + { |
| 477 | + tm->tm_min = tm->tm_min + 30; |
| 478 | + } |
| 479 | + else |
| 480 | + { |
| 481 | + tm->tm_min = tm->tm_min - 30; |
| 482 | + tm->tm_hour = tm->tm_hour + 1; |
| 483 | + } |
| 484 | + } |
| 485 | + |
| 486 | + /*if hour bigger than 24, or small than 0, need to change the day*/ |
| 487 | + if (tm->tm_hour >= 24) |
| 488 | + { |
| 489 | + tm->tm_hour -= 24; |
| 490 | + day += 1; |
| 491 | + } |
| 492 | + else if (tm->tm_hour < 0) |
| 493 | + { |
| 494 | + tm->tm_hour += 24; |
| 495 | + day -= 1; |
| 496 | + } |
| 497 | + |
| 498 | + /*transfer Julian day into date format*/ |
| 499 | + j2date(day, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); |
| 500 | + fsec = 0; |
| 501 | + if (tm2timestamp(tm, fsec, &tz, &result) != 0) |
| 502 | + ereport(ERROR, |
| 503 | + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 504 | + errmsg("timestamp out of range"))); |
| 505 | + |
| 506 | + PG_RETURN_TIMESTAMP(result); |
| 507 | +} |
| 508 | + |
0 commit comments