Re: roundoff problem in time datatype

Lists: pgsql-hackerspgsql-patches
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: roundoff problem in time datatype
Date: 2005-09-25 17:26:25
Message-ID: 501.1127669185@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Inserting into a time field with limited precision rounds off, which
is good except for this case:

regression=# select '23:59:59.9'::time(0);
time
----------
24:00:00
(1 row)

This is bad because:

regression=# select '24:00:00'::time(0);
ERROR: date/time field value out of range: "24:00:00"

which means that data originally accepted will fail to dump and reload.

I see this behavior in all versions back to 7.3. 7.2 was even more
broken:

regression=# select '23:59:59.9'::time(0);
time
----------
00:00:00
(1 row)

I think the correct behavior has to be to check for overflow again
after rounding off. Alternatively: why are we forbidding the value
24:00:00 anyway? Is there a reason not to allow the hours field
to exceed 23?

regards, tom lane


From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 07:39:38
Message-ID: Pine.LNX.4.44.0509260935310.28238-100000@zigo.dhs.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Sun, 25 Sep 2005, Tom Lane wrote:

> Alternatively: why are we forbidding the value 24:00:00 anyway? Is
> there a reason not to allow the hours field to exceed 23?

One reason is because it's what the standard demand. Another is that it
isn't a proper time, just like feb 31 isn't a proper date.

--
/Dennis Björklund


From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 12:58:54
Message-ID: 2017792B-EB5B-4275-B771-4AF35599976B@fastcrypt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Actually, I think there is a case where 24:00 is a proper time. Isn't
it used for adding leap seconds ?

Dave
On 26-Sep-05, at 3:39 AM, Dennis Bjorklund wrote:

> On Sun, 25 Sep 2005, Tom Lane wrote:
>
>
>> Alternatively: why are we forbidding the value 24:00:00 anyway? Is
>> there a reason not to allow the hours field to exceed 23?
>>
>
> One reason is because it's what the standard demand. Another is
> that it
> isn't a proper time, just like feb 31 isn't a proper date.
>
> --
> /Dennis Björklund
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that
> your
> message can get through to the mailing list cleanly
>
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dave Cramer <pg(at)fastcrypt(dot)com>
Cc: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 13:15:14
Message-ID: 23746.1127740514@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Dave Cramer <pg(at)fastcrypt(dot)com> writes:
> Actually, I think there is a case where 24:00 is a proper time. Isn't
> it used for adding leap seconds ?

No, I think the usual notation for a leap-second is '23:59:60'.
We do allow 60 in the seconds field for this purpose.

I suppose there's another possible approach, which is to special-case
the output of this value to look like '23:59:60' instead of '24:00:00'.
Then it could be reloaded. On the whole though, most people who came
across that behavior would probably think it's a bug...

regards, tom lane


From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 14:16:09
Message-ID: Pine.LNX.4.44.0509261552380.28238-100000@zigo.dhs.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, 26 Sep 2005, Tom Lane wrote:

> > Actually, I think there is a case where 24:00 is a proper time. Isn't
> > it used for adding leap seconds ?
>
> No, I think the usual notation for a leap-second is '23:59:60'.
> We do allow 60 in the seconds field for this purpose.

Yes, and it can go up to 23:59:60.999999 (depending on how many fractional
seconds one want).

> I suppose there's another possible approach, which is to special-case
> the output of this value to look like '23:59:60' instead of '24:00:00'.

You would get the same problem with 23:59:60.9 which I guess you want to
round up.

One "solution" is to round '23:59:59.9'::time(0) up to '00:00:00'. That is
normally the next following time value after all. I know why you might not
want to round it "up" to 00:00:00, but it's one logical solution.

By the way, here is another example of the same problem:

# SELECT time '23:59:59.9' + interval '0.1';
?column?
----------
24:00:00

# SELECT time '23:59:59.9' + interval '0.11';
?column?
-------------
00:00:00.01
(1 rad)

--
/Dennis Björklund


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 14:28:31
Message-ID: 24340.1127744911@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org> writes:
> On Mon, 26 Sep 2005, Tom Lane wrote:
>> No, I think the usual notation for a leap-second is '23:59:60'.
>> We do allow 60 in the seconds field for this purpose.

> Yes, and it can go up to 23:59:60.999999 (depending on how many fractional
> seconds one want).

