NAME

Template::Plugin::DtFormatter - Easily create formatted string from DateTime objects

SYNOPSIS

[% USE DtFormatter %]
[% DtFromatter.format(mydt, 'human') %]

15/February/2007, 11:55

[% DtFromatter.format(mydt, 'hm') %]

11:55

DESCRIPTION

This modules provides a simple mean of formatting DateTime object in TT templates. It provides some predefined formatter formatters, and you can extend it by providing your own or overriding existing ones.

The advantage of dtformatter is that, basically, it provides human-readable shortcuts for format strings, which allow you to just modify the formatter in the module configuration to affect all the dates displayed using it.

Use this way:

[% USE DtFormatter %]
[% DtFormatter.format(DateTime_object, formatter_name) %]

PREDEFINED FORMATTERS

These are the predefined formatters. They all use DateTime::Format::Strptime, so that is the only dependency of this module.

sql         => DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M:%S'),
sql_date    => DateTime::Format::Strptime->new(pattern => '%Y-%m-%d'),
human       => DateTime::Format::Strptime->new(pattern => '%d/%m/%Y, %H:%M'),
human2      => DateTime::Format::Strptime->new(pattern => '%d-%m-%Y, %H:%M'),
human_date  => DateTime::Format::Strptime->new(pattern => '%d/%m/%Y'),
human_date2 => DateTime::Format::Strptime->new(pattern => '%d-%m-%Y'),
h           => DateTime::Format::Strptime->new(pattern => '%H'),
hm          => DateTime::Format::Strptime->new(pattern => '%H:%M'),
hms         => DateTime::Format::Strptime->new(pattern => '%H:%M:%S'),
year        => DateTime::Format::Strptime->new(pattern => '%Y'),
dm          => DateTime::Format::Strptime->new(pattern => '%d/%m'),

USER DEFINED FORMATTERS

You can define the formatters (or override existing ones), in two ways. The first is to provide arguments for DateTime::Format::Strptime constructur using an hashref:

[% USE DtFormatter(
    pstrptime => {
        'jazz' => {
            locale  => 'it_IT',
            pattern => '%d %B %Y'
        },
        'second_hour' => {
            pattern => '%S:%H'
        }
    }
) %]
[% DtFormatter.format(DateTime_object, 'jazz') %]

You can also provide any valid DateTime format object. For instance, if you want an Excel-style date and have a DateTime::Format::Excel object in excel_formatter_obj:

[% USE DtFormatter( formatters =>
    { 'excel' => excel_formatter_obj }
) %]
[% DtFormatter.format(DateTime_object, 'excel') %]

The old formatters parameter is deprecated, issues a warning, and will be removed in version 2.00.

TODO

Tests.

SEE ALSO

Template, DateTime.

AUTHOR

Michele Beltrame, mb@italpro.net.

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.