Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created December 28, 2009 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jshirley/264994 to your computer and use it in GitHub Desktop.
Save jshirley/264994 to your computer and use it in GitHub Desktop.
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