pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
timestamp - timestamp result
3+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2020-06-26 eca08f58d0 098868b576  Doc: explain that "timestamp - timestamp" applies justify_hours().
* timestamp - timestamp result
@ 2020-06-26 05:28 Thomas Kellerer <shammat@gmx.net>
  2020-06-26 05:49 ` Re: timestamp - timestamp result Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Thomas Kellerer @ 2020-06-26 05:28 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I regularly see people suggesting to use

    extract(day from one_timestamp - other_timestamp)

to calculate the difference between two timestamps in days.

But I wonder if the "format" of the resulting interval is guaranteed to only have days
(and not months or years)

The following:

     timestamp '2020-06-26 17:00:00' - timestamp '2019-04-01 14:00:00'

returns an interval like this:

     0 years 0 mons 452 days 3 hours 0 mins 0.0 secs

However, is there ever a chance that the expression will yield the (equivalent) interval:

     1 years 2 mons 25 days 3 hours 0 mins 0.0 secs

e.g. as the age() function does.

Is it safe to assume that "timestamp - timestamp" will never contain units larger then days?


Thomas






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

* Re: timestamp - timestamp result
  2020-06-26 05:28 timestamp - timestamp result Thomas Kellerer <shammat@gmx.net>
@ 2020-06-26 05:49 ` Pavel Stehule <pavel.stehule@gmail.com>
  2020-06-26 16:53   ` Re: timestamp - timestamp result Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Pavel Stehule @ 2020-06-26 05:49 UTC (permalink / raw)
  To: Thomas Kellerer <shammat@gmx.net>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

pá 26. 6. 2020 v 7:29 odesílatel Thomas Kellerer <shammat@gmx.net> napsal:

> I regularly see people suggesting to use
>
>     extract(day from one_timestamp - other_timestamp)
>
> to calculate the difference between two timestamps in days.
>
> But I wonder if the "format" of the resulting interval is guaranteed to
> only have days
> (and not months or years)
>
> The following:
>
>      timestamp '2020-06-26 17:00:00' - timestamp '2019-04-01 14:00:00'
>
> returns an interval like this:
>
>      0 years 0 mons 452 days 3 hours 0 mins 0.0 secs
>
> However, is there ever a chance that the expression will yield the
> (equivalent) interval:
>
>      1 years 2 mons 25 days 3 hours 0 mins 0.0 secs
>

postgres=# select age(timestamp '2020-06-26 17:00:00',timestamp '2019-04-01
14:00:00');
┌────────────────────────────────┐
│              age               │
╞════════════════════════════════╡
│ 1 year 2 mons 25 days 03:00:00 │
└────────────────────────────────┘
(1 row)


>
> e.g. as the age() function does.
>
> Is it safe to assume that "timestamp - timestamp" will never contain units
> larger then days?
>

Now, this operator internally calls only interval_justify_hours functions.
So if somebody doesn't change related code, you can expect so only days,
hours field's are changed.

Regards

Pavel




>
>
> Thomas
>
>
>
>


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

* Re: timestamp - timestamp result
  2020-06-26 05:28 timestamp - timestamp result Thomas Kellerer <shammat@gmx.net>
  2020-06-26 05:49 ` Re: timestamp - timestamp result Pavel Stehule <pavel.stehule@gmail.com>
@ 2020-06-26 16:53   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2020-06-26 16:53 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general <pgsql-general@lists.postgresql.org>

Pavel Stehule <pavel.stehule@gmail.com> writes:
> pá 26. 6. 2020 v 7:29 odesílatel Thomas Kellerer <shammat@gmx.net> napsal:
>> Is it safe to assume that "timestamp - timestamp" will never contain units
>> larger then days?

> Now, this operator internally calls only interval_justify_hours functions.

We should probably document that --- I'll go do so.

			regards, tom lane





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


end of thread, other threads:[~2020-06-26 16:53 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-06-26 05:28 timestamp - timestamp result Thomas Kellerer <shammat@gmx.net>
2020-06-26 05:49 ` Pavel Stehule <pavel.stehule@gmail.com>
2020-06-26 16:53   ` 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