为什么justify_interval('360 days':: interval)结果为'1 year' [英] why justify_interval('360 days'::interval) results '1 year'

查看:278
本文介绍了为什么justify_interval('360 days':: interval)结果为'1 year'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因 justify_interval(now()-'2013-02-14':: timestamptz)会产生奇怪的结果:

For some reason justify_interval(now() - '2013-02-14'::timestamptz) produces weird results:

postgres=# select justify_interval(concat(365*4 +1,' days')::interval);                       -[ RECORD 1 ]----+----------------
justify_interval | 4 years 21 days

我检查了一年:

postgres=# select justify_interval('365 days'::interval);
 justify_interval
------------------
 1 year 5 days

所以我走得更远:

postgres=# select justify_interval('360 days'::interval);
 justify_interval
------------------
 1 year
(1 row)

此行为不是特定于平台的(尝试了几种Linux,9.2、9.3、9.6)

This behavior is not platform specific (tried several Linuxes, 9.2, 9.3, 9.6)

为什么一年是360天?..

Why one year is 360 days?..

推荐答案

似乎您正在寻找某种东西,而PostgreSQL称之为使用几年和几个月而不是仅仅几天的符号结果,这就是 age(timestamp,timestamp)(和 age(timestamp))函数返回。

It seems that you are looking for something, which PostgreSQL calls a "symbolic" result that uses years and months, rather than just days, which is what the age(timestamp, timestamp) (and age(timestamp)) function(s) returns.

select age(now(), '2013-02-14');    -- 4 years 16:41:02.571547
select age(timestamp '2013-02-14'); -- 4 years

-运算符始终返回天数差异(最多)。 justify _ *()函数(以及 * / < > 运算符)总是将值切为平均值(即 1天 24小时 1个月 30天),尽管事实上1天实际上可以包含23-25个小时(仅考虑夏时制),而1个月可以包含28-31天(因此结果取决于实际开始时间和范围的端点,这将创建间隔)。

The - operator always returns the difference in days (at most). The justify_*() functions (and the *, /, <, > operators) always "cut" values to an average (i.e. 1 day is 24 hours and 1 month is 30 days) despite the fact that 1 day actually can contain 23-25 hours (just think of daylight saving time zones) and 1 month can contain 28-31 days (so the result depends on the actual start and end points of the range, which creates the interval).

这篇关于为什么justify_interval('360 days':: interval)结果为'1 year'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