pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Francisco Olarte <folarte@peoplecall.com>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How to reformat output of "age()" function
Date: Wed, 11 Sep 2019 14:19:12 -0700
Message-ID: <abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com> (raw)
In-Reply-To: <CA+bJJbz0bkX_=k1RNM23aeCvQP+2qJm=pXq67-GyKOJONi-xsg@mail.gmail.com>
References: <CAMBRECCvHC-mzw5F0H20-gB54D0hnMjZud=8X86Y7v61a54ujQ@mail.gmail.com>
	<CA+bJJbz0bkX_=k1RNM23aeCvQP+2qJm=pXq67-GyKOJONi-xsg@mail.gmail.com>

On 9/11/19 9:34 AM, Francisco Olarte wrote:
> David:
> 
> On Wed, Sep 11, 2019 at 5:39 PM David Gauthier <davegauthierpg@gmail.com> wrote:
>> How can I change the default output of the "age" function to be, for example, in minutes?
>> E.g.
>> dvdb=> select age('2019-09-11 09:00:00','2019-09-09 11:00:00');
>>        age
>> ----------------
>>   1 day 22:00:00
>> (1 row)
>> I want the equivalent of that time delta in minutes.
> 
> Some answers have already been posted, but also consider what you are
> doing. Intervals have three components, months, days, seconds for a
> reason, ( ignoring leap seconds for now ) not all days have 24h (
> daylight saving time changes ) and not all months have 28/29/30/31
> days. IIRC interval normalization for epoch assumes all months have 30
> days, all days have 24 hours.
> 
> If you want to know the elapsed minutes between two timestamps, it
> might be better to do it directly, extract the epoch from both (
> seconds ), substract, divide by 60 truncating/rounding if you need to.
> 
> This is what happens in one case on my timezone ( Europe/Madrid ):
> 
> 
> test=# select '2019.11.20 20:00:00'::timestamptz as end, '2019.06.10
> 10:00:00'::timestamptz as start;
>            end           |         start
> ------------------------+------------------------
>   2019-11-20 20:00:00+01 | 2019-06-10 10:00:00+02
> (1 row)
> 
> test=# select age('2019.11.20 20:00:00'::timestamptz, '2019.06.10
> 10:00:00'::timestamptz) as interval;
>          interval
> -------------------------
>   5 mons 10 days 10:00:00
> (1 row)
> 
> test=# select extract(epoch from age('2019.11.20
> 20:00:00'::timestamptz, '2019.06.10 10:00:00'::timestamptz)) as
> interval_seconds;
>   interval_seconds
> ------------------
>           13860000
> (1 row)
> 
> test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz) -
> extract(epoch from '2019.06.10 10:00:00'::timestamptz) as
> elapsed_seconds;
>   elapsed_seconds
> -----------------
>          14122800
> (1 row)

Epoch is not the issue, age() is. Leave age() out of it:

set timezone = 'Europe/Madrid';

test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz) -
test-# extract(epoch from '2019.06.10 10:00:00'::timestamptz) as
test-# elapsed_seconds;
  elapsed_seconds
-----------------
         14122800


test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz 
-'2019.06.10 10:00:00'::timestamptz) as
elapsed_seconds;
  elapsed_seconds
-----------------
         14122800
(1 row)


> 
> Francisco Olarte.
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (10+ messages)  latest in thread

Message-ID: <abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com>
Permalink:  ../abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com/
Also on:    postgresql.org/message-id/abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com

 · 

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-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, folarte@peoplecall.com, davegauthierpg@gmail.com
  Subject: Re: How to reformat output of "age()" function
  In-Reply-To: <abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com>
  References: <CAMBRECCvHC-mzw5F0H20-gB54D0hnMjZud=8X86Y7v61a54ujQ@mail.gmail.com> <CA+bJJbz0bkX_=k1RNM23aeCvQP+2qJm=pXq67-GyKOJONi-xsg@mail.gmail.com> <abfebdbe-da87-7b21-1d56-32340c5c934a@aklaver.com>

* 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