pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #15698: to_char doesn't return expected value with negative INTERVAL
3+ messages / 3 participants
[nested] [flat]
* BUG #15698: to_char doesn't return expected value with negative INTERVAL
@ 2019-03-16 20:11 PG Bug reporting form <noreply@postgresql.org>
2019-04-09 17:23 ` Re: BUG #15698: to_char doesn't return expected value with negative INTERVAL Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 3+ messages in thread
From: PG Bug reporting form @ 2019-03-16 20:11 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: s.celles@gmail.com
The following bug has been logged on the website:
Bug reference: 15698
Logged by: Sébastien Celles
Email address: s.celles@gmail.com
PostgreSQL version: 10.5
Operating system: Windows 10
Description:
Hello,
This is my first bug report here (despite I'm using PostgreSQL since many
years !)
I don't know if it's a bug... but the following query
SELECT to_char(-(3 * 60 * 60 * 1000 + 7*60 * 1000 + 12345) * INTERVAL '1
millisecond', 'HH24:MI:SS.MS') as interv;
doesn't return the result I was expecting.
It returns:
'-03:-07:-12.-345'
I was expecting
'-03:07:12.345'
If it's not a bug (but a feature ;-) )... is there a way to return result as
I was expecting.
Kind regards
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15698: to_char doesn't return expected value with negative INTERVAL
2019-03-16 20:11 BUG #15698: to_char doesn't return expected value with negative INTERVAL PG Bug reporting form <noreply@postgresql.org>
@ 2019-04-09 17:23 ` Bruce Momjian <bruce@momjian.us>
2019-04-09 18:21 ` Re: BUG #15698: to_char doesn't return expected value with negative INTERVAL s.celles@gmail.com <s.celles@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Bruce Momjian @ 2019-04-09 17:23 UTC (permalink / raw)
To: s.celles@gmail.com; pgsql-bugs@lists.postgresql.org
On Sat, Mar 16, 2019 at 08:11:19PM +0000, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference: 15698
> Logged by: Sébastien Celles
> Email address: s.celles@gmail.com
> PostgreSQL version: 10.5
> Operating system: Windows 10
> Description:
>
> Hello,
>
> This is my first bug report here (despite I'm using PostgreSQL since many
> years !)
>
> I don't know if it's a bug... but the following query
>
> SELECT to_char(-(3 * 60 * 60 * 1000 + 7*60 * 1000 + 12345) * INTERVAL '1
> millisecond', 'HH24:MI:SS.MS') as interv;
>
> doesn't return the result I was expecting.
>
> It returns:
>
> '-03:-07:-12.-345'
>
> I was expecting
>
> '-03:07:12.345'
>
> If it's not a bug (but a feature ;-) )... is there a way to return result as
> I was expecting.
I am sorry for my delay in replying.
Yes, I agree the current output looks odd. You would think that
to_char() could just roll the sign up to a single mention, but intervals
store values in three parts:
typedef struct
{
TimeOffset time; /* all time units other than days, months and
* years */
int32 day; /* days, after time for alignment */
int32 month; /* months and years, after time for alignment */
} Interval;
Those parts can have different signs. Here is an example:
SELECT to_char('-1 month 2 days -3 hours'::interval, 'MM DD HH') AS interv;
interv
------------
-01 02 -03
Therefore, we output _all_ units with separate signs. (I don't know how
I would pass a single negative value into to_char() for
timestamp/timestamptz.)
The values only cross the three unit boundaries when we call "justify"
functions:
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+------------------+------------------+---------------------+------
pg_catalog | justify_days | interval | interval | func
pg_catalog | justify_hours | interval | interval | func
pg_catalog | justify_interval | interval | interval | func
Here is a psql query that optionally outputs the negative sign of your
calculation, and then passes the absolute value to to_char():
\set var -(3 * 60 * 60 * 1000 + 7*60 * 1000 + 12345)
SELECT CASE WHEN :var < 0 THEN '-' END ||
to_char(abs(:var) * INTERVAL '1 millisecond',
'HH24:MI:SS.MS') as interv;
interv
---------------
-03:07:12.345
I hope this helps.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ As you are, so once was I. As I am, so you will be. +
+ Ancient Roman grave inscription +
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15698: to_char doesn't return expected value with negative INTERVAL
2019-03-16 20:11 BUG #15698: to_char doesn't return expected value with negative INTERVAL PG Bug reporting form <noreply@postgresql.org>
2019-04-09 17:23 ` Re: BUG #15698: to_char doesn't return expected value with negative INTERVAL Bruce Momjian <bruce@momjian.us>
@ 2019-04-09 18:21 ` s.celles@gmail.com <s.celles@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: s.celles@gmail.com @ 2019-04-09 18:21 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: pgsql-bugs@lists.postgresql.org
Thanks Bruce for your answer.
It helps me a lot and should fix my use case.
Le mar. 9 avr. 2019 à 19:23, Bruce Momjian <bruce@momjian.us> a écrit :
> On Sat, Mar 16, 2019 at 08:11:19PM +0000, PG Bug reporting form wrote:
> > The following bug has been logged on the website:
> >
> > Bug reference: 15698
> > Logged by: Sébastien Celles
> > Email address: s.celles@gmail.com
> > PostgreSQL version: 10.5
> > Operating system: Windows 10
> > Description:
> >
> > Hello,
> >
> > This is my first bug report here (despite I'm using PostgreSQL since many
> > years !)
> >
> > I don't know if it's a bug... but the following query
> >
> > SELECT to_char(-(3 * 60 * 60 * 1000 + 7*60 * 1000 + 12345) *
> INTERVAL '1
> > millisecond', 'HH24:MI:SS.MS') as interv;
> >
> > doesn't return the result I was expecting.
> >
> > It returns:
> >
> > '-03:-07:-12.-345'
> >
> > I was expecting
> >
> > '-03:07:12.345'
> >
> > If it's not a bug (but a feature ;-) )... is there a way to return
> result as
> > I was expecting.
>
> I am sorry for my delay in replying.
>
> Yes, I agree the current output looks odd. You would think that
> to_char() could just roll the sign up to a single mention, but intervals
> store values in three parts:
>
> typedef struct
> {
> TimeOffset time; /* all time units other than days,
> months and
> * years */
> int32 day; /* days, after time for alignment
> */
> int32 month; /* months and years, after time
> for alignment */
> } Interval;
>
> Those parts can have different signs. Here is an example:
>
> SELECT to_char('-1 month 2 days -3 hours'::interval, 'MM DD HH')
> AS interv;
> interv
> ------------
> -01 02 -03
>
> Therefore, we output _all_ units with separate signs. (I don't know how
> I would pass a single negative value into to_char() for
> timestamp/timestamptz.)
>
> The values only cross the three unit boundaries when we call "justify"
> functions:
>
> List of functions
> Schema | Name | Result data type | Argument data
> types | Type
>
> ------------+------------------+------------------+---------------------+------
> pg_catalog | justify_days | interval | interval
> | func
> pg_catalog | justify_hours | interval | interval
> | func
> pg_catalog | justify_interval | interval | interval
> | func
>
> Here is a psql query that optionally outputs the negative sign of your
> calculation, and then passes the absolute value to to_char():
>
> \set var -(3 * 60 * 60 * 1000 + 7*60 * 1000 + 12345)
>
> SELECT CASE WHEN :var < 0 THEN '-' END ||
> to_char(abs(:var) * INTERVAL '1 millisecond',
> 'HH24:MI:SS.MS') as interv;
> interv
> ---------------
> -03:07:12.345
>
> I hope this helps.
>
> --
> Bruce Momjian <bruce@momjian.us> http://momjian.us
> EnterpriseDB http://enterprisedb.com
>
> + As you are, so once was I. As I am, so you will be. +
> + Ancient Roman grave inscription +
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2019-04-09 18:21 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-03-16 20:11 BUG #15698: to_char doesn't return expected value with negative INTERVAL PG Bug reporting form <noreply@postgresql.org>
2019-04-09 17:23 ` Bruce Momjian <bruce@momjian.us>
2019-04-09 18:21 ` s.celles@gmail.com <s.celles@gmail.com>
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