pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #15545: wrong calculation
2+ messages / 2 participants
[nested] [flat]

* BUG #15545: wrong calculation
@ 2018-12-11 12:46 PG Bug reporting form <noreply@postgresql.org>
  2018-12-11 15:23 ` Re: BUG #15545: wrong calculation Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Bug reporting form @ 2018-12-11 12:46 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: c_michal@poczta.onet.pl

The following bug has been logged on the website:

Bug reference:      15545
Logged by:          Michal C
Email address:      c_michal@poczta.onet.pl
PostgreSQL version: 10.5
Operating system:   Windows
Description:        

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

Result:
"column1";"column2"
1856.25;1856



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #15545: wrong calculation
  2018-12-11 12:46 BUG #15545: wrong calculation PG Bug reporting form <noreply@postgresql.org>
@ 2018-12-11 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2018-12-11 15:23 UTC (permalink / raw)
  To: c_michal@poczta.onet.pl; +Cc: pgsql-bugs@lists.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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-12-11 15:23 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-11 12:46 BUG #15545: wrong calculation PG Bug reporting form <noreply@postgresql.org>
2018-12-11 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>

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