dbTalk Databases Forums  

'1 year' = '360 days' ????

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss '1 year' = '360 days' ???? in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ricardo Perez Lopez
 
Posts: n/a

Default '1 year' = '360 days' ???? - 10-22-2004 , 06:37 AM






Hello everyone:

I'm a PostgreSQL newbie, working now with dates, times, timestamps and
intervals.

I have three questions about the above:

FIRST:
--------

I have observed that, for PostgreSQL, one year is actually 360 days:

SELECT '1 year'::timestamp = '360 days'::timestamp;

?column?
-------------
t


Glubs! I believed that 1 year is 365 days, or 366 if leap. Is it normal?


SECOND:
-----------

When I want to check how many time is between two dates, I have two options
(which shows two different results):

SELECT '30-09-04'::timestamp - '30-09-03'::timestamp,
age('30-09-04'::timestamp, '30-09-03'::timestamp);

?column? | age
-------------------------------
@ 366 days | @ 1 year


The results are different. If we compare the two results:

SELECT ('30-09-04'::timestamp - '30-09-03'::timestamp) =
age('30-09-04'::timestamp, '30-09-03'::timestamp);


?column?
--------------
f


Obviously, it returns False, because I told in the first question, 1 year is
360 days for PostgreSQL.

The question is: is it normal? Which of the two methods is the correct? To
substract timestamps? Or to use the age function?


THIRD:
--------

As I told in the second question, when I do:

SELECT '30-09-04'::timestamp - '30-09-03'::timestamp;

the result is:

?column?
--------------
@ 366 days

The question is: is there any way to "normalize" the result, such that the
result was:

@ 1 year 1 day

?

I think it's better (and more correct) "@ 1 year 1 day" rather than "@ 366
days". Is there any way to achieve that?

Thanks to all.

Ricardo.

__________________________________________________ _______________
Horóscopo, tarot, numerología... Escucha lo que te dicen los astros.
http://astrocentro.msn.es/


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


Reply With Quote
  #2  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 06:25 PM






On Fri, Oct 22, 2004 at 13:37:19 +0200,
Ricardo Perez Lopez <ricpelo (AT) hotmail (DOT) com> wrote:
Quote:
Hello everyone:

I'm a PostgreSQL newbie, working now with dates, times, timestamps and
intervals.

I have three questions about the above:

FIRST:
--------

I have observed that, for PostgreSQL, one year is actually 360 days:
No it isn't. The interval is stored as months and seconds. When
adding intervals to timestamps, adding months and adding seconds are
handled differently. Under some circumstances the months part gets
converted to seconds, and in that event a month is taken to be as
long as 30 days.

Quote:
SELECT '1 year'::timestamp = '360 days'::timestamp;

?column?
-------------
t


Glubs! I believed that 1 year is 365 days, or 366 if leap. Is it normal?


SECOND:
-----------

When I want to check how many time is between two dates, I have two options
(which shows two different results):

SELECT '30-09-04'::timestamp - '30-09-03'::timestamp,
age('30-09-04'::timestamp, '30-09-03'::timestamp);

?column? | age
-------------------------------
@ 366 days | @ 1 year


The results are different. If we compare the two results:

SELECT ('30-09-04'::timestamp - '30-09-03'::timestamp) =
age('30-09-04'::timestamp, '30-09-03'::timestamp);


?column?
--------------
f


Obviously, it returns False, because I told in the first question, 1 year
is 360 days for PostgreSQL.
That isn't really why. When you use age you get an interval with a mix of month
and seconds parts. If you subtract two timestamps then you get an interval
with just a seconds part.

Quote:
The question is: is it normal? Which of the two methods is the correct? To
substract timestamps? Or to use the age function?


THIRD:
--------

As I told in the second question, when I do:

SELECT '30-09-04'::timestamp - '30-09-03'::timestamp;

the result is:

?column?
--------------
@ 366 days

The question is: is there any way to "normalize" the result, such that the
result was:

@ 1 year 1 day
Use age. If you do that, that is what the interval will look like internally.
I don't think there is an easy way to output the value of an interval
so that it looks like that. But if you use it in operations it should
do what you want. (Though you need to consider whether you want the
day added before or after you add the 12 months.)

Quote:
?

I think it's better (and more correct) "@ 1 year 1 day" rather than "@ 366
days". Is there any way to achieve that?

Thanks to all.

Ricardo.

__________________________________________________ _______________
Horóscopo, tarot, numerología... Escucha lo que te dicen los astros.
http://astrocentro.msn.es/


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org
---------------------------(end of broadcast)---------------------------
TIP 3: 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



Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 06:52 PM



"Ricardo Perez Lopez" <ricpelo (AT) hotmail (DOT) com> writes:
Quote:
I have observed that, for PostgreSQL, one year is actually 360 days:

SELECT '1 year'::timestamp = '360 days'::timestamp;

?column?
-------------
t
Nonsense.

regression=# SELECT '1 year'::timestamp = '360 days'::timestamp;
ERROR: invalid input syntax for type timestamp: "1 year"

How about telling us what you *really* did, instead of posting faked
examples?

There are some contexts in which an interval (not a timestamp) of 1
month will be taken as equivalent to 30 days, for lack of any better
idea, but it's not the case that Postgres doesn't know the difference.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #4  
Old   
Doug McNaught
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 07:13 PM



Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> writes:

Quote:
"Ricardo Perez Lopez" <ricpelo (AT) hotmail (DOT) com> writes:
I have observed that, for PostgreSQL, one year is actually 360 days:

SELECT '1 year'::timestamp = '360 days'::timestamp;

?column?
-------------
t

Nonsense.

regression=# SELECT '1 year'::timestamp = '360 days'::timestamp;
ERROR: invalid input syntax for type timestamp: "1 year"

How about telling us what you *really* did, instead of posting faked
examples?
FWIW:

template1=# select '1 year'::interval = '360 days'::interval;
?column?
----------
t
(1 row)

template1=# select '1 year'::interval = '365 days'::interval;
?column?
----------
f
(1 row)

template1=# select version();
version
---------------------------------------------------------------------
PostgreSQL 7.4.5 on i686-pc-linux-gnu, compiled by GCC egcs-2.91.66
(1 row)

-Doug

---------------------------(end of broadcast)---------------------------
TIP 3: 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



Reply With Quote
  #5  
Old   
Tom Lane
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 08:38 PM



Doug McNaught <doug (AT) mcnaught (DOT) org> writes:
Quote:
template1=# select '1 year'::interval = '360 days'::interval;
?column?
----------
t
(1 row)
Yeah, if you look at interval_cmp_internal() it's fairly obvious why.
I think that this definition is probably bogus, and that only intervals
that match exactly (equal months parts *and* equal seconds parts) should
be considered "equal". However the most obvious way to redefine it
(compare the months, and only if equal compare the seconds) would lead
to rather nonintuitive behaviors such as "'1 year' > '1000 days'".
Anybody have any thoughts about a better way to map the multicomponent
reality into a one-dimensional sorting order?

(Note also that as Bruno was just mentioning, we really ought to have
months/days/seconds components, not just months/seconds; which makes the
comparison issue even more interesting.)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #6  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 09:29 PM



On Sat, Oct 23, 2004 at 21:38:15 -0400,
Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> wrote:
Quote:
Doug McNaught <doug (AT) mcnaught (DOT) org> writes:
template1=# select '1 year'::interval = '360 days'::interval;
?column?
----------
t
(1 row)

Yeah, if you look at interval_cmp_internal() it's fairly obvious why.
I think that this definition is probably bogus, and that only intervals
that match exactly (equal months parts *and* equal seconds parts) should
be considered "equal". However the most obvious way to redefine it
(compare the months, and only if equal compare the seconds) would lead
to rather nonintuitive behaviors such as "'1 year' > '1000 days'".
Anybody have any thoughts about a better way to map the multicomponent
reality into a one-dimensional sorting order?
You could return NULL for cases where the number of months in the
first interval is less than the second, but the number of seconds in
the second interval is greater than the first.
You could even tighten things down more by using that months have to
be at least 28 days, but not more than 31 days (neglecting daylight
savings time).
If you want to be able to use a btree index, you need a total ordering, so
in that case I think you have to have things work pretty much the way they do
now, including the way the equality operator works.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #7  
Old   
Tom Lane
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 10:36 PM



Bruno Wolff III <bruno (AT) wolff (DOT) to> writes:
Quote:
Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> wrote:
Anybody have any thoughts about a better way to map the multicomponent
reality into a one-dimensional sorting order?