That's an urban legend. There never have been, and never will be, two
leap seconds instituted in the same minute. We really should reject
anything larger than '23:59:60'.

> One "solution" is to round '23:59:59.9'::time(0) up to '00:00:00'.

7.2 did that, and we concluded it was broken.

regards, tom lane


From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 14:40:03
Message-ID: Pine.LNX.4.44.0509261632010.28238-100000@zigo.dhs.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, 26 Sep 2005, Tom Lane wrote:

> > Yes, and it can go up to 23:59:60.999999 (depending on how many fractional
> > seconds one want).
>
> That's an urban legend. There never have been, and never will be, two
> leap seconds instituted in the same minute. We really should reject
> anything larger than '23:59:60'.

The above is still just one leap second. The time continues to tick until
it wraps over to 00:00:00. So for example a time value of 23:59:60.42
exists if we allow just one leap second.

> > One "solution" is to round '23:59:59.9'::time(0) up to '00:00:00'.
>
> 7.2 did that, and we concluded it was broken.

Doesn't mean that it necissary was a correct conclusion (and I'm not
stating that it was wrong, I would like to think about it for a while
before I claim something like that).

Do the sql standard say anything on the matter?

--
/Dennis Björklund


From: Greg Stark <gsstark(at)mit(dot)edu>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>, Dave Cramer <pg(at)fastcrypt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 15:04:13
Message-ID: 87k6h3hnv6.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

> That's an urban legend. There never have been, and never will be, two
> leap seconds instituted in the same minute. We really should reject
> anything larger than '23:59:60'.

I don't understand. The last second of a normal minute goes from 59.0 to
59.999 (etc) before the next minute begins. So surely the last second of a
minute containing a leap second goes from 60.0 to 60.999?

--
greg


From: Jochem van Dieten <jochemd(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 15:16:41
Message-ID: f96a9b8305092608166df19fa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On 9/26/05, Dennis Bjorklund wrote:
> On Sun, 25 Sep 2005, Tom Lane wrote:
>>
>> Alternatively: why are we forbidding the value 24:00:00 anyway? Is
>> there a reason not to allow the hours field to exceed 23?
>
> One reason is because it's what the standard demand.

Could you cite that? The only thing I can find in the SQL standard is
that the hour field in an INTERVAL can not exceed 23, not datetimes.

> Another is that it
> isn't a proper time, just like feb 31 isn't a proper date.

IIRC ISO 8601 (to whcih the SQL standard points) says
2005-10-01T24:00:00 is valid (and happens to be the same as
2005-10-02T00:00:00). It does seem a bit inconsistent with the spec of
an interval though.

Jochem


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jochem van Dieten <jochemd(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 15:46:47
Message-ID: 29661.1127749607@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Jochem van Dieten <jochemd(at)gmail(dot)com> writes:
> On 9/26/05, Dennis Bjorklund wrote:
>> One reason is because it's what the standard demand.

> Could you cite that? The only thing I can find in the SQL standard is
> that the hour field in an INTERVAL can not exceed 23, not datetimes.

SQL99 has

_____________Table_11-Valid_values_for_datetime_fields_____________

_Keyword____________Valid_values_of_datetime_fields________________

| YEAR | 0001 to 9999 |
| | |
| MONTH | 01 to 12 |
| | |
| DAY | Within the range 1 (one) to 31, but further |
constrained by the value of MONTH and YEAR
fields, according to the rules for well-
formed dates in the Gregorian calendar.

| HOUR | 00 to 23 |
| | |
| MINUTE | 00 to 59 |
| | |
| SECOND | 00 to 61.9(N) where "9(N)" indicates |
the number of digits specified by <time
fractional seconds precision>.

| TIMEZONE_HOUR | -12 to 13 |
| | |
|_TIMEZONE_MINUTE__|_-59_to_59_____________________________________|
| | |
NOTE 62 - Datetime data types will allow dates in the Gregorian
format to be stored in the date range 0001-01-01 CE through
9999-12-31 CE. The range for SECOND allows for as many as two
"leap seconds". Interval arithmetic that involves leap seconds
or discontinuities in calendars will produce implementation-
defined results.

The urban legend about needing 2 leap seconds in the same minute has
infected the standard I see. It should only allow 60.9999 as the max
value for SECOND.

Note however that we feel free to exceed the spec in other aspects of
this --- we exceed their year range for instance. So I don't think we
necessarily have to reject '24:00:00'.

Also, the spec explicitly states that arithmetic on TIME values is done
modulo 24 hours. So it's correct for '23:59:59'::time + '1 second'::interval
to yield '00:00:00', but this does not necessarily mean that we should
cause rounding to behave that way. Depends whether you think that
rounding is an arithmetic operation or not ...

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 15:49:40
Message-ID: 29688.1127749780@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org> writes:
> Do the sql standard say anything on the matter?

It doesn't seem very helpful. AFAICS, we should interpret storing
'23:59:59.99' into a TIME(0) field as a cast from TIME(2) to TIME(0),
and the spec defines that as

15) If TD is the datetime data type TIME WITHOUT TIME ZONE, then let
TSP be the <time precision> of TD.

b) If SD is TIME WITHOUT TIME ZONE, then TV is SV, with
implementation-defined rounding or truncation if necessary.

So it's "implementation-defined" what we do.

regards, tom lane


From: Gaetano Mendola <mendola(at)bigfoot(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 16:13:58
Message-ID: dh96o9$oiq$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org> writes:
>> On Mon, 26 Sep 2005, Tom Lane wrote:
>>> No, I think the usual notation for a leap-second is '23:59:60'.
>>> We do allow 60 in the seconds field for this purpose.
>
>> Yes, and it can go up to 23:59:60.999999 (depending on how many fractional
>> seconds one want).
>
> That's an urban legend. There never have been, and never will be, two
> leap seconds instituted in the same minute. We really should reject
> anything larger than '23:59:60'.

mmm. The second "60" have is on duration of 1 second so 23:59:60.4 have
is right to exist.

Regards
Gaetano Mendola


From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 16:22:17
Message-ID: Pine.LNX.4.44.0509261812420.28238-100000@zigo.dhs.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, 26 Sep 2005, Tom Lane wrote:

> b) If SD is TIME WITHOUT TIME ZONE, then TV is SV, with
> implementation-defined rounding or truncation if necessary.
>
> So it's "implementation-defined" what we do.

Truncation would avoid the problem but of course loses some of the info.

So, what are the alternatives:

* Truncation.

* Rounding and let it wrap when rounding up towards midnight.

* Rounding and never let it wrap. The cases that would wrap
goes to 23:59:59 (or 23:59:59.9 and so on for other precisions)
or to 23:59:60 (or 23:59.60.9 and so on) if one start with a
leap second time.

Are there any more viable cases?

--
/Dennis Björklund


From: Andreas Pflug <pgadmin(at)pse-consulting(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 16:23:06
Message-ID: 4338206A.7080901@pse-consulting.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org> writes:
>
>>Do the sql standard say anything on the matter?
>
>
> It doesn't seem very helpful. AFAICS, we should interpret storing
> '23:59:59.99' into a TIME(0) field as a cast from TIME(2) to TIME(0),
> and the spec defines that as
>
> 15) If TD is the datetime data type TIME WITHOUT TIME ZONE, then let
> TSP be the <time precision> of TD.
>
> b) If SD is TIME WITHOUT TIME ZONE, then TV is SV, with
> implementation-defined rounding or truncation if necessary.
>
> So it's "implementation-defined" what we do.

IMHO Since 23:59:59.99 probably means "the last milliseconds of this
day, as far as precision allows to express it", this should be truncated
to 23:59:59, not rounded to 24:00:00. Until the last microsecond has
elapsed, it's not 24 hours (you wouldn't round "happy new year" at
23:59:30 from a clock with minutes only either)

Regards,
Andreas


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andreas Pflug <pgadmin(at)pse-consulting(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 17:00:21
Message-ID: 284.1127754021@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Andreas Pflug <pgadmin(at)pse-consulting(dot)de> writes:
> Tom Lane wrote:
>> b) If SD is TIME WITHOUT TIME ZONE, then TV is SV, with
>> implementation-defined rounding or truncation if necessary.
>>
>> So it's "implementation-defined" what we do.

> IMHO Since 23:59:59.99 probably means "the last milliseconds of this
> day, as far as precision allows to express it", this should be truncated
> to 23:59:59, not rounded to 24:00:00. Until the last microsecond has
> elapsed, it's not 24 hours (you wouldn't round "happy new year" at
> 23:59:30 from a clock with minutes only either)

