简体   繁体   English

使用 AGE (PostgreSQL) 减去两个时间戳时的不同结果

[英]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时间1 time2时间2
31 31 30 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM