From 600b04d6b5ef6c9ad3ea684aad40260bd60d5872 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 14 Nov 2018 15:41:07 -0500 Subject: Add a timezone-specific variant of date_trunc(). date_trunc(field, timestamptz, zone_name) performs truncation using the named time zone as reference, rather than working in the session time zone as is the default behavior. It's equivalent to date_trunc(field, timestamptz at time zone zone_name) at time zone zone_name but it's faster, easier to type, and arguably easier to understand. Vik Fearing and Tom Lane Discussion: https://postgr.es/m/6249ffc4-2b22-4c1b-4e7d-7af84fedd7c6@2ndquadrant.com --- doc/src/sgml/func.sgml | 53 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 1678c8cbac9..edeb3fd07bd 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -7179,16 +7179,25 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); date_trunc(text, timestamp) timestamp - Truncate to specified precision; see also + Truncate to specified precision; see date_trunc('hour', timestamp '2001-02-16 20:38:40') 2001-02-16 20:00:00 + + date_trunc(text, timestamp with time zone, text) + timestamp with time zone + Truncate to specified precision in the specified time zone; see + + date_trunc('day', timestamptz '2001-02-16 20:38:40+00', 'Australia/Sydney') + 2001-02-16 13:00:00+00 + + date_trunc(text, interval) interval - Truncate to specified precision; see also + Truncate to specified precision; see date_trunc('hour', interval '2 days 3 hours 40 minutes') 2 days 03:00:00 @@ -8078,17 +8087,19 @@ SELECT date_part('hour', INTERVAL '4 hours 3 minutes'); -date_trunc('field', source) +date_trunc(field, source [, time_zone ]) source is a value expression of type - timestamp or interval. + timestamp, timestamp with time zone, + or interval. (Values of type date and time are cast automatically to timestamp or interval, respectively.) field selects to which precision to - truncate the input value. The return value is of type - timestamp or interval - with all fields that are less significant than the + truncate the input value. The return value is likewise of type + timestamp, timestamp with time zone, + or interval, + and it has all fields that are less significant than the selected one set to zero (or one, for day and month). @@ -8112,13 +8123,39 @@ date_trunc('field', source - Examples: + When the input value is of type timestamp with time zone, + the truncation is performed with respect to a particular time zone; + for example, truncation to day produces a value that + is midnight in that zone. By default, truncation is done with respect + to the current setting, but the + optional time_zone argument can be provided + to specify a different time zone. The time zone name can be specified + in any of the ways described in . + + + + A time zone cannot be specified when processing timestamp without + time zone or interval inputs. These are always + taken at face value. + + + + Examples (assuming the local time zone is America/New_York): SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40'); Result: 2001-02-16 20:00:00 SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40'); Result: 2001-01-01 00:00:00 + +SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00'); +Result: 2001-02-16 00:00:00-05 + +SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney'); +Result: 2001-02-16 08:00:00-05 + +SELECT date_trunc('hour', INTERVAL '3 days 02:47:33'); +Result: 3 days 02:00:00 -- cgit v1.2.3