From 615cfb88c1aae1f8372b323e9c53a84d8c9146fe Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Wed, 27 Aug 2008 07:32:11 +0000 Subject: Bug 182238: Allow users to choose what time zone to display times in - Patch by Frédéric Buclin r/a=mkanat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bugzilla/Util.pm | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'Bugzilla/Util.pm') diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 1e7dbf8d1..a8c595a05 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -50,6 +50,7 @@ use Bugzilla::Constants; use Date::Parse; use Date::Format; +use DateTime; use Text::Wrap; # This is from the perlsec page, slightly modified to remove a warning @@ -398,36 +399,39 @@ sub wrap_hard { sub format_time { my ($date, $format) = @_; - # If $format is undefined, try to guess the correct date format. - my $show_timezone; + # If $format is undefined, try to guess the correct date format. if (!defined($format)) { if ($date =~ m/^(\d{4})[-\.](\d{2})[-\.](\d{2}) (\d{2}):(\d{2})(:(\d{2}))?$/) { my $sec = $7; if (defined $sec) { - $format = "%Y-%m-%d %T"; + $format = "%Y-%m-%d %T %Z"; } else { - $format = "%Y-%m-%d %R"; + $format = "%Y-%m-%d %R %Z"; } } else { - # Default date format. See Date::Format for other formats available. - $format = "%Y-%m-%d %R"; + # Default date format. See DateTime for other formats available. + $format = "%Y-%m-%d %R %Z"; } - # By default, we want the timezone to be displayed. - $show_timezone = 1; } - else { - # Search for %Z or %z, meaning we want the timezone to be displayed. - # Till bug 182238 gets fixed, we assume Bugzilla->params->{'timezone'} - # is used. - $show_timezone = ($format =~ s/\s?%Z$//i); - } - - # str2time($date) is undefined if $date has an invalid date format. - my $time = str2time($date); - if (defined $time) { - $date = time2str($format, $time); - $date .= " " . Bugzilla->params->{'timezone'} if $show_timezone; + # strptime($date) returns an empty array if $date has an invalid date format. + my @time = strptime($date); + + if (scalar @time) { + # strptime() counts years from 1900, and months from 0 (January). + # We have to fix both values. + my $dt = DateTime->new({year => 1900 + $time[5], + month => ++$time[4], + day => $time[3], + hour => $time[2], + minute => $time[1], + second => $time[0], + # Use the timezone specified by the server. + time_zone => Bugzilla->local_timezone}); + + # Now display the date using the user's timezone. + $dt->set_time_zone(Bugzilla->user->timezone); + $date = $dt->strftime($format); } else { # Don't let invalid (time) strings to be passed to templates! -- cgit v1.2.3-24-g4f1b