diff options
author | Matthew Somerville <matthew@mysociety.org> | 2012-10-08 18:18:09 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@firefly.ukcod.org.uk> | 2012-10-08 18:18:29 +0100 |
commit | 7a3b06d61f63f34eed12029c974dc66d6ffbdc87 (patch) | |
tree | 816d6ef3e8189a6af36e672949340be997b06074 /perllib/Utils.pm | |
parent | 0a07e772c8c7682f0b8717bed5a8b7e321c25665 (diff) |
Add TIME_ZONE variable for local display of times.
Diffstat (limited to 'perllib/Utils.pm')
-rw-r--r-- | perllib/Utils.pm | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/perllib/Utils.pm b/perllib/Utils.pm index 23a87485d..8a4c4265d 100644 --- a/perllib/Utils.pm +++ b/perllib/Utils.pm @@ -12,9 +12,9 @@ package Utils; use strict; +use DateTime; use Encode; use File::Slurp qw(); -use POSIX qw(strftime); use mySociety::DBHandle qw(dbh); use mySociety::GeoUtil; use mySociety::Locale; @@ -251,25 +251,32 @@ sub cleanup_text { } sub prettify_epoch { - my ( $s, $type ) = @_; + my ( $epoch, $type ) = @_; $type = 'short' if $type eq '1'; - my @s = localtime($s); + my $dt = DateTime->from_epoch( epoch => $epoch, time_zone => 'local' ); + $dt->set_time_zone( FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); + + my $now = DateTime->now( time_zone => 'local' ); + $now->set_time_zone( FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); + my $tt = ''; - $tt = strftime('%H:%M', @s) unless $type eq 'date'; - my @t = localtime(); - if (strftime('%Y%m%d', @s) eq strftime('%Y%m%d', @t)) { + $tt = $dt->strftime('%H:%M') unless $type eq 'date'; + + if ($dt->strftime('%Y%m%d') eq $now->strftime('%Y%m%d')) { return "$tt " . _('today'); } $tt .= ', ' unless $type eq 'date'; - if (strftime('%Y %U', @s) eq strftime('%Y %U', @t)) { - $tt .= decode_utf8(strftime('%A', @s)); + if ($dt->strftime('%Y %U') eq $now->strftime('%Y %U')) { + $tt .= decode_utf8($dt->strftime('%A')); } elsif ($type eq 'short') { - $tt .= decode_utf8(strftime('%e %b %Y', @s)); - } elsif (strftime('%Y', @s) eq strftime('%Y', @t)) { - $tt .= decode_utf8(strftime('%A %e %B %Y', @s)); + $tt .= decode_utf8($dt->strftime('%e %b %Y')); + } elsif ($dt->strftime('%Y') eq $now->strftime('%Y')) { + $tt .= decode_utf8($dt->strftime('%A %e %B %Y')); } else { - $tt .= decode_utf8(strftime('%a %e %B %Y', @s)); + $tt .= decode_utf8($dt->strftime('%a %e %B %Y')); } return $tt; } |