« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App/DB.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Util/Xslate.pm »
| 1 | package Dwarf::Util::DateTime; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use DateTime; | ||
| 4 | use DateTime::Format::Strptime; | ||
| 5 | |||
| 6 | require Exporter; | ||
| 7 | our @ISA = qw(Exporter); | ||
| 8 | our @EXPORT = qw(is_duration_positive epoch2dt str2dt); | ||
| 9 | |||
| 10 | sub is_duration_positive { | ||
| 11 | my ($dt1, $dt2) = @_; | ||
| 12 | die "Something's wrong" if not defined $dt1 or not defined $dt2; | ||
| 13 | my $duration = $dt1 - $dt2; | ||
| 14 | $duration->is_positive; | ||
| 15 | } | ||
| 16 | |||
| 17 | # エポック秒から DateTime オブジェクトを生成する | ||
| 18 | sub epoch2dt { | ||
| 19 | my ($epoch) = @_; | ||
| 20 | return DateTime->from_epoch( | ||
| 21 | epoch => $epoch, | ||
| 22 | time_zone => 'Asia/Tokyo', | ||
| 23 | ); | ||
| 24 | } | ||
| 25 | |||
| 26 | # 文字列でも DateTime オブジェクトでも渡して構わない | ||
| 27 | sub str2dt { | ||
| 28 | my ($string) = @_; | ||
| 29 | return $string if (ref($string) and ref($string) eq 'DateTime'); | ||
| 30 | $string =~ s|^(\d\d-\d\d)$|$1T00:00:00|; | ||
| 31 | $string =~ s|T(\d\d:\d\d)$|T$1:00|; | ||
| 32 | if ($string =~ m|^(\d\d-\d\d)T|) { | ||
| 33 | my @t = localtime; | ||
| 34 | my $year = $t[5] + 1900; | ||
| 35 | $string =~ s|^(\d\d-\d\d)T|${year}-$1T|; | ||
| 36 | } | ||
| 37 | $string =~ s|^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})|$1T$2|; | ||
| 38 | DateTime::Format::Strptime->new(pattern => '%FT%T', time_zone => 'Asia/Tokyo')->parse_datetime($string); | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; |