[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-datetime
Subject:    Re: Sets, Recurrences, Public Holidays .. Business Days trouble
From:       "Flavio S. Glock" <fglock () gmail ! com>
Date:       2008-01-23 15:26:37
Message-ID: aa47605d0801230726h17abf46aj2699231146fa8362 () mail ! gmail ! com
[Download RAW message or body]

2008/1/22, Rick Measham <rick@measham.id.au>:
> 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)]

#!/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";
}



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic