Skip to content

Commit 7408c5d

Browse files
committed
Add timezone offset output option to to_char()
Add ability for to_char() to output the timezone's UTC offset (OF). We already have the ability to return the timezone abbeviation (TZ/tz). Per request from Andrew Dunstan
1 parent 6697aa2 commit 7408c5d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

doc/src/sgml/func.sgml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5645,6 +5645,10 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
56455645
<entry><literal>tz</literal></entry>
56465646
<entry>lower case time-zone name</entry>
56475647
</row>
5648+
<row>
5649+
<entry><literal>OF</literal></entry>
5650+
<entry>time-zone offset</entry>
5651+
</row>
56485652
</tbody>
56495653
</tgroup>
56505654
</table>

src/backend/utils/adt/formatting.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ typedef enum
600600
DCH_MS,
601601
DCH_Month,
602602
DCH_Mon,
603+
DCH_OF,
603604
DCH_P_M,
604605
DCH_PM,
605606
DCH_Q,
@@ -746,6 +747,7 @@ static const KeyWord DCH_keywords[] = {
746747
{"MS", 2, DCH_MS, TRUE, FROM_CHAR_DATE_NONE},
747748
{"Month", 5, DCH_Month, FALSE, FROM_CHAR_DATE_GREGORIAN},
748749
{"Mon", 3, DCH_Mon, FALSE, FROM_CHAR_DATE_GREGORIAN},
750+
{"OF", 2, DCH_OF, FALSE, FROM_CHAR_DATE_NONE}, /* O */
749751
{"P.M.", 4, DCH_P_M, FALSE, FROM_CHAR_DATE_NONE}, /* P */
750752
{"PM", 2, DCH_PM, FALSE, FROM_CHAR_DATE_NONE},
751753
{"Q", 1, DCH_Q, TRUE, FROM_CHAR_DATE_NONE}, /* Q */
@@ -874,7 +876,7 @@ static const int DCH_index[KeyWord_INDEX_SIZE] = {
874876
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
875877
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
876878
-1, -1, -1, -1, -1, DCH_A_D, DCH_B_C, DCH_CC, DCH_DAY, -1,
877-
DCH_FX, -1, DCH_HH24, DCH_IDDD, DCH_J, -1, -1, DCH_MI, -1, -1,
879+
DCH_FX, -1, DCH_HH24, DCH_IDDD, DCH_J, -1, -1, DCH_MI, -1, DCH_OF,
878880
DCH_P_M, DCH_Q, DCH_RM, DCH_SSSS, DCH_TZ, DCH_US, -1, DCH_WW, -1, DCH_Y_YYY,
879881
-1, -1, -1, -1, -1, -1, -1, DCH_a_d, DCH_b_c, DCH_cc,
880882
DCH_day, -1, DCH_fx, -1, DCH_hh24, DCH_iddd, DCH_j, -1, -1, DCH_mi,
@@ -2502,6 +2504,16 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
25022504
s += strlen(s);
25032505
}
25042506
break;
2507+
case DCH_OF:
2508+
INVALID_FOR_INTERVAL;
2509+
sprintf(s, "%+0*ld", S_FM(n->suffix) ? 0 : 3, tm->tm_gmtoff / SECS_PER_HOUR);
2510+
s += strlen(s);
2511+
if (tm->tm_gmtoff % SECS_PER_HOUR != 0)
2512+
{
2513+
sprintf(s, ":%02ld", (tm->tm_gmtoff % SECS_PER_HOUR) / SECS_PER_MINUTE);
2514+
s += strlen(s);
2515+
}
2516+
break;
25052517
case DCH_A_D:
25062518
case DCH_B_C:
25072519
INVALID_FOR_INTERVAL;
@@ -2915,9 +2927,10 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
29152927
break;
29162928
case DCH_tz:
29172929
case DCH_TZ:
2930+
case DCH_OF:
29182931
ereport(ERROR,
29192932
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2920-
errmsg("\"TZ\"/\"tz\" format patterns are not supported in to_date")));
2933+
errmsg("\"TZ\"/\"tz\"/\"OF\" format patterns are not supported in to_date")));
29212934
case DCH_A_D:
29222935
case DCH_B_C:
29232936
case DCH_a_d:

0 commit comments

Comments
 (0)