houseabsolute / DateTime.pm

A date and time object for Perl
https://metacpan.org/release/DateTime/
Other
47 stars 49 forks source link

Constants for months/weekdays? #26

Open autarch opened 8 years ago

autarch commented 8 years ago

Migrated from rt.cpan.org #34912 (status was 'open')

Requestors:

Attachments:

From polettix@cpan.org (@polettix) on 2008-04-12 12:41:24:

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.

Cheers,

Flavio.

autarch commented 8 years ago

From fglock@gmail.com on 2008-04-12 20:11:36:

2008/4/12, Flavio Poletti via RT bug-DateTime@rt.cpan.org:

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: polettix@cpan.org 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
autarch commented 8 years ago

From rickm@isite.net.au on 2008-04-13 08:42:28:

MailGuard Email Filter Alert

Date: April 13 2008, 6:42PM From: rickm@isite.net.au To: bug-datetime@rt.cpan.org Subject: Re: [rt.cpan.org #34912] Constants for months/weekdays? MailGuard Ref: 4801c7681f4a44 Size: 12.9 KB

This message has been QUARANTINED by the MailGuard service for the following reason:

The message contained an attachment of type 'Executable file'.

This message is due to be deleted from the quarantine area on April 20 2008, 6:42PM.If you believe that this message was quarantined in error and would like it released, please click here to email Rick Measham rickm@isite.net.au at Rick Measham as soon as possible.

You will not receive another warning before this message is deleted.

Regards,

MailGuard E-mail Anti-Virus, Anti-Spam and Content Filtering Service. http://www.mailguard.com.au

autarch commented 8 years ago

From rickm@isite.net.au on 2008-04-13 11:24:41:

Flavio S. Glock wrote:

implementation using the "ICal" module

implementation using the "Recurrence" module

Sure, those work. But to quote the journal: "Instead you are apparently expected to copy-paste some slightly fiddly code from an FAQ and tweak it."

I think the code in the blog post has merit, though the wrong module name and therefore, the wrong API

I've taken the code in Aristotle's journal post, fixed it up a bit, wrapped it all in a DateTime::Event API and attached it. There's no tests yet, just the module.

Thoughts and comments are, as always, most welcome.

Aristotle: I'm happy to hand this module over to you if you want to put it on CPAN and maintain it, otherwise I will.

Cheers! Rick Measham

Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and content filtering. http://www.mailguard.com.au

autarch commented 8 years ago

From polettix@cpan.org (@polettix) on 2008-04-13 13:32:37:

On Sun Apr 13 07:24:41 2008, rickm@isite.net.au wrote:

Flavio S. Glock wrote:

  • implementation using the "ICal" module

  • implementation using the "Recurrence" module

... I think the code in the blog post has merit, though the wrong module name and therefore, the wrong API ...

Ehr, this seems to go a little off-topic with respect to the ticket's intent. Looking at the example code from Aristotle, I saw that he was defining some variables to keep the numerical indexes for the week days. I turned on to DateTime and saw there's no constant available in the module for either weekday names or month names, hence my suggestion.

To rephrase: is it possible to have constants like JANUARY, JANUARY_0, FEBRUARY, FEBRUARY_0, ..., MONDAY, MONDAY_0, TUESDAY, TUESDAY_0, ... in DateTime?

I'm not sure Aristotle is taking a look to this ticket in RT, so you'd probably repost your considerations on his blog in use.perl.org, unless you already did.

I hope this all makes sense.

Flavio.