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

Revision 325 (checked in by miyagawa, 14 years ago)

Date->parse uses local as a default

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Date;
2 use strict;
3 use base qw( DateTime );
4
5 use UNIVERSAL::require;
6
7 sub rebless { bless $_[1], $_[0] }
8
9 sub parse {
10     my($class, $format, $date) = @_;
11
12     my $module;
13     if (ref $format) {
14         $module = $format;
15     } else {
16         $module = "DateTime::Format::$format";
17         $module->require or die $@;
18     }
19
20     my $dt = $module->parse_datetime($date) or return;
21     if (my $context = Plagger->context) {
22         $dt->set_time_zone($context->conf->{timezone} || 'local');
23     }
24
25     bless $dt, $class;
26 }
27
28 sub now {
29     my($class, %opt) = @_;
30     my $self = $class->SUPER::now();
31
32     my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local';
33     $self->set_time_zone($tz);
34
35     $self;
36 }
37
38 sub from_epoch {
39     my $class = shift;
40     my %p = @_ == 1 ? (epoch => $_[0]) : @_;
41
42     $p{time_zone} = Plagger->context->conf->{timezone} || 'local';
43     $class->SUPER::from_epoch(%p);
44 }
45
46 sub format {
47     my($self, $format) = @_;
48     my $module = "DateTime::Format::$format";
49     $module->require or die $@;
50
51     $module->format_datetime($self);
52 }
53
54 1;
Note: See TracBrowser for help on using the browser.