| 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 |
|
|---|
| 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__ |
|---|
| 126 |
|
|---|
| 127 |
=head1 NAME |
|---|
| 128 |
|
|---|
| 129 |
Plagger::Date - DateTime subclass for Plagger |
|---|
| 130 |
|
|---|
| 131 |
=head1 SYNOPSIS |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
=head1 DESCRIPTION |
|---|
| 136 |
|
|---|
| 137 |
This module subclasses DateTime for plagger's own needs. |
|---|
| 138 |
|
|---|
| 139 |
=over |
|---|
| 140 |
|
|---|
| 141 |
=item rebless |
|---|
| 142 |
|
|---|
| 143 |
... |
|---|
| 144 |
|
|---|
| 145 |
=item parse |
|---|
| 146 |
|
|---|
| 147 |
... |
|---|
| 148 |
|
|---|
| 149 |
=item parse_dwim |
|---|
| 150 |
|
|---|
| 151 |
... |
|---|
| 152 |
|
|---|
| 153 |
=item strptime |
|---|
| 154 |
|
|---|
| 155 |
... |
|---|
| 156 |
|
|---|
| 157 |
=item now |
|---|
| 158 |
|
|---|
| 159 |
... |
|---|
| 160 |
|
|---|
| 161 |
=item from_epoch |
|---|
| 162 |
|
|---|
| 163 |
... |
|---|
| 164 |
|
|---|
| 165 |
=item format($format) |
|---|
| 166 |
|
|---|
| 167 |
Convenience method. Returns the datetime in the format |
|---|
| 168 |
passed (either a formatter object or a blessed reference) |
|---|
| 169 |
|
|---|
| 170 |
=item set_time_zone |
|---|
| 171 |
|
|---|
| 172 |
Overrides default behavior to default to UTC if the passed |
|---|
| 173 |
time zone isn't a legal |
|---|
| 174 |
|
|---|
| 175 |
=item serialize |
|---|
| 176 |
|
|---|
| 177 |
Returns the object as a W3CDTF string. |
|---|
| 178 |
|
|---|
| 179 |
=cut |
|---|
| 180 |
|
|---|
| 181 |
=back |
|---|
| 182 |
|
|---|
| 183 |
=head1 AUTHOR |
|---|
| 184 |
|
|---|
| 185 |
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> |
|---|
| 186 |
|
|---|
| 187 |
See I<AUTHORS> file for the name of all the contributors. |
|---|
| 188 |
|
|---|
| 189 |
=head1 LICENSE |
|---|
| 190 |
|
|---|
| 191 |
Except where otherwise noted, Plagger is free software; you can |
|---|
| 192 |
redistribute it and/or modify it under the same terms as Perl itself. |
|---|
| 193 |
|
|---|
| 194 |
=head1 SEE ALSO |
|---|
| 195 |
|
|---|
| 196 |
L<http://plagger.org/>, L<DateTime> |
|---|
| 197 |
|
|---|
| 198 |
=cut |
|---|