[英]Different results when subtracting two timestamps using AGE (PostgreSQL)
Can anyone help me to understand why these are showing different outputs?谁能帮我理解为什么这些显示不同的输出?
SELECT
EXTRACT (epoch FROM ('2021-02-01 00:00:00'::timestamp - '2021-01-01 00:00:00'::timestamp)) / 3600 / 24 as time1,
EXTRACT (epoch FROM age('2021-02-01 00:00:00'::timestamp , '2021-01-01 00:00:00'::timestamp)) / 3600 / 24 as time2
output: output:
time1![]() |
time2![]() |
---|---|
31 ![]() |
30 ![]() |
There is a difference between the subtract operator and the function age(),
perthe documentation: : 根据文档,减法运算符和 function
age(),
之间存在差异::
timestamp - timestamp › interval
timestamp - 时间戳 › 时间间隔
Subtract timestamps (converting 24-hour intervals into days, similarly to justify_hours())
减去时间戳(将 24 小时间隔转换为天数,类似于 justify_hours())
versus相对
age ( timestamp, timestamp ) › interval
年龄 (时间戳, 时间戳) › 时间间隔
Subtract arguments, producing a “symbolic” result that uses years and months, rather than just days
减去 arguments,产生一个使用年和月的“符号”结果,而不仅仅是天
Example:例子:
SELECT
'2021-02-01 00:00:00'::timestamp - '2021-01-01 00:00:00'::timestamp as interval1,
age('2021-02-01 00:00:00'::timestamp , '2021-01-01 00:00:00'::timestamp) as interval2
interval1 | interval2
-----------+-----------
31 days | 1 mon
(1 row)
The second interval is converted to 30 days in the further calculations described in the question.在问题中描述的进一步计算中,第二个间隔转换为 30 天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.