| 1 | package MYDan::Oncall; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DateTime; | ||
| 7 | |||
| 8 | use constant { WEEK => 604800, DAY => 86400, HOUR => 3600, MINUTE => 60 }; | ||
| 9 | |||
| 10 | our $LTZ = DateTime::TimeZone->new( name => 'local' ); | ||
| 11 | our %RGX = | ||
| 12 | ( | ||
| 13 | year => qr/[2-9]\d{3}/, | ||
| 14 | month => qr/1[0-2]|0?[1-9]/, | ||
| 15 | day => qr/3[01]|[1-2]\d|0?[1-9]/, | ||
| 16 | hour => qr/2[0-3]|[0-1]?\d/, | ||
| 17 | minute => qr/[0-5]?\d/, | ||
| 18 | second => qr/[0-5]?\d/, | ||
| 19 | ); | ||
| 20 | |||
| 21 | =head1 METHODS | ||
| 22 | |||
| 23 | =head3 epoch( $date, $tz ) | ||
| 24 | |||
| 25 | Returns seconds since epoch of expression $date with timezone $tz | ||
| 26 | |||
| 27 | =cut | ||
| 28 | sub epoch | ||
| 29 | { | ||
| 30 | my ( $class, $date, $tz ) = splice @_; | ||
| 31 | my ( @key, %time ) = qw( year month day hour minute second ); | ||
| 32 | |||
| 33 | return undef unless my @time = split /\D+/, $date || ''; | ||
| 34 | push @time, 0 while @time < @key; | ||
| 35 | |||
| 36 | for my $i ( 0 .. $#key ) | ||
| 37 | { | ||
| 38 | my $key = $key[$i]; | ||
| 39 | return undef if $time[$i] !~ qr/^($RGX{$key})$/; | ||
| 40 | $time{$key} = $1; | ||
| 41 | } | ||
| 42 | return DateTime->new( %time, time_zone => $tz || $LTZ )->epoch; | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; |