Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MyApp::View::TT;
- use strict;
- use parent 'Catalyst::View::TT';
- use Scalar::Util qw(blessed);
- use DateTime::Format::DateParse;
- __PACKAGE__->config(
- formats => {
- date => {
- date => '%D',
- short => '%b %e, %G',
- long => '%m/%d/%Y %l:%M %p',
- }
- }
- );
- sub new {
- my ( $class, $c, $arguments ) = @_;
- my $formats = $self->{formats};
- return $class->next::method( $c, $arguments ) unless ref $formats eq 'HASH';
- $class->config->{FILTERS} ||= {};
- my $filters = $class->config->{FILTERS};
- foreach my $key ( keys %$formats ) {
- if ( $key eq 'date' ) {
- foreach my $date_key ( keys %{$formats->{$key}} ) {
- $filters->{"${key}_$date_key"} = sub {
- my $date = shift;
- return unless defined $date;
- unless ( blessed $date and $date->can("stringify") ) {
- $date = DateTime::Format::DateParse->parse_datetime($date);
- }
- unless ( $date ) { return $date; }
- # Only apply a timezone if we have a complete date.
- unless ( "$date" =~ /T00:00:00$/ ) {
- # Set this to your users timezone as appropriate
- $date->set_time_zone('America/Los_Angeles');
- }
- $date->strftime($formats->{$key}->{$date_key});
- };
- }
- }
- }
- return $class->next::method( $c, $arguments );
- }
- 1;
Add Comment
Please, Sign In to add comment