root/trunk/plagger/lib/Plagger/Date.pm

Revision 1638 (checked in by mark, 14 years ago)

r4@travis: mark | 2006-09-01 17:57:02 +0100
attempt at adding a little more documentation

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Date;
2 use strict;
3 use base qw( DateTime );
4
5 use Encode;
6 use DateTime::Format::Strptime;
7 use UNIVERSAL::require;
8
9 sub rebless { bless $_[1], $_[0] }
10
11 sub parse {
12     my($class, $format, $date) = @_;
13
14     my $module;
15     if (ref $format) {
16         $module = $format;
17     } else {
18         $module = "DateTime::Format::$format";
19         $module->require or die $@;
20     }
21
22     my $dt = $module->parse_datetime($date) or return;
23
24     # If parsed datetime is floating, don't set timezone here. It should be "fixed" in caller plugins
25     unless ($dt->time_zone->is_floating) {
26         $dt->set_time_zone( Plagger->context->conf->{timezone} || 'local' );
27     }
28
29     bless $dt, $class;
30 }
31
32 sub parse_dwim {
33     my($class, $str) = @_;
34
35     require Date::Parse;
36     my $time = Date::Parse::str2time($str) or return;
37
38     $class->from_epoch($time);
39 }
40
41 sub strptime {
42     my($class, $pattern, $date) = @_;
43     Encode::_utf8_on($pattern);
44     my $format = DateTime::Format::Strptime->new(pattern => $pattern);
45     $class->parse($format, $date);
46 }
47
48 sub now {
49     my($class, %opt) = @_;
50     my $self = $class->SUPER::now();
51
52     my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local';
53     $self->set_time_zone($tz);
54
55     $self;
56 }
57
58 sub from_epoch {
59     my $class = shift;
60     my %p = @_ == 1 ? (epoch => $_[0]) : @_;
61
62     $p{time_zone} = Plagger->context->conf->{timezone} || 'local';
63     $class->SUPER::from_epoch(%p);
64 }
65
66 sub format {
67     my($self, $format) = @_;
68
69     my $module;
70     if (ref $format) {
71         $module = $format;
72     } else {
73         $module = "DateTime::Format::$format";
74         $module->require or die $@;
75     }
76
77     $module->format_datetime($self);
78 }
79
80 sub set_time_zone {
81     my $self = shift;
82
83     eval {
84         $self->SUPER::set_time_zone(@_);
85     };
86     if ($@) {
87         $self->SUPER::set_time_zone('UTC');
88     }
89
90     return $self;
91 }
92
93 sub serialize {
94     my $self = shift;
95     $self->format('W3CDTF');
96 }
97
98 1;
99
100 __END__
101
102 =head1 NAME
103
104 Plagger::Date - DateTime subclass for Plagger
105
106 =head1 SYNOPSIS
107
108
109
110 =head1 DESCRIPTION
111
112 This module subclasses DataTime for plagger's own needs.
113
114 =over
115
116 =item rebless
117
118 ...
119
120 =item parse
121
122 ...
123
124 =item parse_dwim
125
126 ...
127
128 =item strptime
129
130 ...
131
132 =item now
133
134 ...
135
136 =item from_epoch
137
138 ...
139
140 =item format($format)
141
142 Convience method.  Returns the datetime in the format
143 passed (either a formatter object or a blessed reference)
144
145 =item set_time_zone
146
147 Overides default behavior to default to UTC if the passed
148 time zone isn't a legal
149
150 =item serialize
151
152 Returns the object as a W3CDTF string.
153
154 =cut
155
156 =back
157
158 =head1 AUTHOR
159
160 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
161
162 See I<AUTHORS> file for the name of all the contributors.
163
164 =head1 LICENSE
165
166 Except where otherwise noted, Plagger is free software; you can
167 redistribute it and/or modify it under the same terms as Perl itself.
168
169 =head1 SEE ALSO
170
171 L<http://plagger.org/>, L<DateTime>
172
173 =cut
Note: See TracBrowser for help on using the browser.