Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Date Calculation

by palette (Scribe)
on Jan 30, 2007 at 14:33 UTC ( #597370=perlquestion: print w/replies, xml ) Need Help??

palette has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Date Calculation
by philcrow (Priest) on Jan 30, 2007 at 14:38 UTC
    You could try Date::Calc. From its docs:

    8) How can I add a week offset to a business date (including across year boundaries)?
    use Date::Calc qw( Business_to_Standard Add_Delta_Days Standard_to_Business ); @temp = Business_to_Standard($year,$week,$dow); @temp = Add_Delta_Days(@temp, $week_offset * 7); ($year,$week,$dow) = Standard_to_Business(@temp);


    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Date Calculation
by davorg (Chancellor) on Jan 30, 2007 at 14:40 UTC

    How do you define a business day? Is it weekdays? Or do you need to take into account public holidays and things like that?

    Here's a naive approach that just leaves you needing to define an is_business_day() subroutine which takes a time (as an epoch seconds value) as an argument.

    my $today = time; my $days_added; while (1) { $time += 24 * 60 * 60; # add a day $days_added++ if is_business_day($time); last if $days_added == 10; } print scalar localtime $time;

    Update: As ikegami correctly points out below, there's a potential bug in this program if it's run across days that don't contain 24 hours (i.e. when switching to or from daylight saving time). The fix is to normalise the time to noon (assuming that timezone changes always occur overnight). This change is left as an exercise for the reader.

      Not all days have 24 hours.
Re: Date Calculation
by davidrw (Prior) on Jan 30, 2007 at 15:19 UTC
    here's a Date::Calc solution -- assumes "business day" is just M-F, so adding 10 just needs to add in the two weekends as well, so is simply:
    use Date::Calc qw/Add_Delta_Days/; my @start = ( $year, $month, $day ); my @end = Add_Delta_Days( @start, 14 );
    If you want to take into account holidays, see the "10) How can I calculate the last business day (payday!) of a month?" example in the Date::Calc docs for reference.
Re: Date Calculation
by liverpole (Monsignor) on Jan 30, 2007 at 14:39 UTC
    Hi palette,

    Look at Date::Calc.  There is a function Add_Delta_Days which may be close to what you need.

    Additionally, there are some functions in it which deal specifically with business days:

    check_business_date Standard_to_Business Business_to_Standard

Re: Date Calculation
by Cody Pendant (Prior) on Jan 31, 2007 at 00:38 UTC
    Call me Mr States-the-obvious, but how about the module Date::Business?

    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Date Calculation
by gube (Parson) on Jan 30, 2007 at 15:06 UTC

    Updated : Sorry, Mistaken i suggest to add days i missed to read the word buisness days.

    Use DateCalc module. DateCalc

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://597370]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-09-27 02:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found