You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add functions to do timestamptz arithmetic in a non-default timezone.
Add versions of timestamptz + interval, timestamptz - interval, and
generate_series(timestamptz, ...) in which a timezone can be specified
explicitly instead of defaulting to the TimeZone GUC setting.
The new functions for the first two are named date_add and
date_subtract. This might seem too generic, but we could use
overloading to add additional variants if that seems useful.
Along the way, improve the docs' pretty inadequate explanation
of how timestamptz +- interval works.
Przemysław Sztoch and Gurjeet Singh; cosmetic changes and most of
the docs work by me
Discussion: https://postgr.es/m/01a84551-48dd-1359-bf7e-f6b0203a6bd0@sztoch.pl
@@ -9808,13 +9852,23 @@ SELECT (DATE '2001-10-30', DATE '2001-10-30') OVERLAPS
9808
9852
9809
9853
<para>
9810
9854
When adding an <type>interval</type> value to (or subtracting an
9811
-
<type>interval</type> value from) a <type>timestamp with time zone</type>
9812
-
value, the days component advances or decrements the date of the
9813
-
<type>timestamp with time zone</type> by the indicated number of days,
9814
-
keeping the time of day the same.
9815
-
Across daylight saving time changes (when the session time zone is set to a
9816
-
time zone that recognizes DST), this means <literal>interval '1 day'</literal>
9817
-
does not necessarily equal <literal>interval '24 hours'</literal>.
9855
+
<type>interval</type> value from) a <type>timestamp</type>
9856
+
or <type>timestamp with time zone</type> value, the months, days, and
9857
+
microseconds fields of the <type>interval</type> value are handled in turn.
9858
+
First, a nonzero months field advances or decrements the date of the
9859
+
timestamp by the indicated number of months, keeping the day of month the
9860
+
same unless it would be past the end of the new month, in which case the
9861
+
last day of that month is used. (For example, March 31 plus 1 month
9862
+
becomes April 30, but March 31 plus 2 months becomes May 31.)
9863
+
Then the days field advances or decrements the date of the timestamp by
9864
+
the indicated number of days. In both these steps the local time of day
9865
+
is kept the same. Finally, if there is a nonzero microseconds field, it
9866
+
is added or subtracted literally.
9867
+
When doing arithmetic on a <type>timestamp with time zone</type> value in
9868
+
a time zone that recognizes DST, this means that adding or subtracting
9869
+
(say) <literal>interval '1 day'</literal> does not necessarily have the
9870
+
same result as adding or subtracting <literal>interval '24
9871
+
hours'</literal>.
9818
9872
For example, with the session time zone set
9819
9873
to <literal>America/Denver</literal>:
9820
9874
<screen>
@@ -22017,13 +22071,17 @@ AND
22017
22071
<returnvalue>setof timestamp</returnvalue>
22018
22072
</para>
22019
22073
<para role="func_signature">
22020
-
<function>generate_series</function> ( <parameter>start</parameter> <type>timestamp with time zone</type>, <parameter>stop</parameter> <type>timestamp with time zone</type>, <parameter>step</parameter> <type>interval</type> )
22074
+
<function>generate_series</function> ( <parameter>start</parameter> <type>timestamp with time zone</type>, <parameter>stop</parameter> <type>timestamp with time zone</type>, <parameter>step</parameter> <type>interval</type> <optional>, <parameter>timezone</parameter> <type>text</type> </optional> )
22021
22075
<returnvalue>setof timestamp with time zone</returnvalue>
22022
22076
</para>
22023
22077
<para>
22024
22078
Generates a series of values from <parameter>start</parameter>
22025
22079
to <parameter>stop</parameter>, with a step size
22026
22080
of <parameter>step</parameter>.
22081
+
In the timezone-aware form, times of day and daylight-savings
22082
+
adjustments are computed according to the time zone named by
22083
+
the <parameter>timezone</parameter> argument, or the current
22084
+
<xref linkend="guc-timezone"/> setting if that is omitted.
22027
22085
</para></entry>
22028
22086
</row>
22029
22087
</tbody>
@@ -22091,6 +22149,25 @@ SELECT * FROM generate_series('2008-03-01 00:00'::timestamp,
22091
22149
2008-03-03 22:00:00
22092
22150
2008-03-04 08:00:00
22093
22151
(9 rows)
22152
+
22153
+
-- this example assumes that TimeZone is set to UTC; note the DST transition:
22154
+
SELECT * FROM generate_series('2001-10-22 00:00 -04:00'::timestamptz,
0 commit comments