forked from miyagawa/remedie
/
Date.pm
203 lines (137 loc) · 3.59 KB
/
Date.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package Plagger::Date;
use strict;
use base qw( DateTime );
use Encode;
use DateTime::Format::Strptime;
use DateTime::TimeZone;
use UNIVERSAL::require;
sub rebless { bless $_[1], $_[0] }
sub parse {
my($class, $format, $date) = @_;
my $module;
if (ref $format) {
$module = $format;
} else {
$module = "DateTime::Format::$format";
$module->require or die $@;
}
my $dt = $module->parse_datetime($date) or return;
bless $dt, $class;
}
sub parse_dwim {
my($class, $str) = @_;
return unless defined $str;
# check if it's Japanese
if ($str =~ /^(\x{5E73}\x{6210}|\x{662D}\x{548C}|\x{5927}\x{6B63}|\x{660E}\x{6CBB})/) {
eval { require DateTime::Format::Japanese };
if ($@) {
Plagger->context->log(warn => "requires DateTime::Format::Japanese to parse '$str'");
return;
}
return $class->parse( 'Japanese', encode_utf8($str) );
}
# Handle YYYY*MM*DD (This should also parse Japanese strings as well)
if ($str =~ /^(\d{4}).(\d{1,2}).(\d{1,2}).?$/) {
return $class->new(year => $1, month => $2, day => $3);
}
require Date::Parse;
my %p;
@p{qw( second minute hour day month year zone )} = Date::Parse::strptime($str);
unless (defined($p{year}) && defined($p{month}) && defined($p{day})) {
return;
}
$p{year} += 1900;
$p{month}++;
my $zone = delete $p{zone};
for (qw( second minute hour )) {
delete $p{$_} unless defined $p{$_};
}
my $dt = $class->new(%p);
if (defined $zone) {
my $tz = DateTime::TimeZone::offset_as_string($zone);
$dt->set_time_zone($tz);
}
$dt;
}
sub strptime {
my($class, $pattern, $date) = @_;
Encode::_utf8_on($pattern);
my $format = DateTime::Format::Strptime->new(pattern => $pattern);
$class->parse($format, $date);
}
sub now {
my($class, %opt) = @_;
my $self = $class->SUPER::now();
my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local';
$self->set_time_zone($tz);
$self;
}
sub from_epoch {
my $class = shift;
my %p = @_ == 1 ? (epoch => $_[0]) : @_;
$class->SUPER::from_epoch(%p);
}
sub format {
my($self, $format) = @_;
my $module;
if (ref $format) {
$module = $format;
} else {
$module = "DateTime::Format::$format";
$module->require or die $@;
}
$module->format_datetime($self);
}
sub set_time_zone {
my $self = shift;
eval {
$self->SUPER::set_time_zone(@_);
};
if ($@) {
$self->SUPER::set_time_zone('UTC');
}
return $self;
}
sub serialize {
my $self = shift;
$self->format('W3CDTF');
}
1;
__END__
=head1 NAME
Plagger::Date - DateTime subclass for Plagger
=head1 SYNOPSIS
=head1 DESCRIPTION
This module subclasses DateTime for plagger's own needs.
=over
=item rebless
...
=item parse
...
=item parse_dwim
...
=item strptime
...
=item now
...
=item from_epoch
...
=item format($format)
Convenience method. Returns the datetime in the format
passed (either a formatter object or a blessed reference)
=item set_time_zone
Overrides default behavior to default to UTC if the passed
time zone isn't a legal
=item serialize
Returns the object as a W3CDTF string.
=cut
=back
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
See I<AUTHORS> file for the name of all the contributors.
=head1 LICENSE
Except where otherwise noted, Plagger is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<http://plagger.org/>, L<DateTime>
=cut