pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
to_char(OF) is broken
4+ messages / 3 participants
[nested] [flat]

* to_char(OF) is broken
@ 2016-03-16 14:02 David Fetter <david@fetter.org>
  2016-03-17 02:23 ` Re: to_char(OF) is broken Thomas Munro <thomas.munro@enterprisedb.com>
  0 siblings, 1 reply; 4+ messages in thread

From: David Fetter @ 2016-03-16 14:02 UTC (permalink / raw)
  To: pgsql-bugs

Folks,

In to_char(), the new-in-9.4 'OF' feature is straight-up broken, to
wit:

set timezone to 'America/Caracas'; select to_char(now(), 'OF');
SET
 to_char 
─────────
 -04:-30
 ^OK ^NOT OK

Interestingly, in git master as of yesterday, it's broken in an
entirely different way:

set timezone to 'America/Caracas'; select to_char(now(), 'OF');
SET
 to_char 
─────────
 -004:30
  ^ What's this extra zero doing here?
(1 row)

Fractional offset time zones should probably be in our regression test
suite for this feature.

Cheers,
David.
-- 
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: to_char(OF) is broken
  2016-03-16 14:02 to_char(OF) is broken David Fetter <david@fetter.org>
@ 2016-03-17 02:23 ` Thomas Munro <thomas.munro@enterprisedb.com>
  2016-03-17 03:14   ` Re: to_char(OF) is broken Thomas Munro <thomas.munro@enterprisedb.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Munro @ 2016-03-17 02:23 UTC (permalink / raw)
  To: David Fetter <david@fetter.org>; +Cc: pgsql-bugs

On Thu, Mar 17, 2016 at 3:02 AM, David Fetter <david@fetter.org> wrote:
> Folks,
>
> In to_char(), the new-in-9.4 'OF' feature is straight-up broken, to
> wit:
>
> set timezone to 'America/Caracas'; select to_char(now(), 'OF');
> SET
>  to_char
> ─────────
>  -04:-30
>  ^OK ^NOT OK
>
> Interestingly, in git master as of yesterday, it's broken in an
> entirely different way:
>
> set timezone to 'America/Caracas'; select to_char(now(), 'OF');
> SET
>  to_char
> ─────────
>  -004:30
>   ^ What's this extra zero doing here?
> (1 row)
>
> Fractional offset time zones should probably be in our regression test
> suite for this feature.

It looks like 2d87eedc made adjustments in accounting for padding
negative numbers in several places, but the case of OF is different
than the other places because it also has a sign for positive numbers,
so no adjustment was necessary there.  See attached.

-- 
Thomas Munro
http://www.enterprisedb.com

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

caracas.patch (application/octet-stream, 2.2K) download diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 2b5622a..0325a511 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -2506,7 +2506,7 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col break; case DCH_OF: INVALID_FOR_INTERVAL; - sprintf(s, "%+0*d", S_FM(n->suffix) ? 0 : (tm->tm_gmtoff >= 0) ? 3 : 4, + sprintf(s, "%+0*d", S_FM(n->suffix) ? 0 : 3, (int) tm->tm_gmtoff / SECS_PER_HOUR); s += strlen(s); if ((int) tm->tm_gmtoff % SECS_PER_HOUR != 0) diff --git a/src/test/regress/expected/timestamptz.out b/src/test/regress/expected/timestamptz.out index fffcaf4..a4d8c42 100644 --- a/src/test/regress/expected/timestamptz.out +++ b/src/test/regress/expected/timestamptz.out @@ -1699,6 +1699,35 @@ SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') | 2001 1 1 1 1 1 1 (66 rows) +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +04:30 +(1 row) + +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + -04:30 +(1 row) + +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +00 +(1 row) + +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +13 +(1 row) + +RESET timezone; CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4 INSERT INTO TIMESTAMPTZ_TST VALUES(1, 'Sat Mar 12 23:58:48 1000 IST'); diff --git a/src/test/regress/sql/timestamptz.sql b/src/test/regress/sql/timestamptz.sql index 03dbc05..16175e7 100644 --- a/src/test/regress/sql/timestamptz.sql +++ b/src/test/regress/sql/timestamptz.sql @@ -248,6 +248,16 @@ SELECT '' AS to_char_10, to_char(d1, 'IYYY IYY IY I IW IDDD ID') SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') FROM TIMESTAMPTZ_TBL; +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); +RESET timezone; + CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: to_char(OF) is broken
  2016-03-16 14:02 to_char(OF) is broken David Fetter <david@fetter.org>
  2016-03-17 02:23 ` Re: to_char(OF) is broken Thomas Munro <thomas.munro@enterprisedb.com>
@ 2016-03-17 03:14   ` Thomas Munro <thomas.munro@enterprisedb.com>
  2016-03-17 19:06     ` Re: to_char(OF) is broken Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Munro @ 2016-03-17 03:14 UTC (permalink / raw)
  To: David Fetter <david@fetter.org>; +Cc: pgsql-bugs

On Thu, Mar 17, 2016 at 3:23 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
> On Thu, Mar 17, 2016 at 3:02 AM, David Fetter <david@fetter.org> wrote:
>> Folks,
>>
>> In to_char(), the new-in-9.4 'OF' feature is straight-up broken, to
>> wit:
>>
>> set timezone to 'America/Caracas'; select to_char(now(), 'OF');
>> SET
>>  to_char
>> ─────────
>>  -04:-30
>>  ^OK ^NOT OK
>>
>> Interestingly, in git master as of yesterday, it's broken in an
>> entirely different way:
>>
>> set timezone to 'America/Caracas'; select to_char(now(), 'OF');
>> SET
>>  to_char
>> ─────────
>>  -004:30
>>   ^ What's this extra zero doing here?
>> (1 row)
>>
>> Fractional offset time zones should probably be in our regression test
>> suite for this feature.
>
> It looks like 2d87eedc made adjustments in accounting for padding
> negative numbers in several places, but the case of OF is different
> than the other places because it also has a sign for positive numbers,
> so no adjustment was necessary there.  See attached.

... and 9.4 has a different bug as you showed (missing abs).  Here's a
patch for 9.4 that uses the same code as master for that switch case
(and adds the same regression test).

9.5 doesn't have either of the bugs, but might as well have the same
regression test just to show that.

-- 
Thomas Munro
http://www.enterprisedb.com

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments: download all (2 files, .tar.gz)

caracas-9.4.patch (application/octet-stream, 2.5K) download diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index b0c2c85..60cae50 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -2503,11 +2503,12 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col break; case DCH_OF: INVALID_FOR_INTERVAL; - sprintf(s, "%+0*ld", S_FM(n->suffix) ? 0 : 3, tm->tm_gmtoff / SECS_PER_HOUR); + sprintf(s, "%+0*d", S_FM(n->suffix) ? 0 : 3, + (int) tm->tm_gmtoff / SECS_PER_HOUR); s += strlen(s); - if (tm->tm_gmtoff % SECS_PER_HOUR != 0) + if ((int) tm->tm_gmtoff % SECS_PER_HOUR != 0) { - sprintf(s, ":%02ld", (tm->tm_gmtoff % SECS_PER_HOUR) / SECS_PER_MINUTE); + sprintf(s, ":%02d", abs((int) tm->tm_gmtoff % SECS_PER_HOUR) / SECS_PER_MINUTE); s += strlen(s); } break; diff --git a/src/test/regress/expected/timestamptz.out b/src/test/regress/expected/timestamptz.out index e5a2130..0a3fd16 100644 --- a/src/test/regress/expected/timestamptz.out +++ b/src/test/regress/expected/timestamptz.out @@ -1675,6 +1675,35 @@ SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') | 2001 1 1 1 1 1 1 (66 rows) +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +04:30 +(1 row) + +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + -04:30 +(1 row) + +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +00 +(1 row) + +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +13 +(1 row) + +RESET timezone; CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4 INSERT INTO TIMESTAMPTZ_TST VALUES(1, 'Sat Mar 12 23:58:48 1000 IST'); diff --git a/src/test/regress/sql/timestamptz.sql b/src/test/regress/sql/timestamptz.sql index f4b455e..dae7235 100644 --- a/src/test/regress/sql/timestamptz.sql +++ b/src/test/regress/sql/timestamptz.sql @@ -241,6 +241,16 @@ SELECT '' AS to_char_10, to_char(d1, 'IYYY IYY IY I IW IDDD ID') SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') FROM TIMESTAMPTZ_TBL; +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); +RESET timezone; + CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4
caracas-9.5.patch (application/octet-stream, 1.6K) download diff --git a/src/test/regress/expected/timestamptz.out b/src/test/regress/expected/timestamptz.out index e5a2130..0a3fd16 100644 --- a/src/test/regress/expected/timestamptz.out +++ b/src/test/regress/expected/timestamptz.out @@ -1675,6 +1675,35 @@ SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') | 2001 1 1 1 1 1 1 (66 rows) +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +04:30 +(1 row) + +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); + to_char +--------- + -04:30 +(1 row) + +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +00 +(1 row) + +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); + to_char +--------- + +13 +(1 row) + +RESET timezone; CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4 INSERT INTO TIMESTAMPTZ_TST VALUES(1, 'Sat Mar 12 23:58:48 1000 IST'); diff --git a/src/test/regress/sql/timestamptz.sql b/src/test/regress/sql/timestamptz.sql index f4b455e..dae7235 100644 --- a/src/test/regress/sql/timestamptz.sql +++ b/src/test/regress/sql/timestamptz.sql @@ -241,6 +241,16 @@ SELECT '' AS to_char_10, to_char(d1, 'IYYY IYY IY I IW IDDD ID') SELECT '' AS to_char_11, to_char(d1, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') FROM TIMESTAMPTZ_TBL; +SET timezone = '-04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '04:30'; +SELECT to_char(now(), 'OF'); +SET timezone = '00:00'; +SELECT to_char(now(), 'OF'); +SET timezone = '-13:00'; +SELECT to_char(now(), 'OF'); +RESET timezone; + CREATE TABLE TIMESTAMPTZ_TST (a int , b timestamptz); -- Test year field value with len > 4
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: to_char(OF) is broken
  2016-03-16 14:02 to_char(OF) is broken David Fetter <david@fetter.org>
  2016-03-17 02:23 ` Re: to_char(OF) is broken Thomas Munro <thomas.munro@enterprisedb.com>
  2016-03-17 03:14   ` Re: to_char(OF) is broken Thomas Munro <thomas.munro@enterprisedb.com>
@ 2016-03-17 19:06     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-03-17 19:06 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@enterprisedb.com>; +Cc: David Fetter <david@fetter.org>; pgsql-bugs

Thomas Munro <thomas.munro@enterprisedb.com> writes:
> On Thu, Mar 17, 2016 at 3:23 PM, Thomas Munro
> <thomas.munro@enterprisedb.com> wrote:
>> On Thu, Mar 17, 2016 at 3:02 AM, David Fetter <david@fetter.org> wrote:
>>> Fractional offset time zones should probably be in our regression test
>>> suite for this feature.

>> It looks like 2d87eedc made adjustments in accounting for padding
>> negative numbers in several places, but the case of OF is different
>> than the other places because it also has a sign for positive numbers,
>> so no adjustment was necessary there.  See attached.

> ... and 9.4 has a different bug as you showed (missing abs).  Here's a
> patch for 9.4 that uses the same code as master for that switch case
> (and adds the same regression test).

> 9.5 doesn't have either of the bugs, but might as well have the same
> regression test just to show that.

This is still not quite right, as it would do the wrong thing for a
zone '-00:30'.  I'm not sure that such a zone exists in reality,
but we might as well get it right.

Will fix and commit.  Thanks for looking at this!

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2016-03-17 19:06 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-03-16 14:02 to_char(OF) is broken David Fetter <david@fetter.org>
2016-03-17 02:23 ` Thomas Munro <thomas.munro@enterprisedb.com>
2016-03-17 03:14   ` Thomas Munro <thomas.munro@enterprisedb.com>
2016-03-17 19:06     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox