pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: c_michal@poczta.onet.pl
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15545: wrong calculation
Date: Tue, 11 Dec 2018 10:23:46 -0500
Message-ID: <9241.1544541826@sss.pgh.pa.us> (raw)
In-Reply-To: <15545-96bc1bb99d03b6af@postgresql.org>
References: <15545-96bc1bb99d03b6af@postgresql.org>

=?utf-8?q?PG_Bug_reporting_form?= <noreply@postgresql.org> writes:
> select extract (epoch from age ('2018-01-31'::date,'2013-01-01'::date) ) /
> 86400,'2018-01-31'::date-'2013-01-01'::date
> first column is wrong calculation

No, it isn't; you're just using the feature incorrectly.

If you want to know the number of days between two dates, date subtraction
is the operation to use.  age() has a different purpose, which can be
more clearly seen by looking at its actual result:

select age ('2018-01-31'::date,'2013-01-01'::date);   
       age       
-----------------
 5 years 30 days
(1 row)

For certain operations, that's a very useful representation of the
difference between two dates.  For example you could add the result
to another date to get a similarly-related date:

select '2012-01-01'::date + '5 years 30 days'::interval;
      ?column?       
---------------------
 2017-01-31 00:00:00
(1 row)

You couldn't replicate this by adding a number of days, because
in this example there's a different number of leap days in between.

As for the fractional result from extract(), there's not much
it can really do, because per the above example '5 years 30 days'
doesn't represent a fixed number of seconds.  It's approximating
the result as 365.25 days per year (and ignoring issues like
DST, too).  Personally I'd never use extract(epoch) on an interval
containing day, month, or year fields, because the result isn't
terribly meaningful --- we only offer that calculation because
the SQL spec says we should.

In short, different operators with different purposes may well
yield different answers.  We could wish that the civil calendar
system were less screwy and easier to calculate in, but Postgres
can't fix that :-(

			regards, tom lane




view thread (2+ messages)

Message-ID: <9241.1544541826@sss.pgh.pa.us>
Permalink:  ../9241.1544541826@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9241.1544541826@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, c_michal@poczta.onet.pl
  Subject: Re: BUG #15545: wrong calculation
  In-Reply-To: <9241.1544541826@sss.pgh.pa.us>
  References: <15545-96bc1bb99d03b6af@postgresql.org> <9241.1544541826@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox