Re: skip weekends: revisited - Mailing list pgsql-general

From Andre Maasikas
Subject Re: skip weekends: revisited
Date
Msg-id 4235BAA9.6060900@abs.ee
Whole thread Raw
In response to skip weekends: revisited  (Jeff Amiel <jamiel@istreamimaging.com>)
Responses Re: skip weekends: revisited
List pgsql-general
Jeff Amiel wrote:
> Back in 2002, Jean-Luc Lachance gave a nifty algorithm
> (http://archives.postgresql.org/pgsql-sql/2002-06/msg00301.php) for
> determining the resulting date given a starting date and number of
> 'workdays' in the future.  The trick was that weekends (Saturday and
> Sunday) could not be counted.
>
> The algorithm looks like this:
>
> date := now - day_of_the_week
> interval := interval + day_of_the_week
> date := date + int( interval/5)x7 + ( interval mod 5)
>
> However, when I attempted to implement it, I found the answers I
> received to be problematic.
> I'm sure the issue is in my interpretation of the algorithm, but I can't
> quite figure it out.
> Let's take an example.  Starting on March 11, 2005, what date is 4
> 'workdays' in the future?
>
> Step one:
> date := now - day_of_the_week
> March 5th = March 11th - 6   (6 is the day of week for Friday....march
> 11th).
...

> What am I doing/interpreting wrong?
>
 From the looks of it, variable date seems to calculate
the first day of a week and for it to work day_of_the_week
has to start from 0 so Sunday - 0,  Friday - 5
(in US at least?)

Andre



pgsql-general by date:

Previous
From: Thomas F.O'Connell
Date:
Subject: Re: Peculiar performance observation....
Next
From: Michael Fuhr
Date:
Subject: Re: plpython function problem workaround