Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lqL1e-0006R0-TA for pgsql-general@arkaria.postgresql.org; Mon, 07 Jun 2021 19:35:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lqL1d-0008K8-NQ for pgsql-general@arkaria.postgresql.org; Mon, 07 Jun 2021 19:35:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lqL1d-0008Ju-87 for pgsql-general@lists.postgresql.org; Mon, 07 Jun 2021 19:35:05 +0000 Received: from mail-qv1-xf34.google.com ([2607:f8b0:4864:20::f34]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lqL1Y-0000w5-9Z for pgsql-general@postgresql.org; Mon, 07 Jun 2021 19:35:04 +0000 Received: by mail-qv1-xf34.google.com with SMTP id hf3so3624678qvb.1 for ; Mon, 07 Jun 2021 12:34:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=gEmVjkhhunfxc8iFSQjQE6nqdQCi21IasttN4cRc2Es=; b=nxJgBTT7YOrPKbBBIaIaIqB8pQcewEcQDZZdVS5TFNRQXlDrU7CC/hnMElNdAWaN44 LulisYFEfcT4guAxHOuDmmZ2myvy3p8wmHx6OL+qBTiMXbnF8AXmfmJxIOWrEjGFlot8 AnnDcz/mlL3yAxq2ofNL+AC04dKMctmxvAneYBazO20v1chSKO+vq9i454tZAWaGdkqj YDo77TNwxMnAcg7NgycpmK82hH/enIrz3qmColrvTenPJcvLFnIxxKF6VMM8AaCmz0n2 No82wUmhe/DKmY8g++sBtXR4Yx8JZtUhyywDwKnaXUTzlysDXRG4isxPGEnMD7rb65CU Iugw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=gEmVjkhhunfxc8iFSQjQE6nqdQCi21IasttN4cRc2Es=; b=iCtMyczWnKenysCvEigYDNbSpTW+rsiMWtANg5urFDC/f6/3aaw7gv58KVFA/VUlOo xDe08B9EwqDMRnUrcGUgp3hsfDFLABl3ZcNP+ZaBRX+Wd9CNT6pwLKbx0BgG9YH4UX7h +BTciCqHyXpDOndCr+FaGKBNEo3UoPmEPBrTVGx2vqhB5xhOvQWgA1LteK5uwHNfCe6+ 0Zt+3p08Cr16zPbkpUaXMiV7L9L0FAAHhYrmr92RBRu5osH27PjbM34qK8nDTOwC8qMv 4laF7oNI+I4NtPyA2ruIzBwCWE2End7suz5ElX84XBE7DglCgU4TktrVHQsBB6DLI9uK Zciw== X-Gm-Message-State: AOAM530FIdpIFOJfWzdSBRBDpyEkScUGfVybNq6/gi7NEpsSZSRyoVc5 oIIygJUduG7RCV57rFo8o8uLiXwLPgNmG/funblgHC9Ci8EO/8rrDknYr51DcwtoIMSLF8ydDGl MgFXvboG4N64WAcvLpWNq+hBvnFupZGHvX+qAsC/jD/TfpVaEFbaF2zAnAE9pPVbSvEnsnqDGMq qReog= X-Google-Smtp-Source: ABdhPJyh65PydY0CFni4lWrUkCqH/+K8UQcYQ2I907Ac9toiNWQS8K5o+4g308/oy298yAg4es9Szg== X-Received: by 2002:ad4:4c51:: with SMTP id cs17mr19856136qvb.57.1623094497969; Mon, 07 Jun 2021 12:34:57 -0700 (PDT) Received: from [192.168.4.41] (072-017-018-098.res.spectrum.com. [72.17.18.98]) by smtp.gmail.com with ESMTPSA id b2sm8697023qke.63.2021.06.07.12.34.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Jun 2021 12:34:57 -0700 (PDT) Subject: Re: Need to omit time during weekends from age calculations To: David Gauthier , Postgres General References: From: Joe Conway Message-ID: Date: Mon, 7 Jun 2021 15:34:56 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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