When adding an interval value to (or subtracting an
interval value from) a timestamp with time zone
- value, the days component advances (or decrements) the date of the
+ value, the days component advances or decrements the date of the
timestamp with time zone by the indicated number of days.
- Across daylight saving time changes (with the session time zone set to a
+ Across daylight saving time changes (when the session time zone is set to a
time zone that recognizes DST), this means interval '1 day'
does not necessarily equal interval '24 hours'.
For example, with the session time zone set to CST7CDT,
- Note there can be ambiguity in the months> returned by
- age> because different months have a different number of
+ Note there can be ambiguity in the months> field returned by
+ age> because different months have different numbers of
days.
PostgreSQL>'s approach uses the month from the
earlier of the two dates when calculating partial months. For example,
age('2004-06-01', '2004-04-30')> uses April to yield
- Subtraction of dates and timestamps can also be complex. The most
- accurate way to perform subtraction is to convert each value to a number
- of seconds using EXTRACT(EPOCH FROM ...)> and compute the
+ Subtraction of dates and timestamps can also be complex. One conceptually
+ simple way to perform subtraction is to convert each value to a number
+ of seconds using EXTRACT(EPOCH FROM ...)>, then subtract the
+ results; this produces the
number of seconds> between the two values. This will adjust
for the number of days in each month, timezone changes, and daylight
- saving time adjustments. Operator subtraction of date or timestamp
- values returns the number of days (24-hours) and hours/minutes/seconds
+ saving time adjustments. Subtraction of date or timestamp
+ values with the ->
operator
+ returns the number of days (24-hours) and hours/minutes/seconds
between the values, making the same adjustments. The age>
function returns years, months, days, and hours/minutes/seconds,
performing field-by-field subtraction and then adjusting for negative
- field values. The following queries, produced with timezone
- = 'US/Eastern'> and including a daylight saving time change,
- illustrates these issues:
+ field values. The following queries illustrate the differences in these
+ approaches. The sample results were produced with timezone
+ = 'US/Eastern'>; there is a daylight saving time change between the
+ two dates used:
EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00');
Result: 10537200
SELECT (EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') -
- EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00'))
+ EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00'))
/ 60 / 60 / 24;
Result: 121.958333333333
SELECT timestamptz '2013-07-01 12:00:00' - timestamptz '2013-03-01 12:00:00';