Re: Obtaining the Julian Day from a date

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Karl O(dot) Pinc" <kop(at)meme(dot)com>
Cc: Bruno Wolff III <bruno(at)wolff(dot)to>, pgsql-general(at)postgresql(dot)org
Subject: Re: Obtaining the Julian Day from a date
Date: 2004-09-11 15:33:47
Message-ID: 10750.1094916827@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Karl O. Pinc" <kop(at)meme(dot)com> writes:
> On 2004.09.10 20:32 Bruno Wolff III wrote:
>> If you keep your data in a date field you can get the Julian day
>> by subtracting the appropiate date. You can then do mod on this
>> difference.

> I've been doing:
> CAST (to_char(date, 'J') AS INT)
> but your way seems faster. Is it?

Date subtraction is extremely fast (it's really the same as integer
subtraction), so yes I'd expect it to beat the pants off doing to_char
and then conversion back to integer.

Another advantage is that you can equally easily adopt *any* base date,
it doesn't have to be Julian day 0. This would let you shift between
say Monday and Sunday as start-of-the-week without extra logic.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Josué Maldonado 2004-09-11 17:18:01 pass an array as parameter to a function
Previous Message Tom Lane 2004-09-11 15:27:02 Re: What is the postgres version of mysql's "ON DUPLICATE KEY"