From: | Joseph Barillari <jbarilla(at)princeton(dot)edu> |
---|---|
To: | Josh Berkus <josh(at)agliodbs(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Casting dates |
Date: | 2002-04-29 16:19:21 |
Message-ID: | m3lmb6fpfa.fsf@washer.barillari.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
>>>>> "JB" == Josh Berkus <josh(at)agliodbs(dot)com> writes:
JB> Joe,
>> Does anyone know if it's possible to calculate the number of
>> days (or weeks, seconds, minutes, or what-have-you) in a given
>> interval without casting to EPOCH and performing manipulations
>> there?
>>
>> PostgreSQL usually returns intervals created by subtraction in
>> days and smaller fragments, I would like to know if it's
>> possible for it to return years. (Integer division of the
>> number of days by 365 would produce an almost-correct result,
>> but it's rather inelegant.)
JB> Unfortunately, you've hit one of Postgres implementation
JB> limits. Multiplying and dividing INTERVAL values, while
JB> covered by the SQL92 spec, has not been completely implemented
JB> in Postgres. That is, it should be possible for you to:
JB> SELECT INTERVAL '3 days 5 hours' / INTERVAL '1 minute'
JB> However, implementing this has been a challenge for all SQL-DB
JB> designers. I don't know of any DB that does handle INTERVAL
JB> division.
JB> It's a surmountable challenge, though, but requires somebody
JB> to take the lead in a) working out the logic, and b) writing
JB> the code to implement it. A) is not a trivial task, either
JB> ... while 60 minutes / 1 second is obvious, how about 4 months
JB> / 4 days? Months are not constant in length.
For the purposes of this application (because it deals in relatively
short intervals, it's a calendaring application), I just divide the
day count by 365 and throw away the remainder.
For example,
select date_part('day', (timestamp 'jan 1, 1996' -
timestamp 'jan 1, 1992'));
is 1461 days, or 1+365*4 (1992 is a leap year). Integer division of
1461/365 produces the correct year count. This doesn't work for longer
intervals:
cal=> select date_part('day', (timestamp 'jan 1, 2600'
- timestamp 'jan 1, 1000'))/365;
?column?
------------------
1601.06301369863 <-- an error of +1 year, due to accumulated leap days
Obviously, this naive method won't work for astronomical time
intervals, but for my purposes, few people have calendars that
stretch across millennia.
--Joe
From | Date | Subject | |
---|---|---|---|
Next Message | Stephan Szabo | 2002-04-29 16:46:59 | Re: abnormal size of the on-disk file. |
Previous Message | Joseph Barillari | 2002-04-29 16:07:14 | Re: Temporary table weirdness |