Finding the date of an event based on related events.

Discussion in 'Ruby' started by Kaldrenon, Jul 18, 2008.

  1. Kaldrenon

    Kaldrenon Guest

    Hi all,

    I'm doing some planning to write a simple utility program for
    maintaining my schedule. The basic scenario is that, in my job, I have
    a series of multi-stage procedures to perform, and for each instance
    of the procedure, the stages need to happen a fixed number of business
    days apart (i.e, if step one happens on Monday, step two is Friday,
    but if step one is Tuesday, step two will be the following Monday). As
    one of the first steps of the program, I'm going to write a script to
    quickly spit out the calendar dates of all steps given the date of the
    first and the relative date (in business days) of all the others.

    My question is, can you help me drum up a 'smart' way to take weekends
    and holidays into account? I know that with Ruby's Date class you can
    use the - (minus) operator to subtract x days from the date of an
    existing event. which is how I can find the exact date once I've
    determined the number of total days it will be before it has been x
    business days.

    But the "human" solution to the problem isn't very efficient -
    iterating over the days with a counter, checking Date.wday, ignoring
    days that return 0 and 6, and spitting back the date at which the
    counter reaches x. I'd like to find a more elegant and computationally
    efficient solution.

    If you've worked out this problem before, or a better solution seems
    readily available to you, I'd love input. If my explanation was
    unclear, I'll gladly clarify anything.

    -Andrew Fallows
     
    Kaldrenon, Jul 18, 2008
    #1
    1. Advertising

  2. Kaldrenon wrote:
    > Hi all,
    >
    > I'm doing some planning to write a simple utility program for
    > maintaining my schedule. The basic scenario is that, in my job, I have
    > a series of multi-stage procedures to perform, and for each instance
    > of the procedure, the stages need to happen a fixed number of business
    > days apart (i.e, if step one happens on Monday, step two is Friday,
    > but if step one is Tuesday, step two will be the following Monday). As
    > one of the first steps of the program, I'm going to write a script to
    > quickly spit out the calendar dates of all steps given the date of the
    > first and the relative date (in business days) of all the others.
    >
    > My question is, can you help me drum up a 'smart' way to take weekends
    > and holidays into account?

    (...)
    >
    > -Andrew Fallows


    To exclude just the weekends you could do

    class Date
    def add_workdays(n)
    days_to_add =((self.cwday+n-1)/5)*2 +n
    self + days_to_add
    end
    end

    Which returns a new Date and seems to work with negative n's. (Just
    wrote this code, don't know if it's bullet proof).

    Holidays are a complication.Ideally the solution would allow for
    different holidays for different countries. Working hours could be
    specified, to enable calculating exactly when a 14 hour job starting on
    friday 24 december 15:15 in the US would finish.

    Two applications I work with have these features. I'm not aware of a
    ruby solution/gem.

    Regards,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Jul 18, 2008
    #2
    1. Advertising

  3. Kaldrenon

    Todd Benson Guest

    On Fri, Jul 18, 2008 at 5:04 PM, Siep Korteling <> wrote:
    > To exclude just the weekends you could do
    >
    > class Date
    > def add_workdays(n)
    > days_to_add =((self.cwday+n-1)/5)*2 +n
    > self + days_to_add
    > end
    > end
    >
    > Which returns a new Date and seems to work with negative n's. (Just
    > wrote this code, don't know if it's bullet proof).


    It isn't. It won't work if you start from a weekend day.

    Use today (Saturday), and do

    d = Date.today.add_workdays(5)
    puts d

    => 2008-07-28

    I like the approach, though.

    Todd
     
    Todd Benson, Jul 19, 2008
    #3
  4. Kaldrenon

    Axel Etzold Guest

    -------- Original-Nachricht --------
    > Datum: Sat, 19 Jul 2008 01:20:12 +0900
    > Von: Kaldrenon <>
    > An:
    > Betreff: Finding the date of an event based on related events.


    > Hi all,
    >
    > I'm doing some planning to write a simple utility program for
    > maintaining my schedule. The basic scenario is that, in my job, I have
    > a series of multi-stage procedures to perform, and for each instance
    > of the procedure, the stages need to happen a fixed number of business
    > days apart (i.e, if step one happens on Monday, step two is Friday,
    > but if step one is Tuesday, step two will be the following Monday). As
    > one of the first steps of the program, I'm going to write a script to
    > quickly spit out the calendar dates of all steps given the date of the
    > first and the relative date (in business days) of all the others.
    >
    > My question is, can you help me drum up a 'smart' way to take weekends
    > and holidays into account? I know that with Ruby's Date class you can
    > use the - (minus) operator to subtract x days from the date of an
    > existing event. which is how I can find the exact date once I've
    > determined the number of total days it will be before it has been x
    > business days.
    >
    > But the "human" solution to the problem isn't very efficient -
    > iterating over the days with a counter, checking Date.wday, ignoring
    > days that return 0 and 6, and spitting back the date at which the
    > counter reaches x. I'd like to find a more elegant and computationally
    > efficient solution.
    >
    > If you've worked out this problem before, or a better solution seems
    > readily available to you, I'd love input. If my explanation was
    > unclear, I'll gladly clarify anything.
    >
    > -Andrew Fallows


    Dear Andrew,

    reading previous responses to your post, I'd like to mention that you can get a list of public holidays from the Wikipedia

    http://en.wikipedia.org/wiki/List_of_holidays_by_country,

    for many countries, the movable holidays are mostly Easter and Pentecost,
    there is a listing of the Easter dates here (and Pentecost is, I believe, just 7 weeks later):

    http://www.assa.org.au/edm.html#List20

    and, secondly, that for the main task you have -- scheduling --- you could use a "Constraint Programming" framework,
    such as this:

    http://gecoder.rubyforge.org/

    where you input all the conditions you have and get back a solution...

    Best regards,

    Axel

    --
    Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
    Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
     
    Axel Etzold, Jul 19, 2008
    #4
  5. Kaldrenon

    Todd Benson Guest

    On Fri, Jul 18, 2008 at 11:20 AM, Kaldrenon <> wrote:
    > Hi all,
    >
    > I'm doing some planning to write a simple utility program for
    > maintaining my schedule. The basic scenario is that, in my job, I have
    > a series of multi-stage procedures to perform, and for each instance
    > of the procedure, the stages need to happen a fixed number of business
    > days apart (i.e, if step one happens on Monday, step two is Friday,
    > but if step one is Tuesday, step two will be the following Monday). As
    > one of the first steps of the program, I'm going to write a script to
    > quickly spit out the calendar dates of all steps given the date of the
    > first and the relative date (in business days) of all the others.
    >
    > My question is, can you help me drum up a 'smart' way to take weekends
    > and holidays into account? I know that with Ruby's Date class you can
    > use the - (minus) operator to subtract x days from the date of an
    > existing event. which is how I can find the exact date once I've
    > determined the number of total days it will be before it has been x
    > business days.
    >
    > But the "human" solution to the problem isn't very efficient -
    > iterating over the days with a counter, checking Date.wday, ignoring
    > days that return 0 and 6, and spitting back the date at which the
    > counter reaches x. I'd like to find a more elegant and computationally
    > efficient solution.
    >
    > If you've worked out this problem before, or a better solution seems
    > readily available to you, I'd love input. If my explanation was
    > unclear, I'll gladly clarify anything.
    >
    > -Andrew Fallows


    Maybe build a calendar beforehand that only includes work days...

    w_days = [nil] + (Date.today..Date.today +
    some_number_of_days).map.select {|day| (day.cwday / 5).zero?}

    ...and then later...

    w_days -= holiday_dates #which is an array of holiday Date objects chosen by you

    ...which afterwards you could walk through 6 at a time.

    That, to me is somewhat elegant and robust, but Siep's idea will work
    if the script/program will always run from a weekday. You will notice
    the peculiarity that Saturday acts like Monday and Sunday acts like
    Tuesday with Siep's example.

    I'd probably create a WorkCalendar class, though.

    Todd
     
    Todd Benson, Jul 19, 2008
    #5
  6. Kaldrenon

    Todd Benson Guest

    On Sat, Jul 19, 2008 at 1:13 PM, Todd Benson <> wrote:
    > w_days = [nil] + (Date.today..Date.today +
    > some_number_of_days).map.select {|day| (day.cwday / 5).zero?}


    That should be a 6 not a 5.

    Todd
     
    Todd Benson, Jul 19, 2008
    #6
  7. Kaldrenon

    Guest

    Patch failure for Ruby18-gems-1.2.0 on amd64

    Following error message:
    ===> Patching for ruby18-gems-1.2.0
    ===> ruby18-gems-1.2.0 depends on file: /usr/local/bin/ruby18 - found
    ===> Applying FreeBSD patches for ruby18-gems-1.2.0
    => Patch patch-lib-rubygems-source_info_cache.rb failed to apply cleanly.
    *** Error code 1

    Stop in /usr/ports/devel/ruby-gems

    FreeBSD ***.vizion2000.net 7.0-STABLE FreeBSD 7.0-STABLE #0: Wed Jul 16
    09:27:38 PDT 2008 @***.vizion2000.net:/usr/obj/usr/src/sys/GENERIC amd64

    This system has just been upgraded from 6.3 to 7.0-Stable. Is there a
    possible connection here?

    After getting this error I checked to make sure the port tree was uptofate
    and tried again with the same result.
    I am currently recompiling ruby* ports on the system after a portsclean
    with:
    portupgrade -fr ruby*
    Has anyone else experienced a similar problem?
    I will report result of recompile when it finishes.

    David
     
    , Jul 21, 2008
    #7
  8. Re: Patch failure for Ruby18-gems-1.2.0 on amd64


    > -----Original Message-----
    > From:
    > [mailto:eek:] On Behalf Of
    >
    > Sent: 21 July 2008 02:48
    > To:
    > Cc:
    > Subject: Patch failure for Ruby18-gems-1.2.0 on amd64
    >
    > Following error message:
    > ===> Patching for ruby18-gems-1.2.0
    > ===> ruby18-gems-1.2.0 depends on file:
    > /usr/local/bin/ruby18 - found
    > ===> Applying FreeBSD patches for ruby18-gems-1.2.0 => Patch
    > patch-lib-rubygems-source_info_cache.rb failed to apply cleanly.
    > *** Error code 1
    >
    > Stop in /usr/ports/devel/ruby-gems
    >
    > FreeBSD ***.vizion2000.net 7.0-STABLE FreeBSD 7.0-STABLE #0:
    > Wed Jul 16
    > 09:27:38 PDT 2008
    > @***.vizion2000.net:/usr/obj/usr/src/sys/GENERIC amd64
    >
    > This system has just been upgraded from 6.3 to 7.0-Stable. Is
    > there a possible connection here?
    >
    > After getting this error I checked to make sure the port tree
    > was uptofate and tried again with the same result.
    > I am currently recompiling ruby* ports on the system after a
    > portsclean
    > with:
    > portupgrade -fr ruby*
    > Has anyone else experienced a similar problem?
    > I will report result of recompile when it finishes.
    >
    > David


    The recompile produced the same result for ruby-gems.
    Is a pr needed here or is someone on to it already?
    David
     
    David Southwell, Jul 21, 2008
    #8
  9. Kaldrenon

    Kaldrenon Guest

    On Jul 19, 2:08 pm, Todd Benson <> wrote:
    > That, to me is somewhat elegant and robust, but Siep's idea will work
    > if the script/program will always run from a weekday.  


    The general idea is that it will, given that the input will always be
    an event that the user is scheduling, and the context is scheduling
    for a M-F 5-day workweek setting. However, in the interest of
    versatility, I like to avoid coding in a "assuming the user knows what
    format to input" setup.

    Thanks to all for the replies and advice! This is kind of a side
    project for me at the moment, so I haven't yet gotten a chance to
    implement any of these things or test them, but I will be sure to get
    back to the group with my results when I do.

    As for my holiday list, I think the simplest initial concept, for
    testing the basic functionality, is a Hash of Date objects based on my
    employer's list of holidays for which employees get time off, as
    opposed to a list of all US holidays, but I'll look into a flexible
    way to plug in a holiday list at a later point.

    Thanks again,
    Andrew
     
    Kaldrenon, Jul 21, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  2. Replies:
    10
    Views:
    914
    Dale King
    May 15, 2006
  3. Maxwell Hammer
    Replies:
    7
    Views:
    647
    Peter Hansen
    Jun 18, 2005
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    374
    Chris
    Aug 30, 2005
  5. Edward
    Replies:
    3
    Views:
    184
    Gunnar Hjalmarsson
    Sep 8, 2004
Loading...

Share This Page