pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: David Gauthier <davegauthierpg@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Need to omit time during weekends from age calculations
Date: Mon, 7 Jun 2021 15:34:56 -0400
Message-ID: <ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.com> (raw)
In-Reply-To: <CAMBRECBCO3toxcCXRNxZqxqyYyNKdu7icy6x_B0Ln7vdD6uyQw@mail.gmail.com>
References: <CAMBRECBCO3toxcCXRNxZqxqyYyNKdu7icy6x_B0Ln7vdD6uyQw@mail.gmail.com>

On 6/7/21 3:12 PM, David Gauthier wrote:
> Hi:
> 
> I suspect I'm not the first to ask about this but couldn't find anything 
> after googling for a bit.  So here goes....
> 
> I'd like to get the "age" difference between two times which span either 
> all or part of a weekend but exclude any time that transpired during the 
> weekend.
> Example (please pardon the non-timestamp vals here...)
> 
> age('Monday-Noon','Prev-Friday-Noon')
> would give me '1 day'.
> 
> ...and...
> 
> age('Sunday-Noon','Prev-Friday-Noon')
> would give me '12 hours'
> 
> You get the picture.
> 
> Has this wheel already been invented ?
> I don't see an easy way to do this off-hand.
> All Ears :-)

Perhaps not the prettiest of solutions, but what about something like this?

8<----------------------------------------
CREATE OR REPLACE FUNCTION nonweekendhours(startts timestamptz, endts 
timestamptz)
RETURNS interval AS $$
   SELECT
     (SUM(case when extract(dow from g.ts) > 0
                and extract(dow from g.ts) < 6 then
            1
          else
            0 end) || ' hours')::interval
   FROM generate_series(startts, endts - '1 hour'::interval,'1 hour') AS 
g(ts)
$$ LANGUAGE sql;

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-06 12:00:00');
  nonweekendhours
-----------------
  12:00:00
(1 row)

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-07 12:00:00');
  nonweekendhours
-----------------
  24:00:00
(1 row)

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-11 12:00:00');
  nonweekendhours
-----------------
  120:00:00
(1 row)
8<----------------------------------------

HTH,

Joe
-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development





view thread (7+ messages)  latest in thread

Message-ID: <ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.com>
Permalink:  ../ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.com/
Also on:    postgresql.org/message-id/ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.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: mail@joeconway.com, davegauthierpg@gmail.com
  Subject: Re: Need to omit time during weekends from age calculations
  In-Reply-To: <ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.com>
  References: <CAMBRECBCO3toxcCXRNxZqxqyYyNKdu7icy6x_B0Ln7vdD6uyQw@mail.gmail.com> <ebce6ea1-c872-78a8-34c5-7d74875c2d5e@joeconway.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