Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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:

Hi All,
I wanted to have a way to add 10 business days to the current date.
Thanks !

Comment on Date Calculation
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);

    Phil

      But I want the 10 days even to be a business days.

      I think it will not work in that case
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

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
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 gube (Parson) on Jan 30, 2007 at 15:06 UTC
    Hi,

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

    Use DateCalc module. DateCalc

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 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-10-01 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (36 votes), past polls