timezone aware date_trunc function

Jay picture Jay · Jun 4, 2014 · Viewed 12.6k times · Source

The following query

SELECT the_date FROM date_trunc('day', timestamp with time zone 
       '2001-01-1 00:00:00+0100') as the_date

results to

the_date
2000-12-31 00:00

Is there a way to tell date_trunc to do day/month/year conversions based on the timezone it is feeded with?

The expected output would be: 2001-01-1 00:00+0100

Answer

Clodoaldo Neto picture Clodoaldo Neto · Jun 4, 2014

You need to specify at which time zone you want it to show

select
    date_trunc(
        'day',
        timestamp with time zone '2001-01-1 00:00:00+0100' at time zone '-02'
    ) as the_date;
      the_date       
---------------------
 2001-01-01 00:00:00

AT TIME ZONE