Re: Casting dates
От | Joseph Barillari |
---|---|
Тема | Re: Casting dates |
Дата | |
Msg-id | m3lmb6fpfa.fsf@washer.barillari.org обсуждение исходный текст |
Ответ на | Re: Casting dates ("Josh Berkus" <josh@agliodbs.com>) |
Список | pgsql-sql |
>>>>> "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? >> >> PostgreSQLusually 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-correctresult, >> 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 shouldbe 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 doeshandle INTERVAL JB> division. JB> It's a surmountable challenge, though, but requires somebody JB> to take the lead in a) working out the logic, andb) writing JB> the code to implement it. A) is not a trivial task, either JB> ... while 60 minutes / 1 second isobvious, 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
В списке pgsql-sql по дате отправления: