why justify_interval('360 days'::interval) results '1 year'

后端 未结 2 1552
南笙
南笙 2021-01-17 01:36

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

postgres=# select justify_interval(concat(365*4 +1         


        
相关标签:
2条回答
  • 2021-01-17 01:41

    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
    

    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).

    0 讨论(0)
  • 2021-01-17 01:58

    accrding to docs:

    justify_interval(interval) - Adjust interval using justify_days and justify_hours, with additional sign adjustments

    and further:

    justify_days(interval) - Adjust interval so 30-day time periods are represented as months

    So 30*12=360

    Not expected but obviously defined in docs...

    0 讨论(0)
提交回复
热议问题