Postgres has date_trunc
which operates on timestamp
or interval
, and:
Values of type date and time are cast automatically to timestamp or interval, respectively.
In other words we can use date_trunc
for date
values with a cast:
select date_trunc('month',current_date)::date;
┌────────────┐
│ date_trunc │
├────────────┤
│ 2014-12-01 │
└────────────┘
But is this timezone safe—if the current date is in Daylight Saving Time and the start of the month is not or vice versa will the correct date result?
Best Answer
Yes it's safe.
It's not exactly clear in the documentation but
date_trunc
can return eithertimestamp
ortimestamptz
depending on what you pass it. If you pass adate
, it will cast the input totimestamp
and returnstimestamp
so there can be no tz issue when casting back todate
.If you pass a
timestamptz
it does the truncation the way you'd expect and returns atimestamptz
: