Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify “the start of today” in a specific time zone?

Tags:

postgresql

I have a table with a “timestamp with time zone” column. I would like to find all of the rows whose timestamp is earlier than today, where “today” is determined in a specific time zone.

I know how to use at time zone to interpret a literal timestamp as being in some particular time zone, and I know how to use date_trunc to get the beginning of this day. But I’m not sure how to combine them to get what I need. I tried

select date_trunc('day', current_date at time zone 'cst');

which gave me “2015-03-16 00:00:00”, but it’s unclear to me what time zone is used for this result (or whether it has one at all). How can I select the beginning of the current day according to a specific time zone?

like image 672
bdesham Avatar asked Mar 17 '15 14:03

bdesham


People also ask

What is timestamp with timezone?

For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.

What is the best time zone to store things like when a record was added or updated?

The widely-recommended solution for storing dates and times is to store the date and time in UTC. This means, whenever you have a user that inserts or updates a datetime value in the database, convert it to UTC and store the UTC value in the database column. Your data will be consistent.


1 Answers

It helped me to reframe the question as follows: what are the current date and time in the Central time zone? Then, what I want is the midnight at the beginning of that day [in the Central time zone]. I found that I could write this as

current_date::timestamp AT TIME ZONE 'cst'
like image 149
bdesham Avatar answered Oct 26 '22 21:10

bdesham