Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i2kk2-0001QM-Kk for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2019 23:19:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i2kk0-0006ZN-US for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2019 23:19:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i2kk0-0006ZG-L7 for pgsql-general@lists.postgresql.org; Tue, 27 Aug 2019 23:19:08 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i2kjy-0008JP-1q for pgsql-general@lists.postgresql.org; Tue, 27 Aug 2019 23:19:08 +0000 Received: by mail-pl1-x635.google.com with SMTP id h3so272183pls.7 for ; Tue, 27 Aug 2019 16:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Oz0qKW6OkkDTqqVv0iXG0pbKlyXqqVS8N8EP90wR4mI=; b=jO5Vey2AxqdQAcO3Z1ubtHu8wTUW9WHoB6oLqtsPcsxJBq8mI+0c70V5SwkKGennVS R+3ujba9ShA/KmRiMh0lrVFefKdqKDiMQFBUzhcJ2fTiuvld1x4QA3ahQpBIpaJyzdxg 1QtZqqs0MrKGVu5Vzi2ZCbl8Qamb6U6nzeMCwHx9YCIGr+96LheUNJBF/aMKSeEF0WIb 48vB+q4FAUBaSJ3T7lS2dbD6WNvNlJTOxEt3rJo5DpsEDWSKEZU9lPyEkNVmD493GzdO G2u5E/WI5zzzGcQ1Q22SW1wfWMZFWp3eI/veV6msEcbhcfBrUBS2jB5+NXa//rYoUv1D zD9A== 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-transfer-encoding :content-language; bh=Oz0qKW6OkkDTqqVv0iXG0pbKlyXqqVS8N8EP90wR4mI=; b=L4xpwgpuAxgiiTIuXwHVdIqg+G11gVDYrIw+dsw4g8NXGvowne+ZdyarTUCRAghrLm cy2Bb51943lFIzSBL1Z3e/d7oIoVMGJjk3eYG5nD5GMiEnDSW18yBeclo1BtS4iHJDX+ qnpwP43saeJllOELyhbLijPAJ2kQ2p4xfgIbBvhIivyIyxn91FdA0qy24V+dvIug4oWg VA5uqGsO7+XdmDdbxtMu6k/8wkyM0ZJkhYEV2JpoUaeplrY+Q39ucPZtE9ibaT/Dl+v6 F0HORSdTri/WNwMkOucuDRaBf8BuyAc2wlAlLtkkpdEyzcCsSTWFIyL23DqyuLs4/x1N r9kg== X-Gm-Message-State: APjAAAX/7RyhbmKEZ38EhjNORq4ENnByiVyKftDhSkztG6KqOVZ2srnr 08jMEuVQZZZzDFeOQkNwlQQ2L/mi X-Google-Smtp-Source: APXvYqy1zOlgwpEcRZZRICkdarsIWWIWrLYvAdKF4ryYwRArf0RA3gaH3KkZeNEM5ysG9L5NqQWfsw== X-Received: by 2002:a17:902:f301:: with SMTP id gb1mr1304728plb.249.1566947544488; Tue, 27 Aug 2019 16:12:24 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id ay7sm242694pjb.4.2019.08.27.16.12.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2019 16:12:24 -0700 (PDT) Subject: Re: Work hours? To: pgsql-general@lists.postgresql.org References: <20190827222741.GB19306@panix.com> <499b1af5-5be9-2639-2236-a7329ee5689d@aklaver.com> From: Rob Sargent Message-ID: <97292698-e645-0afc-6d5b-10f14bf0275c@gmail.com> Date: Tue, 27 Aug 2019 17:12:22 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <499b1af5-5be9-2639-2236-a7329ee5689d@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 8/27/19 4:59 PM, Adrian Klaver wrote: > On 8/27/19 3:27 PM, stan wrote: >> I am just starting to explore the power of PostgreSQL's time and date >> functionality. I must say they seem very powerful. >> >> I need to write a function that, given a month, and a year as input >> returns >> the "work hours" in that month. In other words something like >> >> 8 * the count of all days in the range Monday to Friday) within that >> calendar month. >> >> Any thoughts as to the best way to approach this? > > Use generate_series: > > https://www.postgresql.org/docs/11/functions-srf.html > > to generate all the days in the month. > > Loop over the days and use EXTRACT: > > https://www.postgresql.org/docs/11/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT > > > to find the dates with a dow(The day of the week as Sunday (0) to > Saturday (6)) or isodow(The day of the week as Monday (1) to Sunday (7)) > that falls in Mon-Fri and add to counter. > >> Don't you also need a feed from something like google US holidays (assuming OP is stateside) >> > >