Hm, so the proposal is "round unless that would produce 24:00:00, in
which case truncate"? Seems a bit ugly but it would follow the letter
of the spec, and avoid rejecting inputs that we used to accept. It's
still not very clear what to do with '23:59:60.9' though.

regards, tom lane


From: Andreas Pflug <pgadmin(at)pse-consulting(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-26 17:21:21
Message-ID: 43382E11.30009@pse-consulting.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:

>
> Hm, so the proposal is "round unless that would produce 24:00:00, in
> which case truncate"? Seems a bit ugly but it would follow the letter
> of the spec, and avoid rejecting inputs that we used to accept. It's
> still not very clear what to do with '23:59:60.9' though.

I'd handle it the same; 23.59.60.9 -> 23.59.60 since this is apparently
a leap second. A normal second should never become a leap second from
some conversion, but a leap second should stay one.

Regards,
Andreas


From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Andreas Pflug <pgadmin(at)pse-consulting(dot)de>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-30 22:27:06
Message-ID: 20050930222705.GH40138@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, Sep 26, 2005 at 06:23:06PM +0200, Andreas Pflug wrote:
> Tom Lane wrote:
> >Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org> writes:
> >
> >>Do the sql standard say anything on the matter?
> >
> >
> >It doesn't seem very helpful. AFAICS, we should interpret storing
> >'23:59:59.99' into a TIME(0) field as a cast from TIME(2) to TIME(0),
> >and the spec defines that as
> >
> > 15) If TD is the datetime data type TIME WITHOUT TIME ZONE, then
> > let
> > TSP be the <time precision> of TD.
> >
> > b) If SD is TIME WITHOUT TIME ZONE, then TV is SV, with
> > implementation-defined rounding or truncation if necessary.
> >
> >So it's "implementation-defined" what we do.
>
> IMHO Since 23:59:59.99 probably means "the last milliseconds of this
> day, as far as precision allows to express it", this should be truncated
> to 23:59:59, not rounded to 24:00:00. Until the last microsecond has
> elapsed, it's not 24 hours (you wouldn't round "happy new year" at
> 23:59:30 from a clock with minutes only either)

Maybe also allow for a warning to be generated? Or some way to signal an
overflow?

I think it could be valid to do this, or round up to 24:00:00 or 'round
up' to 00:00:00, depending on what the app was trying to accomplish.
Would it be possible to allow an option to the datatype that specifies
the rounding behavior, or would they need to be different datatypes?
--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461


