aboutsummaryrefslogtreecommitdiff
path: root/lib/Template/Plugin/DateTimeFormat.pm
blob: 8c6d592224eea441dd34391b1bd4207d9ce1c743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Template::Plugin::DateTimeFormat; 
$Template::Plugin::DateTimeFormat::VERSION = '0.04';
use 5.006;
use strict;
use warnings;
use DateTime;
use Class::Load;
 
# hacked Template::Plugin::DateTime::Format with patch from 
# https://rt.cpan.org/Public/Bug/Display.html?id=120391 
 
use base 'Template::Plugin';
 
sub new {
    my ($class$context$formatter_class$new_args$format_args) = @_;
    Class::Load::load_class($formatter_class || die 'need class name');
 
    my @new_args = ref $new_args eq 'ARRAY' ? @$new_args : $new_args;
    if ($format_args) {
      $format_args = [ $format_args ] unless ref $format_args eq 'ARRAY';
    }
 
    bless {
_CONTEXT    => $context,
        formatter   => $formatter_class->new(@new_args),
        format_args => $format_args || [],
    }, $class;
}
 
sub format {
    my ($self$date) = @_;
 
    my $fmt = $self->{formatter};
    my @args = @{$self->{format_args}};
    return $fmt->format_datetime($date@args);
}
 
1;