Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. package MyApp::View::TT;
  2.  
  3. use strict;
  4.  
  5. use parent 'Catalyst::View::TT';
  6.  
  7. use Scalar::Util qw(blessed);
  8. use DateTime::Format::DateParse;
  9.  
  10. __PACKAGE__->config(
  11. formats => {
  12. date => {
  13. date => '%D',
  14. short => '%b %e, %G',
  15. long => '%m/%d/%Y %l:%M %p',
  16. }
  17. }
  18. );
  19.  
  20. sub new {
  21. my ( $class, $c, $arguments ) = @_;
  22. my $formats = $self->{formats};
  23.  
  24. return $class->next::method( $c, $arguments ) unless ref $formats eq 'HASH';
  25.  
  26. $class->config->{FILTERS} ||= {};
  27.  
  28. my $filters = $class->config->{FILTERS};
  29.  
  30. foreach my $key ( keys %$formats ) {
  31. if ( $key eq 'date' ) {
  32. foreach my $date_key ( keys %{$formats->{$key}} ) {
  33. $filters->{"${key}_$date_key"} = sub {
  34. my $date = shift;
  35. return unless defined $date;
  36. unless ( blessed $date and $date->can("stringify") ) {
  37. $date = DateTime::Format::DateParse->parse_datetime($date);
  38. }
  39. unless ( $date ) { return $date; }
  40. # Only apply a timezone if we have a complete date.
  41. unless ( "$date" =~ /T00:00:00$/ ) {
  42. # Set this to your users timezone as appropriate
  43. $date->set_time_zone('America/Los_Angeles');
  44. }
  45. $date->strftime($formats->{$key}->{$date_key});
  46. };
  47. }
  48. }
  49. }
  50.  
  51. return $class->next::method( $c, $arguments );
  52. }
  53.  
  54. 1;
Add Comment
Please, Sign In to add comment