Re: Sets, Recurrences, Public Holidays .. Business Days trouble

[email protected] ("Flavio S. Glock")
Newsgroups perl.datetime
Message-ID <[email protected]>
2008/1/22, Rick Measham <[email protected]>:
> The aim is to work out N 'business days' from now.
>
> Every one defines business days differently, but for me it's Monday to
> Friday skipping public holidays.
>
> Step 1: Set of every day
> Step 2: Complemented with weekends
> Step 3: Complemented with public holidays
>
> However, the code I've put together dies in rather nasty ways or runs
> really slowly (change the @events in the grep to @events[1,2])
>

I simplified the code a bit and it now works.
However, I don't know what is the problem with the original code.

- Flavio S. Glock
business_days.pl (application/x-perl, 1.2 KB)
#!/usr/bin/perl

use strict;
use warnings;

use DateTime;
use DateTime::Set;
use Data::ICal::DateTime;
use DateTime::Event::Recurrence;
use LWP::Simple;
use Data::Dumper;
use DateTime::Event::ICal;

my $min = DateTime->now->truncate( to => 'year' )->subtract( years => 1 );
my $max = $min->clone->add( years => 3 );

my $weekends  = DateTime::Event::ICal->recur(
    freq    => 'weekly',
	byday   => [ "mo", "tu", "we", "th", "fr" ],
    dtstart => $min,
    dtend   => $max,
);

my $public_holiday_ical = Data::ICal->new(
	# Melbourne public holidays
	data => get('http://www.google.com/calendar/ical/5be1qvd6221bpn0jknc1210qe4%40group.calendar.google.com/public/basic.ics')
);
my @events = $public_holiday_ical->events();

my $public_holidays = DateTime::Set->from_datetimes(
	dates => [
		map { $_->start }
		# We only want those that are actually public holidays
		grep { $_->property('description')->[0]->value eq 'Public Holiday' }
		@events
	]
);

my $business_days = $weekends->complement( $public_holidays );

require DateTime::Format::ICal;
print "recurrence: ", 
    join( "\n", DateTime::Format::ICal->format_recurrence( $business_days ) ), 
    "\n";

my $iter = $business_days->iterator;
while ( my $dt = $iter->next ) {
    print $dt->ymd, "\n";
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.