Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date with timezone offset

Tags:

I am trying to extract the date from a query in postgres. The timestamp is stored as UTC, so if I have 1/1/2014 02:00:00, I want the date in pacific time, to be 12/31/2013, not 1/1/2014. I am really close, but both query 2 and 3 still return 1/1/2014.

SELECT '1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles'; 

returns

2014-01-01 02:00:00-08 

-

SELECT CAST('1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles' AS Date); 

returns

2014-01-01 

but I want it to return 2013-12-31.

SELECT CAST('1-1-2014 00:02:00'::timestamp at time zone 'America/Los_Angeles' AS Date) at time zone 'America/Los_Angeles'; 

returns

2014-01-01 00:00:00 

but I want it to return 2013-12-31 00:00:00

I basically want to return the date in the timezone it is in, in this case the pacific timezone.

like image 762
WallMobile Avatar asked Apr 10 '14 19:04

WallMobile


People also ask

How do you get timezone from timezone offset?

That is given a timezone I can get the timezone offset by the following code snippet: TimeZone tz = TimeZone. getDefault(); System. out.

How do I get timezone from UTC offset?

Knowing just the offset from UTC, you can't tell what timezone you are in, because of DST. You could consider looking at the time part of the time to try to guess whether DST was in effect then or not, but political considerations make that nearly impossible, as different jurisdictions change the definition of DST.

How do you use timezone offset?

The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).


Video Answer


1 Answers

If it is timestamp without time zone you need first to tell it to use the UTC time zone and then convert to another time zone:

SELECT '1-1-2014 02:00:00'::timestamp at time zone 'UTC' at time zone 'America/Los_Angeles';       timezone        ---------------------  2013-12-31 18:00:00 
like image 107
Clodoaldo Neto Avatar answered Oct 05 '22 03:10

Clodoaldo Neto