| 1 | package Plagger::Date; | ||
| 2 | use strict; | ||
| 3 | use base qw( DateTime ); | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | use DateTime::Format::Strptime; | ||
| 7 | use DateTime::TimeZone; | ||
| 8 | use UNIVERSAL::require; | ||
| 9 | |||
| 10 | sub rebless { bless $_[1], $_[0] } | ||
| 11 | |||
| 12 | sub parse { | ||
| 13 | my($class, $format, $date) = @_; | ||
| 14 | |||
| 15 | my $module; | ||
| 16 | if (ref $format) { | ||
| 17 | $module = $format; | ||
| 18 | } else { | ||
| 19 | $module = "DateTime::Format::$format"; | ||
| 20 | $module->require or die $@; | ||
| 21 | } | ||
| 22 | |||
| 23 | my $dt = $module->parse_datetime($date) or return; | ||
| 24 | bless $dt, $class; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub parse_dwim { | ||
| 28 | my($class, $str) = @_; | ||
| 29 | |||
| 30 | return unless defined $str; | ||
| 31 | |||
| 32 | # check if it's Japanese | ||
| 33 | if ($str =~ /^(\x{5E73}\x{6210}|\x{662D}\x{548C}|\x{5927}\x{6B63}|\x{660E}\x{6CBB})/) { | ||
| 34 | eval { require DateTime::Format::Japanese }; | ||
| 35 | if ($@) { | ||
| 36 | Plagger->context->log(warn => "requires DateTime::Format::Japanese to parse '$str'"); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | return $class->parse( 'Japanese', encode_utf8($str) ); | ||
| 40 | } | ||
| 41 | |||
| 42 | require Date::Parse; | ||
| 43 | my %p; | ||
| 44 | @p{qw( second minute hour day month year zone )} = Date::Parse::strptime($str); | ||
| 45 | |||
| 46 | unless (defined($p{year}) && defined($p{month}) && defined($p{day})) { | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | $p{year} += 1900; | ||
| 51 | $p{month}++; | ||
| 52 | |||
| 53 | my $zone = delete $p{zone}; | ||
| 54 | for (qw( second minute hour )) { | ||
| 55 | delete $p{$_} unless defined $p{$_}; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $dt = $class->new(%p); | ||
| 59 | |||
| 60 | if (defined $zone) { | ||
| 61 | my $tz = DateTime::TimeZone::offset_as_string($zone); | ||
| 62 | $dt->set_time_zone($tz); | ||
| 63 | } | ||
| 64 | |||
| 65 | $dt; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub strptime { | ||
| 69 | my($class, $pattern, $date) = @_; | ||
| 70 | Encode::_utf8_on($pattern); | ||
| 71 | my $format = DateTime::Format::Strptime->new(pattern => $pattern); | ||
| 72 | $class->parse($format, $date); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub now { | ||
| 76 | my($class, %opt) = @_; | ||
| 77 | my $self = $class->SUPER::now(); | ||
| 78 | |||
| 79 | my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local'; | ||
| 80 | $self->set_time_zone($tz); | ||
| 81 | |||
| 82 | $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub from_epoch { | ||
| 86 | my $class = shift; | ||
| 87 | my %p = @_ == 1 ? (epoch => $_[0]) : @_; | ||
| 88 | $class->SUPER::from_epoch(%p); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub format { | ||
| 92 | my($self, $format) = @_; | ||
| 93 | |||
| 94 | my $module; | ||
| 95 | if (ref $format) { | ||
| 96 | $module = $format; | ||
| 97 | } else { | ||
| 98 | $module = "DateTime::Format::$format"; | ||
| 99 | $module->require or die $@; | ||
| 100 | } | ||
| 101 | |||
| 102 | $module->format_datetime($self); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub set_time_zone { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | eval { | ||
| 109 | $self->SUPER::set_time_zone(@_); | ||
| 110 | }; | ||
| 111 | if ($@) { | ||
| 112 | $self->SUPER::set_time_zone('UTC'); | ||
| 113 | } | ||
| 114 | |||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub serialize { | ||
| 119 | my $self = shift; | ||
| 120 | $self->format('W3CDTF'); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ |