Re: [rt.cpan.org #34912] Constants for months/weekdays?

[email protected] ("Flavio S. Glock")
Newsgroups perl.datetime
Message-ID <[email protected]>
2008/4/12, Flavio Poletti via RT <[email protected]>:
>  Sat Apr 12 08:41:24 2008: Request 34912 was acted upon.
>  Transaction: Ticket created by POLETTIX
>        Queue: DateTime
>      Subject: Constants for months/weekdays?
>    Broken in: 0.42
>     Severity: Wishlist
>        Owner: Nobody
>   Requestors: [email protected]
>       Status: new
>   Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=34912 >
>
>  Hi,
>
>    I think it would be useful to have some constants exported by the
>  module to refer to month names and weekdays, like JANUARY, JANUARY_0 and
>  so on. Otherwise, it's likely that the users will define such constants
>  by themselves (or worse hardcode them), leading to potential bugs.
>
>  I noticed this while reading http://use.perl.org/~Aristotle/journal/36022.


# Reference: http://use.perl.org/~Aristotle/journal/36022

{
#--------------
# implementation using the "ICal" module

use DateTime::Event::ICal;
print "with ICal\n";
my $first_thursday = DateTime::Event::ICal->recur(
    freq => 'monthly',
    byday => [ '1th' ]
);
my $fridays = DateTime::Event::ICal->recur(
    freq => 'weekly',
    byday => [ 'fr' ]
);
my $first_friday = DateTime::Event::ICal->recur(
    freq => 'monthly',
    byday => [ '1fr' ]
);
my $last_friday = DateTime::Event::ICal->recur(
    freq => 'monthly',
    byday => [ '-1fr' ]
);

my $dt = DateTime->today;
my $dt_month = DateTime->today->truncate( to => 'month' )->add( months => 1 );

print 'First Friday of this month: ',
   $first_friday->previous( $dt_month ), "\n";

print 'Last Friday of this month: ',
   $last_friday->previous( $dt_month ), "\n";

print 'The Friday before today: ',
   $fridays->previous( $dt ), "\n";

print 'London.pm heretics meeting this month: ',
   $first_thursday->previous( $dt_month ), "\n";

print "\n";
#--------------
}


{
#--------------
# implementation using the "Recurrence" module
# - requires a little more calculations; more error-prone

use DateTime::Event::Recurrence;
print "with Recurrence\n";
my $thursdays = DateTime::Event::Recurrence->weekly( days => [ 'th' ] );
my $fridays   = DateTime::Event::Recurrence->weekly( days => [ 'fr' ] );

my $dt = DateTime->today;

print 'First Friday of this month: ',
   $fridays->next( $dt->clone->truncate( to => 'month' )->subtract(
days => 1 ) ), "\n";

print 'Last Friday of this month: ',
   $fridays->previous( $dt->clone->truncate( to => 'month' )->add(
months => 1 ) ), "\n";

print 'The Friday before today: ',
   $fridays->previous( $dt ), "\n";

print 'London.pm heretics meeting this month: ',
   $thursdays->next( $dt->clone->truncate( to => 'month' )->subtract(
days => 1 ) ), "\n";

print "\n";
#--------------
}

# First Friday of this month: 2008-04-04T00:00:00
# Last Friday of this month: 2008-04-25T00:00:00
# The Friday before today: 2008-04-11T00:00:00
# London.pm heretics meeting this month: 2008-04-03T00:00:00
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.