|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.51 2000/10/29 13:17:33 petere Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.52 2000/11/11 19:55:19 thomas Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -339,6 +339,61 @@ abstime_date(PG_FUNCTION_ARGS)
|
339 | 339 | }
|
340 | 340 |
|
341 | 341 |
|
| 342 | +/* date_text() |
| 343 | + * Convert date to text data type. |
| 344 | + */ |
| 345 | +Datum |
| 346 | +date_text(PG_FUNCTION_ARGS) |
| 347 | +{ |
| 348 | + /* Input is a Date, but may as well leave it in Datum form */ |
| 349 | + Datum date = PG_GETARG_DATUM(0); |
| 350 | + text *result; |
| 351 | + char *str; |
| 352 | + int len; |
| 353 | + |
| 354 | + str = DatumGetCString(DirectFunctionCall1(date_out, date)); |
| 355 | + |
| 356 | + len = (strlen(str) + VARHDRSZ); |
| 357 | + |
| 358 | + result = palloc(len); |
| 359 | + |
| 360 | + VARATT_SIZEP(result) = len; |
| 361 | + memmove(VARDATA(result), str, (len - VARHDRSZ)); |
| 362 | + |
| 363 | + pfree(str); |
| 364 | + |
| 365 | + PG_RETURN_TEXT_P(result); |
| 366 | +} |
| 367 | + |
| 368 | + |
| 369 | +/* text_date() |
| 370 | + * Convert text string to date. |
| 371 | + * Text type is not null terminated, so use temporary string |
| 372 | + * then call the standard input routine. |
| 373 | + */ |
| 374 | +Datum |
| 375 | +text_date(PG_FUNCTION_ARGS) |
| 376 | +{ |
| 377 | + text *str = PG_GETARG_TEXT_P(0); |
| 378 | + int i; |
| 379 | + char *sp, |
| 380 | + *dp, |
| 381 | + dstr[MAXDATELEN + 1]; |
| 382 | + |
| 383 | + if (VARSIZE(str) - VARHDRSZ > MAXDATELEN) |
| 384 | + elog(ERROR, "Bad date external representation (too long)"); |
| 385 | + |
| 386 | + sp = VARDATA(str); |
| 387 | + dp = dstr; |
| 388 | + for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++) |
| 389 | + *dp++ = *sp++; |
| 390 | + *dp = '\0'; |
| 391 | + |
| 392 | + return DirectFunctionCall1(date_in, |
| 393 | + CStringGetDatum(dstr)); |
| 394 | +} |
| 395 | + |
| 396 | + |
342 | 397 | /*****************************************************************************
|
343 | 398 | * Time ADT
|
344 | 399 | *****************************************************************************/
|
@@ -576,6 +631,61 @@ time_interval(PG_FUNCTION_ARGS)
|
576 | 631 | }
|
577 | 632 |
|
578 | 633 |
|
| 634 | +/* time_text() |
| 635 | + * Convert time to text data type. |
| 636 | + */ |
| 637 | +Datum |
| 638 | +time_text(PG_FUNCTION_ARGS) |
| 639 | +{ |
| 640 | + /* Input is a Time, but may as well leave it in Datum form */ |
| 641 | + Datum time = PG_GETARG_DATUM(0); |
| 642 | + text *result; |
| 643 | + char *str; |
| 644 | + int len; |
| 645 | + |
| 646 | + str = DatumGetCString(DirectFunctionCall1(time_out, time)); |
| 647 | + |
| 648 | + len = (strlen(str) + VARHDRSZ); |
| 649 | + |
| 650 | + result = palloc(len); |
| 651 | + |
| 652 | + VARATT_SIZEP(result) = len; |
| 653 | + memmove(VARDATA(result), str, (len - VARHDRSZ)); |
| 654 | + |
| 655 | + pfree(str); |
| 656 | + |
| 657 | + PG_RETURN_TEXT_P(result); |
| 658 | +} |
| 659 | + |
| 660 | + |
| 661 | +/* text_time() |
| 662 | + * Convert text string to time. |
| 663 | + * Text type is not null terminated, so use temporary string |
| 664 | + * then call the standard input routine. |
| 665 | + */ |
| 666 | +Datum |
| 667 | +text_time(PG_FUNCTION_ARGS) |
| 668 | +{ |
| 669 | + text *str = PG_GETARG_TEXT_P(0); |
| 670 | + int i; |
| 671 | + char *sp, |
| 672 | + *dp, |
| 673 | + dstr[MAXDATELEN + 1]; |
| 674 | + |
| 675 | + if (VARSIZE(str) - VARHDRSZ > MAXDATELEN) |
| 676 | + elog(ERROR, "Bad time external representation (too long)"); |
| 677 | + |
| 678 | + sp = VARDATA(str); |
| 679 | + dp = dstr; |
| 680 | + for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++) |
| 681 | + *dp++ = *sp++; |
| 682 | + *dp = '\0'; |
| 683 | + |
| 684 | + return DirectFunctionCall1(time_in, |
| 685 | + CStringGetDatum(dstr)); |
| 686 | +} |
| 687 | + |
| 688 | + |
579 | 689 | /*****************************************************************************
|
580 | 690 | * Time With Time Zone ADT
|
581 | 691 | *****************************************************************************/
|
@@ -851,3 +961,58 @@ datetimetz_timestamp(PG_FUNCTION_ARGS)
|
851 | 961 |
|
852 | 962 | PG_RETURN_TIMESTAMP(result);
|
853 | 963 | }
|
| 964 | + |
| 965 | + |
| 966 | +/* timetz_text() |
| 967 | + * Convert timetz to text data type. |
| 968 | + */ |
| 969 | +Datum |
| 970 | +timetz_text(PG_FUNCTION_ARGS) |
| 971 | +{ |
| 972 | + /* Input is a Timetz, but may as well leave it in Datum form */ |
| 973 | + Datum timetz = PG_GETARG_DATUM(0); |
| 974 | + text *result; |
| 975 | + char *str; |
| 976 | + int len; |
| 977 | + |
| 978 | + str = DatumGetCString(DirectFunctionCall1(timetz_out, timetz)); |
| 979 | + |
| 980 | + len = (strlen(str) + VARHDRSZ); |
| 981 | + |
| 982 | + result = palloc(len); |
| 983 | + |
| 984 | + VARATT_SIZEP(result) = len; |
| 985 | + memmove(VARDATA(result), str, (len - VARHDRSZ)); |
| 986 | + |
| 987 | + pfree(str); |
| 988 | + |
| 989 | + PG_RETURN_TEXT_P(result); |
| 990 | +} |
| 991 | + |
| 992 | + |
| 993 | +/* text_timetz() |
| 994 | + * Convert text string to timetz. |
| 995 | + * Text type is not null terminated, so use temporary string |
| 996 | + * then call the standard input routine. |
| 997 | + */ |
| 998 | +Datum |
| 999 | +text_timetz(PG_FUNCTION_ARGS) |
| 1000 | +{ |
| 1001 | + text *str = PG_GETARG_TEXT_P(0); |
| 1002 | + int i; |
| 1003 | + char *sp, |
| 1004 | + *dp, |
| 1005 | + dstr[MAXDATELEN + 1]; |
| 1006 | + |
| 1007 | + if (VARSIZE(str) - VARHDRSZ > MAXDATELEN) |
| 1008 | + elog(ERROR, "Bad timetz external representation (too long)"); |
| 1009 | + |
| 1010 | + sp = VARDATA(str); |
| 1011 | + dp = dstr; |
| 1012 | + for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++) |
| 1013 | + *dp++ = *sp++; |
| 1014 | + *dp = '\0'; |
| 1015 | + |
| 1016 | + return DirectFunctionCall1(timetz_in, |
| 1017 | + CStringGetDatum(dstr)); |
| 1018 | +} |
0 commit comments