You could return NULL for cases where the number of months in the
first interval is less than the second, but the number of seconds in
the second interval is greater than the first.
No, you can't, at least not if you want to have btree indexes on
interval columns. The comparison operators can never return NULL
for nonnull inputs.

Quote:
If you want to be able to use a btree index, you need a total ordering, so
in that case I think you have to have things work pretty much the way they do
now, including the way the equality operator works.
We don't have to have this particular sorting decision, we just have
to have *some* unique sorting order. In particular, if we want to say
that two interval values are not equal, we have to be able to say which
one is less. For instance, "compare the months first and only if equal
compare the seconds" would work fine from the point of view of btree.
It's just that that leads to a sort order that users will probably not
like very much.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #8  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 11:15 PM



On Sat, Oct 23, 2004 at 23:36:05 -0400,
Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> wrote:
Quote:
We don't have to have this particular sorting decision, we just have
to have *some* unique sorting order. In particular, if we want to say
that two interval values are not equal, we have to be able to say which
one is less. For instance, "compare the months first and only if equal
compare the seconds" would work fine from the point of view of btree.
It's just that that leads to a sort order that users will probably not
like very much.
One way to do comparisons is to use a mapping f(m,s) => R and compare
(m1,s1) and (m2,s2) by comparing f(m1,s1) and f(m2,s2) and break ties
by comparing say m1 and m2. This will work as long as f(m,s1) = f(m,s2)
implies s1 = s2. It will probably be desirable to use a subset of these
mappings where f(m,s) = g(m) + h(s). In fact the current system uses
this with g(m) = 30*24*60*60*m and h(s) = s (but without the tiebreak
that compares m values). Because of the way intervals work, I think
you want to use an ordering generated like that you want to use
something of the form f(m,s) = C1*m + C2*s. I also think that treating
a month as 30 days and having round numbers is better than using
something like 1/12 a solar year in seconds. So I think the best plan
is to do things as they are now, except for adding a tie breaker just
using months or seconds for when both intervals give the same number of
seconds when treating months as 30 days, but have a different number of
months.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #9  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-23-2004 , 11:51 PM



On Sat, Oct 23, 2004 at 23:15:57 -0500,
Bruno Wolff III <bruno (AT) wolff (DOT) to> wrote:
Quote:
by comparing say m1 and m2. This will work as long as f(m,s1) = f(m,s2)
implies s1 = s2. It will probably be desirable to use a subset of these
mappings where f(m,s) = g(m) + h(s). In fact the current system uses
this with g(m) = 30*24*60*60*m and h(s) = s (but without the tiebreak
that compares m values). Because of the way intervals work, I think
you want to use an ordering generated like that you want to use
something of the form f(m,s) = C1*m + C2*s. I also think that treating
a month as 30 days and having round numbers is better than using
something like 1/12 a solar year in seconds. So I think the best plan
is to do things as they are now, except for adding a tie breaker just
using months or seconds for when both intervals give the same number of
seconds when treating months as 30 days, but have a different number of
months.
Some more comments on this. I was thinking about it a bit more and using
1/12 of the number of seconds in a solar year doesn't seem that bad
for comparisons. That way 366 days > 1 year > 365 days. However, if you
go that route, I think you would also want to change EXTRACT so that
when you extract the EPOCH you use the same function as for comparison.
One value I found for a solar year is 365 days, 5 hours, 48 minutes, 45.51
seconds.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
  #10  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: '1 year' = '360 days' ???? - 10-24-2004 , 12:22 AM



On Sat, Oct 23, 2004 at 23:51:20 -0500,
Bruno Wolff III <bruno (AT) wolff (DOT) to> wrote:
Quote:
One value I found for a solar year is 365 days, 5 hours, 48 minutes, 45.51
seconds.
Wikipedia gives 365.242189670 days (86400 seconds) as the length of the
mean solar year in 2000. To give you some idea of how constant that values is,
Wikipedia claims that 2000 years ago the mean solar year was about 10 seconds
longer.
Using the above value I get there is an average of 2629743 seconds in a month.

And yet another option is to note that in the Gregorian calendar there are
400*365+97 days or 400*12 months in 400 years, which gives 2629746 seconds
per month on average.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.