From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jochem van Dieten <jochemd(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-09-30 22:29:25
Message-ID: 20050930222925.GI40138@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mon, Sep 26, 2005 at 11:46:47AM -0400, Tom Lane wrote:
> Jochem van Dieten <jochemd(at)gmail(dot)com> writes:
> > On 9/26/05, Dennis Bjorklund wrote:
> >> One reason is because it's what the standard demand.
>
> > Could you cite that? The only thing I can find in the SQL standard is
> > that the hour field in an INTERVAL can not exceed 23, not datetimes.
>
> SQL99 has
>
> _____________Table_11-Valid_values_for_datetime_fields_____________
>
> _Keyword____________Valid_values_of_datetime_fields________________
>
> | YEAR | 0001 to 9999 |
> | | |
> | MONTH | 01 to 12 |
> | | |
> | DAY | Within the range 1 (one) to 31, but further |
> constrained by the value of MONTH and YEAR
> fields, according to the rules for well-
> formed dates in the Gregorian calendar.
>
> | HOUR | 00 to 23 |
> | | |
> | MINUTE | 00 to 59 |
> | | |
> | SECOND | 00 to 61.9(N) where "9(N)" indicates |
> the number of digits specified by <time
> fractional seconds precision>.
>
> | TIMEZONE_HOUR | -12 to 13 |
> | | |
> |_TIMEZONE_MINUTE__|_-59_to_59_____________________________________|
> | | |
> NOTE 62 - Datetime data types will allow dates in the Gregorian
> format to be stored in the date range 0001-01-01 CE through
> 9999-12-31 CE. The range for SECOND allows for as many as two
> "leap seconds". Interval arithmetic that involves leap seconds
> or discontinuities in calendars will produce implementation-
> defined results.
>
> The urban legend about needing 2 leap seconds in the same minute has
> infected the standard I see. It should only allow 60.9999 as the max
> value for SECOND.
>
> Note however that we feel free to exceed the spec in other aspects of
> this --- we exceed their year range for instance. So I don't think we
> necessarily have to reject '24:00:00'.
>
> Also, the spec explicitly states that arithmetic on TIME values is done
> modulo 24 hours. So it's correct for '23:59:59'::time + '1 second'::interval
> to yield '00:00:00', but this does not necessarily mean that we should
> cause rounding to behave that way. Depends whether you think that
> rounding is an arithmetic operation or not ...

Does that portion of the spec also apply to plain time fields? The
entire issue here only exists because there's no method to handle the
overflow, unlike in a timestamp.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-10-13 20:48:01
Message-ID: 200510132048.j9DKm1g08373@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


Where are we on this? I see current CVS behaving the same as below,
except the last query now returns 24:00:00.

---------------------------------------------------------------------------

Tom Lane wrote:
> Inserting into a time field with limited precision rounds off, which
> is good except for this case:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 24:00:00
> (1 row)
>
> This is bad because:
>
> regression=# select '24:00:00'::time(0);
> ERROR: date/time field value out of range: "24:00:00"
>
> which means that data originally accepted will fail to dump and reload.
>
> I see this behavior in all versions back to 7.3. 7.2 was even more
> broken:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 00:00:00
> (1 row)
>
> I think the correct behavior has to be to check for overflow again
> after rounding off. Alternatively: why are we forbidding the value
> 24:00:00 anyway? Is there a reason not to allow the hours field
> to exceed 23?
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-10-13 20:53:16
Message-ID: 2598.1129236796@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> Where are we on this?

We haven't decided what to do.

I think my preference is to allow '24:00:00' (but not anything larger)
as a valid input value of the time datatypes. This for two reasons:
* existing dump files may contain such values
* it's consistent with allowing, eg, '12:13:60', which we
allow even though it's certainly not a valid leap second.

The alternative is to try to catch all places where 23:59:59.something
could get rounded up to 24:00:00, but that looks messy, and it would
introduce a gotcha into calculations on time values.

regards, tom lane


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-10-13 20:53:55
Message-ID: 200510132053.j9DKrtc16848@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> > Where are we on this?
>
> We haven't decided what to do.
>
> I think my preference is to allow '24:00:00' (but not anything larger)
> as a valid input value of the time datatypes. This for two reasons:
> * existing dump files may contain such values
> * it's consistent with allowing, eg, '12:13:60', which we
> allow even though it's certainly not a valid leap second.
>
> The alternative is to try to catch all places where 23:59:59.something
> could get rounded up to 24:00:00, but that looks messy, and it would
> introduce a gotcha into calculations on time values.

Is this a must-fix for 8.1?

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: roundoff problem in time datatype
Date: 2005-10-13 21:04:47
Message-ID: 2708.1129237487@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> Tom Lane wrote:
>> I think my preference is to allow '24:00:00' (but not anything larger)
>> as a valid input value of the time datatypes.

> Is this a must-fix for 8.1?

No, since it's a pre-existing issue, but it's the kind of thing that
should be changed during a major release not a point-release. If we
don't change it then I think we'd have to wait till 8.2 before doing
anything about it.

regards, tom lane


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: roundoff problem in time datatype
Date: 2005-10-13 21:19:01
Message-ID: 200510131419.01729.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom,

> I think my preference is to allow '24:00:00' (but not anything larger)
> as a valid input value of the time datatypes. This for two reasons:
> * existing dump files may contain such values
> * it's consistent with allowing, eg, '12:13:60', which we
> allow even though it's certainly not a valid leap second.

It's also consistent with how several other RDBMSes do things (SQL Server,
MySQL), and several programming languages.

--
--Josh

Josh Berkus
Aglio Database Solutions
San Francisco


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] roundoff problem in time datatype
Date: 2005-10-14 02:54:30
Message-ID: 200510140254.j9E2sVK16102@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


I have written the attached patch which I think does what you suggested.
I found all the places where we disallowed 24:00:00, and make it valid,
including nabstime.c.

test=> select '24:00:00'::time(0);
time
----------
24:00:00
(1 row)

test=> select '24:00:01'::time(0);
ERROR: date/time field value out of range: "24:00:01"

---------------------------------------------------------------------------

Tom Lane wrote:
> Inserting into a time field with limited precision rounds off, which
> is good except for this case:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 24:00:00
> (1 row)
>
> This is bad because:
>
> regression=# select '24:00:00'::time(0);
> ERROR: date/time field value out of range: "24:00:00"
>
> which means that data originally accepted will fail to dump and reload.
>
> I see this behavior in all versions back to 7.3. 7.2 was even more
> broken:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 00:00:00
> (1 row)
>
> I think the correct behavior has to be to check for overflow again
> after rounding off. Alternatively: why are we forbidding the value
> 24:00:00 anyway? Is there a reason not to allow the hours field
> to exceed 23?
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

Attachment Content-Type Size
unknown_filename text/plain 4.1 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] roundoff problem in time datatype
Date: 2005-10-14 02:59:26
Message-ID: 5143.1129258766@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> I have written the attached patch which I think does what you suggested.
> I found all the places where we disallowed 24:00:00, and make it valid,
> including nabstime.c.

Looks reasonable right offhand ... don't forget to update the docs too.

regards, tom lane


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] roundoff problem in time datatype
Date: 2005-10-14 11:48:20
Message-ID: 200510141148.j9EBmKu24143@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


Patch applied, with documentation updates.

---------------------------------------------------------------------------

Tom Lane wrote:
> Inserting into a time field with limited precision rounds off, which
> is good except for this case:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 24:00:00
> (1 row)
>
> This is bad because:
>
> regression=# select '24:00:00'::time(0);
> ERROR: date/time field value out of range: "24:00:00"
>
> which means that data originally accepted will fail to dump and reload.
>
> I see this behavior in all versions back to 7.3. 7.2 was even more
> broken:
>
> regression=# select '23:59:59.9'::time(0);
> time
> ----------
> 00:00:00
> (1 row)
>
> I think the correct behavior has to be to check for overflow again
> after rounding off. Alternatively: why are we forbidding the value
> 24:00:00 anyway? Is there a reason not to allow the hours field
> to exceed 23?
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

Attachment Content-Type Size
unknown_filename text/plain 5.8 KB

From: Neil Conway <neilc(at)samurai(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] roundoff problem in time datatype
Date: 2005-10-16 00:13:32
Message-ID: 1129421612.8219.37.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Thu, 2005-13-10 at 22:54 -0400, Bruce Momjian wrote:
> I have written the attached patch which I think does what you suggested.
> I found all the places where we disallowed 24:00:00, and make it valid,
> including nabstime.c.

Should this be added to the regression tests?

-Neil


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] roundoff problem in time datatype
Date: 2005-10-16 15:50:09
Message-ID: 200510161550.j9GFo9e14041@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Neil Conway wrote:
> On Thu, 2005-13-10 at 22:54 -0400, Bruce Momjian wrote:
> > I have written the attached patch which I think does what you suggested.
> > I found all the places where we disallowed 24:00:00, and make it valid,
> > including nabstime.c.
>
> Should this be added to the regression tests?

Yes.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: josh(at)agliodbs(dot)com, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: roundoff problem in time datatype
Date: 2005-11-04 21:15:59
Message-ID: 65937bea0511041315l2df6b73dhd162aa65b8c5ad25@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On 10/13/05, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
> Tom,
>
> > I think my preference is to allow '24:00:00' (but not anything larger)
> > as a valid input value of the time datatypes. This for two reasons:
> > * existing dump files may contain such values
> > * it's consistent with allowing, eg, '12:13:60', which we
> > allow even though it's certainly not a valid leap second.

we shouldn't be allowing such timestamps! We should enforce only the
canonical formats of any datatype. Imagine what chaos would have been
caused if we didn't have IEEE specifications for the floating point
numbers!!!

>
> It's also consistent with how several other RDBMSes do things (SQL Server,
> MySQL), and several programming languages.

Just wanted to note that this is not really consistent with other
databases. For eg. SQL Server's o/p is shown below.

select convert( datetime, '23:59:59.998' )
1900-01-01 23:59:59.997

select convert( datetime, '23:59:59.999' )
1900-01-02 00:00:00.000 /* the date changes but the time remains
under 24:00:00 */

select convert( datetime, '24:00:00' )
Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.

Moreover, 24:00:00 not in canonical format so it should not be encoraged at all.

Gujreet.