Alister West

home is where your code is ...

Dates and Times in perl

Date stuff comes up all the time. Examples here for me from DateTime, Date::Tiny, Date::Parse, Time::ParseDate, Date::Manip and occasionally Bash :)

# DateTime mysql date range
use DateTime;
my $start = DateTime->new( year => 2010, month => 01 );
my $end = DateTime->now()->set(day=>1)->add(months=>1);

while( DateTime::compare($end, $start) >= 0 ) {
    say $start->ymd;
    $start->add( months => 1 );
}

# DateTime - Find yesterday's date
use DateTime;
my $dt = DateTime->now( time_zone => "local" );
my $delta = DateTime::Duration->new(days => 1);
$dt -= $delta;
say $dt->ymd;



# stupid mysql date range - is simple more elegant?
for my $y (2010..2012) {
    for my $m (1..12) {
        say sprintf("%4d-%02d-01", $y, $m);
        exit if $y >= 2012 and $m >= 4;
    }
}



# RT::Date - diffs.
my $date = '2010-10-10';
my $due = RT::Date->new( RT->SystemUser );
$due->Set( Format => 'sql', Value => $date );

# if its over a day old .. do something
my $diff = $due->Diff;
return (1) if ( $diff < 24*60*60);

# check on epoch
$date->Unix > time ? 1 : 0;
By Alister West