line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::DtFormatter::RelativeDate; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
43843
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
981
|
use utf8; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
5
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
37
|
use base 'Template::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
979
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#use DateTime::Locale; |
10
|
1
|
|
|
1
|
|
9162
|
use DateTime::Format::Natural; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Template::Plugin::DtFormatter::RelativeDate::I18N; |
12
|
|
|
|
|
|
|
#my $loc = DateTime::Locale->load('ja_JP'); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $MOCK = 0; |
15
|
|
|
|
|
|
|
my $NATURAL = DateTime::Format::Natural->new; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Template::Plugin::DtFormatter::RelativeDate - return finder like relative date. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 VERSION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Version 0.03 |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
[% USE DtFormatter.RelativeDate %] |
32
|
|
|
|
|
|
|
[% SET ymd = DtFormatter.RelativeDate.formatter("%Y-%m-%d", 'en') %] |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
[% USE date = DateTime(today => 1) %] |
35
|
|
|
|
|
|
|
[% ymd( date ) %] # Today |
36
|
|
|
|
|
|
|
[% ymd( date.add(days=>1) ) %] # Tomorrow |
37
|
|
|
|
|
|
|
[% ymd( date.add(days=>1) ) %] # 2007-07-31 |
38
|
|
|
|
|
|
|
[% ymd( date.subtract(days=>3) ) %] # Yesterday |
39
|
|
|
|
|
|
|
[% ymd( date.subtract(days=>1) ) %] # 2007-07-27 |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 FUNCTIONS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 new |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
internal function. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub new { |
50
|
|
|
|
|
|
|
my ($class, $context) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $self = bless {}, $class; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return $self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 formatter(strftime_string, lang) |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
return closure. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub formatter { |
64
|
|
|
|
|
|
|
my ($self, $format, $lang) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$lang ||= 'en'; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my %memoize; |
69
|
|
|
|
|
|
|
if ($MOCK) { |
70
|
|
|
|
|
|
|
my $today = DateTime->new(year=>2007,month=>7,day=>28); |
71
|
|
|
|
|
|
|
%memoize = ( |
72
|
|
|
|
|
|
|
today => $today, |
73
|
|
|
|
|
|
|
yesterday => $today->clone->subtract( days => 1 ), |
74
|
|
|
|
|
|
|
tomorrow => $today->clone->add( days => 1 ), |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $lh = Template::Plugin::DtFormatter::RelativeDate::I18N->get_handle($lang); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
return sub { |
81
|
|
|
|
|
|
|
my $dt = shift; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
local $NATURAL->{Time_zone} = $dt->{tz} if ref($dt) and $dt->isa("DateTime"); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
for my $string (qw/today yesterday tomorrow/) { |
86
|
|
|
|
|
|
|
$memoize{$string} ||= $NATURAL->parse_datetime($string); |
87
|
|
|
|
|
|
|
return $lh->maketext($string) if $memoize{$string}->ymd eq $dt->ymd; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
return $format ? $dt->strftime($format) : $dt->ymd; |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 AUTHOR |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
bokutin, C<< >> |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Copyright 2007 bokutin, all rights reserved. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
105
|
|
|
|
|
|
|
under the same terms as Perl itself. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |