Created
December 28, 2009 22:19
-
-
Save jshirley/264994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment