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 patterns for DateTime::Format::Strptime using an hashref:
[% USE DtFormatter( patterns => { 'jazz' => '%H - %Y' } ) %]
[% DtFormatter.format(DateTime_object, 'jazz') %]
You can also provide any valid DateTime format object. For instance, if you want an Excel-style date:
[% USE DtFormatter( formatters =>
{ 'excel' => DateTime->Format->Excel->new() }
) %]
[% DtFormatter.format(DateTime_object, 'excel') %]
SEE ALSO
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.