From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Bruce Momjian <bruce@momjian.us>
Cc: Ron <ronljohnsonjr@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Date calculation
Date: Thu, 31 Jan 2019 21:00:48 +0000
Message-ID: <875zu4a6ox.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <20190131202340.GB3377@momjian.us>
References: <c10b17de-623b-4af3-8a8c-45c4a3a5826d@gmail.com>
<20190131201514.GA3377@momjian.us>
<24e5ee21-0b9f-e3c8-2553-817813a04593@gmail.com>
<20190131202340.GB3377@momjian.us>
>>>>> "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)
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andrew@tao11.riddles.org.uk, bruce@momjian.us, ronljohnsonjr@gmail.com
Subject: Re: Date calculation
In-Reply-To: <875zu4a6ox.fsf@news-spur.riddles.org.uk>
References: <c10b17de-623b-4af3-8a8c-45c4a3a5826d@gmail.com> <20190131201514.GA3377@momjian.us> <24e5ee21-0b9f-e3c8-2553-817813a04593@gmail.com> <20190131202340.GB3377@momjian.us> <875zu4a6ox.fsf@news-spur.riddles.org.uk>
* 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