Re: skip weekends: revisited

From: Andre Maasikas <andre(dot)maasikas(at)abs(dot)ee>
To: Jeff Amiel <jamiel(at)istreamimaging(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: skip weekends: revisited
Date: 2005-03-14 16:24:09
Message-ID: 4235BAA9.6060900@abs.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-03-14 16:55:45 Re: plpython function problem workaround
Previous Message Thomas F.O'Connell 2005-03-14 16:08:41 Re: Peculiar performance observation....