From: Steve Crawford <scrawford@pinpointresearch.com>
To: Dan Harris <fbsd@drivefaster.net>
Cc: pgsql-admin@postgresql.org
Subject: Re: Function to offset current timestamp
Date: Fri, 01 Jun 2007 11:24:45 -0700
Message-ID: <4660646D.3070208@pinpointresearch.com> (raw)
In-Reply-To: <46605886.1070404@drivefaster.net>
References: <46605886.1070404@drivefaster.net>
Dan Harris wrote:
> I'm trying to write a function that can do a select on an integer value
> from a table and subtract that value from current_timestamp.
>
> Let's say I have a table called users and a field called tz_offset..
>
> I want my function to do something like :
>
> select current_timestamp - '( select tz_offset from users where userid =
> ? ) hours'::interval
>
> Obviously this syntax is incorrect, but I'm not quite sure how to do the
> subquery and make the integer result of it part of the quoted interval
> expression.
>
> Any tips?
You don't need to make it part of the quoted expression - just multiply
an interval by your integer. Using your version, try something like:
select current_timestamp - '1 hour'::interval * ( select tz_offset from
users where userid = ? )
But this is actually wrong because the returned time is still in your
current timezone - just offset by X hours. If you store this value and
end up displaying it in another timezone you will get unexpected
results. Additionally, it won't give you the desired result in the
vicinity of DST change. If your server is Pacific time and the user is
Eastern, the East-coasters will have jumped forward/backward while your
server hasn't.
Instead, you might switch the timezone to whatever zone is correct for
the specific user.
-- Start transaction to keep timestamps the same
test=# begin;
BEGIN
-- Get current time (server default is PST8PDT - currently -07 hours)
test=# select current_timestamp;
timestamptz
-------------------------------
2007-06-01 10:54:17.627987-07
-- Subtract three hours to get pseudo-east-coast time. Really we got
-- three-hours earlier in Pacific time (still -07).
test=# select current_timestamp - 3 * '1 hour'::interval;
?column?
-------------------------------
2007-06-01 07:54:17.627987-07
-- Set the timezone to Eastern US
test=# set timezone to EST5EDT;
SET
-- Time doesn't change - it is just displayed appropriately
-- for the user (Eastern Daylight Time = -04).
test=# select current_timestamp;
timestamptz
-------------------------------
2007-06-01 13:54:17.627987-04
I'm not quite sure how to change session timezone based on a query. This
doesn't work:
set timezone to (select timezone from users where userid = ?);
Another possibility is to use "at timezone" which can be handled from a
query:
test=# select current_timestamp at timezone (select 'CST6CDT');
timezone
----------------------------
2007-06-01 13:23:59.234811
But beware. This query appears to strip off the timezone information and
return a timestamp without timezone. This may be fine for display
purposes but might not be if you are storing the value somewhere.
Cheers,
Steve
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-admin@postgresql.org
Cc: scrawford@pinpointresearch.com, fbsd@drivefaster.net
Subject: Re: Function to offset current timestamp
In-Reply-To: <4660646D.3070208@pinpointresearch.com>
* 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