Re: Date calculation - Mailing list pgsql-general

From Andrew Gierth
Subject Re: Date calculation
Date
Msg-id 875zu4a6ox.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Re: Date calculation  (Bruce Momjian <bruce@momjian.us>)
List pgsql-general
>>>>> "Bruce" == Bruce Momjian <bruce@momjian.us> writes:

 Bruce> Oh, right, you want date, so use:

 Bruce>    SELECT date_trunc('week', CURRENT_DATE) + '6 days';

Three major things wrong with this:

1. If you do this on Sunday, it gives you the current day not the _next_
Sunday.

2. If you try and do this for other days of the week it doesn't work at
all, instead giving you the specified day of the current week whether or
not it's before or after the current day.

3. It's letting PG cast the date to a timestamptz, which is inefficient,
possibly incorrect, and mutable; you want to force it to cast to
timestamp without timezone instead. (A good rule of thumb is that you
virtually never want to cast dates to timestamptz; the natural cast from
date is to timestamp _without_ timezone.)

-- 
Andrew (irc:RhodiumToad)


pgsql-general by date:

Previous
From: Andrew Gierth
Date:
Subject: Re: Date calculation
Next
From: Brad Leupen
Date:
Subject: FK Constraint with ON DELETE SET DEFAULT cascading as table owner