Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to find business days?

by cosmicsoup (Beadle)
on Feb 23, 2004 at 18:55 UTC ( [id://331180]=perlquestion: print w/replies, xml ) Need Help??

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

Monks

I have two dates, startdt and an enddt. I would like to find the number of business days between these two dates. Would anyone know the best solution for this?

Example:
startdt = 2004-02-01
enddt = 2004-02-10
It has taken 7 business days to complete.

Thanks,
Louis

Replies are listed 'Best First'.
Re: How to find business days?
by dragonchild (Archbishop) on Feb 23, 2004 at 19:00 UTC
    Date::Calc is your friend here. You want to look at days whose day-of-week ($dow) is less than 5.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: How to find business days?
by Enlil (Parson) on Feb 23, 2004 at 19:38 UTC
    You might want to take a look at Date::Calendar (delta_workdays), depending on how you want to define a workday (i.e. holidays might not be workdays).

    -enlil

      But unless there is already a Date::Calendar::Profiles module available you still have to provide your own Calendar.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to find business days?
by fuzzyping (Chaplain) on Feb 23, 2004 at 19:02 UTC
    What working code do you already have in place? Date::Calc has a bunch of functions that will be of use to you.

    Example:
    (Day_of_Week($year,$month,$day) < 6)
    -fp
      Hi. The working code that I have right now only as values for start and end dates. It would be simpler if I could have a number of days instead of just having those days listed.

      Louis
Re: How to find business days?
by leriksen (Curate) on Feb 23, 2004 at 23:24 UTC
    Have a look at datetime.perl.org , its so fresh and new !

    or

    Date::Business, though that hasn't changed for a long time

    +++++++++++++++++
    #!/usr/bin/perl
    use warnings;use strict;use brain;

Re: How to find business days?
by adrianh (Chancellor) on Feb 23, 2004 at 23:16 UTC

    The wonderful DateTime modules are your friend.

    use DateTime; use DateTime::Event::Recurrence; my $working_days = DateTime::Event::Recurrence->weekly( days => [1 .. +5] ); my $start = DateTime->new(year => 2004, month => 2, day => 1 ); my $end = DateTime->new(year => 2004, month => 2, day => 10 ); my $num_days = $working_days->as_list( start => $start, end => $end ); print $num_days;
Re: How to find business days?
by Roger (Parson) on Feb 23, 2004 at 23:14 UTC
    Don't forget about the holidays.

    That's slightly trickier, you need some sort of database or lookup table that contains all the holidays. In the bank I am working at, we use the infinity system, which provides a table in the database with holidays.

    Update: I see somebody has already mentioned the Calendar and Calendar profile modules. Never mind then.

Re: How to find business days?
by zentara (Cardinal) on Feb 24, 2004 at 15:06 UTC
    Here is a snippet which calculates "working days" between 2 dates. It takes local holidays into account as well. It was posted here a few weeks ago.
    #!/usr/bin/perl -l use Date::Calendar::Profiles qw( $Profiles ); use Date::Calendar; my $cal = Date::Calendar->new( $Profiles->{US} ) or die "no calendar\n"; my $days = $cal->delta_workdays( 2004, 2, 11, # first date 2004, 2, 18, # second date 1, # include first date 0); # exclude second date print "days: $days"; # The example prints the number of US workdays from # Feb. 11, 2004 to Feb. 18, 2004, which is 4 (Presidents Day!).

    I'm not really a human, but I play one on earth. flash japh
Re: How to find business days?
by cosmicsoup (Beadle) on Feb 24, 2004 at 15:47 UTC
    Monks

    Thank you to all that have responded to help me with this. I'm afraid that I'm still confounded. Mostly because my administrator only has Date::Manip installed.

    I'm reading in a file that has in it, among other things a start and end date for Many records:
    $id, ... $startdt, $enddt, ...
    I'm currently only doing a datediff between $startdt and $enddt, but I need to filter out weekends. Would anyone have any examples using Date::Manip's weekdays?

    As always, any help is appreciated!

    Louis

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-10-13 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.