Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ba6fdf

Browse files
committed
Support TZ and OF format codes in to_timestamp().
Formerly, these were only supported in to_char(), but there seems little reason for that restriction. We should at least have enough support to permit round-tripping the output of to_char(). In that spirit, TZ accepts either zone abbreviations or numeric (HH or HH:MM) offsets, which are the cases that to_char() can output. In an ideal world we'd make it take full zone names too, but that seems like it'd introduce an unreasonable amount of ambiguity, since the rules for POSIX-spec zone names are so lax. OF is a subset of this, accepting only HH or HH:MM. One small benefit of this improvement is that we can simplify jsonpath's executeDateTimeMethod function, which no longer needs to consider the HH and HH:MM cases separately. Moreover, letting it accept zone abbreviations means it will accept "Z" to mean UTC, which is emitted by JSON.stringify() for example. Patch by me, reviewed by Aleksander Alekseev and Daniel Gustafsson Discussion: https://postgr.es/m/1681086.1686673242@sss.pgh.pa.us
1 parent 06a66d8 commit 8ba6fdf

File tree

9 files changed

+318
-74
lines changed

9 files changed

+318
-74
lines changed

doc/src/sgml/func.sgml

+4-6
Original file line numberDiff line numberDiff line change
@@ -8131,13 +8131,11 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
81318131
</row>
81328132
<row>
81338133
<entry><literal>TZ</literal></entry>
8134-
<entry>upper case time-zone abbreviation
8135-
(only supported in <function>to_char</function>)</entry>
8134+
<entry>upper case time-zone abbreviation</entry>
81368135
</row>
81378136
<row>
81388137
<entry><literal>tz</literal></entry>
8139-
<entry>lower case time-zone abbreviation
8140-
(only supported in <function>to_char</function>)</entry>
8138+
<entry>lower case time-zone abbreviation</entry>
81418139
</row>
81428140
<row>
81438141
<entry><literal>TZH</literal></entry>
@@ -8149,8 +8147,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
81498147
</row>
81508148
<row>
81518149
<entry><literal>OF</literal></entry>
8152-
<entry>time-zone offset from UTC
8153-
(only supported in <function>to_char</function>)</entry>
8150+
<entry>time-zone offset from UTC (<replaceable>HH</replaceable>
8151+
or <replaceable>HH</replaceable><literal>:</literal><replaceable>MM</replaceable>)</entry>
81548152
</row>
81558153
</tbody>
81568154
</tgroup>

src/backend/utils/adt/datetime.c

+76
Original file line numberDiff line numberDiff line change
@@ -3246,6 +3246,82 @@ DecodeTimezoneNameToTz(const char *tzname)
32463246
return result;
32473247
}
32483248

3249+
/* DecodeTimezoneAbbrevPrefix()
3250+
* Interpret prefix of string as a timezone abbreviation, if possible.
3251+
*
3252+
* This has roughly the same functionality as DecodeTimezoneAbbrev(),
3253+
* but the API is adapted to the needs of formatting.c. Notably,
3254+
* we will match the longest possible prefix of the given string
3255+
* rather than insisting on a complete match, and downcasing is applied
3256+
* here rather than in the caller.
3257+
*
3258+
* Returns the length of the timezone abbreviation, or -1 if not recognized.
3259+
* On success, sets *offset to the GMT offset for the abbreviation if it
3260+
* is a fixed-offset abbreviation, or sets *tz to the pg_tz struct for
3261+
* a dynamic abbreviation.
3262+
*/
3263+
int
3264+
DecodeTimezoneAbbrevPrefix(const char *str, int *offset, pg_tz **tz)
3265+
{
3266+
char lowtoken[TOKMAXLEN + 1];
3267+
int len;
3268+
3269+
*offset = 0; /* avoid uninitialized vars on failure */
3270+
*tz = NULL;
3271+
3272+
if (!zoneabbrevtbl)
3273+
return -1; /* no abbrevs known, so fail immediately */
3274+
3275+
/* Downcase as much of the string as we could need */
3276+
for (len = 0; len < TOKMAXLEN; len++)
3277+
{
3278+
if (*str == '\0' || !isalpha((unsigned char) *str))
3279+
break;
3280+
lowtoken[len] = pg_tolower((unsigned char) *str++);
3281+
}
3282+
lowtoken[len] = '\0';
3283+
3284+
/*
3285+
* We could avoid doing repeated binary searches if we cared to duplicate
3286+
* datebsearch here, but it's not clear that such an optimization would be
3287+
* worth the trouble. In common cases there's probably not anything after
3288+
* the zone abbrev anyway. So just search with successively truncated
3289+
* strings.
3290+
*/
3291+
while (len > 0)
3292+
{
3293+
const datetkn *tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3294+
zoneabbrevtbl->numabbrevs);
3295+
3296+
if (tp != NULL)
3297+
{
3298+
if (tp->type == DYNTZ)
3299+
{
3300+
DateTimeErrorExtra extra;
3301+
pg_tz *tzp = FetchDynamicTimeZone(zoneabbrevtbl, tp,
3302+
&extra);
3303+
3304+
if (tzp != NULL)
3305+
{
3306+
/* Caller must resolve the abbrev's current meaning */
3307+
*tz = tzp;
3308+
return len;
3309+
}
3310+
}
3311+
else
3312+
{
3313+
/* Fixed-offset zone abbrev, so it's easy */
3314+
*offset = tp->value;
3315+
return len;
3316+
}
3317+
}
3318+
lowtoken[--len] = '\0';
3319+
}
3320+
3321+
/* Did not find a match */
3322+
return -1;
3323+
}
3324+
32493325

32503326
/* ClearPgItmIn
32513327
*

0 commit comments

Comments
 (0)