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

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

Use XPath rather than recursive tree manipulation to find XOXO, thanks to youpy
http://subtech.g.hatena.ne.jp/youpy/20060505/p1

  • 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 {
10     my($class, $dt) = @_;
11     bless $dt, $class;
12 }
13
14 sub parse {
15     my($class, $format, $date) = @_;
16
17     my $module;
18     if (ref $format) {
19         $module = $format;
20     } else {
21         $module = "DateTime::Format::$format";
22         $module->require or die $@;
23     }
24
25     my $dt = $module->parse_datetime($date) or return;
26     $class->rebless($dt);
27 }
28
29 sub parse_dwim {
30     my($class, $str) = @_;
31
32     require Date::Parse;
33     my $time = Date::Parse::str2time($str) or return;
34
35     $class->from_epoch($time);
36 }
37
38 sub strptime {
39     my($class, $pattern, $date) = @_;
40     Encode::_utf8_on($pattern);
41     my $format = DateTime::Format::Strptime->new(pattern => $pattern);
42     $class->parse($format, $date);
43 }
44
45 sub now {
46     my($class, %opt) = @_;
47     my $self = $class->SUPER::now();
48
49     my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local';
50     $self->set_time_zone($tz);
51
52     $self;
53 }
54
55 sub from_epoch {
56     my $class = shift;
57     my %p = @_ == 1 ? (epoch => $_[0]) : @_;
58
59     $p{time_zone} = Plagger->context->conf->{timezone} || 'local';
60     $class->SUPER::from_epoch(%p);
61 }
62
63 sub format {
64     my($self, $format) = @_;
65     my $module = "DateTime::Format::$format";
66     $module->require or die $@;
67
68     $module->format_datetime($self);
69 }
70
71 1;
Note: See TracBrowser for help on using the browser.