Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created September 3, 2009 02:37
Show Gist options
  • Save kyanny/180088 to your computer and use it in GitHub Desktop.
Save kyanny/180088 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use DateTime;
use DateTime::Duration;
use Benchmark qw(timethese cmpthese);
use Date::Calc qw(check_date check_time Today Today_and_Now Delta_DHMS);
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
sub now { sprintf('%04d-%02d-%02d %02d:%02d:%02d', Today_and_Now()); }
my $old = DateTime->new(year => 2009, month => 9, day => 3,
hour => 9, minute => 57, second => 30, time_zone => 'local');
sub from_epoch {
DateTime->from_epoch(epoch => time, time_zone => 'local')->datetime;
}
sub regexp {
my ($year, $month, $day, $hour, $minute, $second) =
now() =~ m{^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$};
DateTime->new(year => $year, month => $month, day => $day,
hour => $hour, minute => $minute, second => $second, time_zone => 'local')->datetime;
}
sub from_epoch_duration {
my $new = DateTime->from_epoch(epoch => time, time_zone => 'local');
my $dur = $new - $old;
my $dump = Dumper +{ $dur->deltas };
#warn $dump;
my $new_dur = DateTime::Duration->new(%{ eval $dump });
#warn Dumper +{ $new_dur->deltas };
}
sub regexp_duration {
my ($year, $month, $day, $hour, $minute, $second) =
now() =~ m{^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$};
my $new = DateTime->new(year => $year, month => $month, day => $day,
hour => $hour, minute => $minute, second => $second, time_zone => 'local');
my $dur = $new - $old;
my $dump = Dumper +{ $dur->deltas };
#warn $dump;
my $new_dur = DateTime::Duration->new(%{ eval $dump });
#warn Dumper +{ $new_dur->deltas };
}
my $count = shift || 1000;
timethese($count, {
from_epoch => \&from_epoch,
regexp => \&regexp,
from_epoch_duration => \&from_epoch_duration,
regexp_duration => \&regexp_duration,
});
cmpthese($count, {
from_epoch => \&from_epoch,
regexp => \&regexp,
from_epoch_duration => \&from_epoch_duration,
regexp_duration => \&regexp_duration,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment