From: Joseph Barillari <jbarilla@princeton.edu>
To: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Casting dates
Date: Mon, 29 Apr 2002 12:19:21 -0400
Message-ID: <m3lmb6fpfa.fsf@washer.barillari.org> (raw)
In-Reply-To: <web-1387712@davinci.ethosmedia.com>
References: <web-1387712@davinci.ethosmedia.com>
>>>>> "JB" == Josh Berkus <josh@agliodbs.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jbarilla@princeton.edu, josh@agliodbs.com
Subject: Re: Casting dates
In-Reply-To: <m3lmb6fpfa.fsf@washer.barillari.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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