Re: counting days

From: Ragnar <gnari(at)hive(dot)is>
To: garry saddington <garry(at)schoolteachers(dot)co(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: counting days
Date: 2006-08-29 22:57:07
Message-ID: 1156892227.5167.17.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On þri, 2006-08-29 at 19:35 +0100, garry saddington wrote:
> I need to count the days between two dates that are not saturdays or
> sundays. I have read the manual and searched the lists but I am
> struggling. I can count the days but am finding difficulty excluding sat
> and sun from the count. I need this without reference to any tables.
> Does anyone have any pointers please.

how about something like:

# assuming d id number of days
# and dow is day of week of first day (0-7;sun=0)

wholeweeks=days div 7
partial=days mod 7

# adjust dow to mon=0,tue=1...sun=6
dow=(dow+6) mod 7

# count 5 weekdays for each whole week
wd=5*wholeweeks

# add all days of partial week
wd=wd+partial

# substract 1 if saturday was included
if dow+partial>=6 then wd=wd-1

# substract 1 if sunday was included
if dow+partial>=7 then wd=wd-1

# now wd is result

hope this helps
gnari

In response to

Browse pgsql-general by date

  From Date Subject
Next Message CSN 2006-08-29 23:23:48 Re: database files are incompatible with server, after computer restart
Previous Message Ron Johnson 2006-08-29 22:48:35 Re: strange sum behaviour