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

Revision 481 (checked in by miyagawa, 14 years ago)
  • Added CustomFeed?::Simple to extract links that match a regexp. Fixes #32
  • Added Plagger::Date->strptime($format, $date)
  • Added decode_content and extract_title to Util
  • Support metadata in Config, for now
  • 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     if (my $context = Plagger->context) {
24         $dt->set_time_zone($context->conf->{timezone} || 'local');
25     }
26
27     bless $dt, $class;
28 }
29
30 sub strptime {
31     my($class, $pattern, $date) = @_;
32     Encode::_utf8_on($pattern);
33     my $format = DateTime::Format::Strptime->new(pattern => $pattern);
34     $class->parse($format, $date);
35 }
36
37 sub now {
38     my($class, %opt) = @_;
39     my $self = $class->SUPER::now();
40
41     my $tz = $opt{timezone} || Plagger->context->conf->{timezone} || 'local';
42     $self->set_time_zone($tz);
43
44     $self;
45 }
46
47 sub from_epoch {
48     my $class = shift;
49     my %p = @_ == 1 ? (epoch => $_[0]) : @_;
50
51     $p{time_zone} = Plagger->context->conf->{timezone} || 'local';
52     $class->SUPER::from_epoch(%p);
53 }
54
55 sub format {
56     my($self, $format) = @_;
57     my $module = "DateTime::Format::$format";
58     $module->require or die $@;
59
60     $module->format_datetime($self);
61 }
62
63 1;
Note: See TracBrowser for help on using the browser.