6.12 is listed as TODO, here is my solution. Is there a better way to do it? If there's no better way is it worth making some sort of module -- although I'm not sure what to call it!

Step four can be removed if we reword the question to remove the part about 'days'.

Cheers!
Rick

# FAQ 6.12

use strict;

use DateTime;
use DateTime::Span;
use DateTime::Event::Recurrence;
use DateTime::Format::Duration;

my $dt1 = DateTime->now();
my $dt2 = $dt1->clone->add( days => 7 );


# 1. Get the span including weekends


my $span = DateTime::Span->from_datetimes(
        start  => $dt1,
        before => $dt2,
);



# 2. Get a set of the weekends

# 2a. Get every Saturday
my $saturdays = DateTime::Event::Recurrence->weekly(
        days => 6
);

# 2b. Turn the Saturday datetimes into 2-day spans
my $weekend_spans = DateTime::SpanSet
        ->from_set_and_duration( set => $saturdays, days => 2 );



# 3. Subtract 2 from 1

my $relevent_span = $span->complement( $weekend_spans );



# 4. Report the result

printf "The span contains %s weekdays.",
        DateTime::Format::Duration::strfduration(
                duration  => $relevent_span->duration,
                pattern   => '%j',
                normalise => 'ISO',
        );

__END__




Senior Developer PrintSupply - Print Procurement & Supply Management 18 Greenaway Street VIC 3105 Tel: (03) 9850 3255 Fx: (03) 9850 3277 http://www.printsupply.com.au/



Reply via email to