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/Install.pm | 4 ++- Bugzilla/Install/Requirements.pm | 6 ++++ Bugzilla/Template.pm | 10 +++++- Bugzilla/User.pm | 22 ++++++++++++ Bugzilla/User/Setting/Timezone.pm | 71 +++++++++++++++++++++++++++++++++++++++ Bugzilla/Util.pm | 44 +++++++++++++----------- 6 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 Bugzilla/User/Setting/Timezone.pm (limited to 'Bugzilla') diff --git a/Bugzilla/Install.pm b/Bugzilla/Install.pm index c70f8a8bc..3d382add8 100644 --- a/Bugzilla/Install.pm +++ b/Bugzilla/Install.pm @@ -62,7 +62,9 @@ sub SETTINGS { default => ${Bugzilla->languages}[0] }, # 2007-07-02 altlist@gmail.com -- Bug 225731 quote_replies => { options => ['quoted_reply', 'simple_reply', 'off'], - default => "quoted_reply" } + default => "quoted_reply" }, + # 2008-08-27 LpSolit@gmail.com -- Bug 182238 + timezone => { subclass => 'Timezone', default => 'local' }, } }; diff --git a/Bugzilla/Install/Requirements.pm b/Bugzilla/Install/Requirements.pm index 2216d963d..9ff26246e 100644 --- a/Bugzilla/Install/Requirements.pm +++ b/Bugzilla/Install/Requirements.pm @@ -69,6 +69,12 @@ sub REQUIRED_MODULES { module => 'Date::Format', version => '2.21' }, + # 0.28 fixed some important bugs in DateTime. + { + package => 'DateTime', + module => 'DateTime', + version => '0.28' + }, { package => 'PathTools', module => 'File::Spec', diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index 8a322fae5..76ff9f11d 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -604,7 +604,15 @@ sub create { }, # Format a time for display (more info in Bugzilla::Util) - time => \&Bugzilla::Util::format_time, + time => [ sub { + my ($context, $format) = @_; + return sub { + my $time = shift; + return format_time($time, $format); + }; + }, + 1 + ], # Bug 120030: Override html filter to obscure the '@' in user # visible strings. diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index ccee85755..7f59b749c 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -50,6 +50,7 @@ use Bugzilla::Classification; use Bugzilla::Field; use Scalar::Util qw(blessed); +use DateTime::TimeZone; use base qw(Bugzilla::Object Exporter); @Bugzilla::User::EXPORT = qw(is_available_username @@ -349,6 +350,22 @@ sub settings { return $self->{'settings'}; } +sub timezone { + my $self = shift; + + if (!defined $self->{timezone}) { + my $tz = $self->settings->{timezone}->{value}; + if ($tz eq 'local') { + # The user wants the local timezone of the server. + $self->{timezone} = Bugzilla->local_timezone; + } + else { + $self->{timezone} = DateTime::TimeZone->new(name => $tz); + } + } + return $self->{timezone}; +} + sub flush_queries_cache { my $self = shift; @@ -1884,6 +1901,11 @@ value - the value of this setting for this user. Will be the same is_default - a boolean to indicate whether the user has chosen to make a preference for themself or use the site default. +=item C + +Returns the timezone used to display dates and times to the user, +as a DateTime::TimeZone object. + =item C Returns an arrayref of L objects representing diff --git a/Bugzilla/User/Setting/Timezone.pm b/Bugzilla/User/Setting/Timezone.pm new file mode 100644 index 000000000..d75b1113b --- /dev/null +++ b/Bugzilla/User/Setting/Timezone.pm @@ -0,0 +1,71 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# The Initial Developer of the Original Code is Frédéric Buclin. +# Portions created by Frédéric Buclin are Copyright (c) 2008 Frédéric Buclin. +# All rights reserved. +# +# Contributor(s): Frédéric Buclin + +package Bugzilla::User::Setting::Timezone; + +use strict; + +use DateTime::TimeZone; + +use base qw(Bugzilla::User::Setting); + +use Bugzilla::Constants; + +sub legal_values { + my ($self) = @_; + + return $self->{'legal_values'} if defined $self->{'legal_values'}; + + my @timezones = DateTime::TimeZone->all_names; + # Remove old formats, such as CST6CDT, EST, EST5EDT. + @timezones = grep { $_ =~ m#.+/.+#} @timezones; + # Append 'local' to the list, which will use the timezone + # given by the server. + push(@timezones, 'local'); + + return $self->{'legal_values'} = \@timezones; +} + +1; + +__END__ + +=head1 NAME + +Bugzilla::User::Setting::Timezone - Object for a user preference setting for desired timezone + +=head1 DESCRIPTION + +Timezone.pm extends Bugzilla::User::Setting and implements a class specialized for +setting the desired timezone. + +=head1 METHODS + +=over + +=item C + +Description: Returns all legal timezones + +Params: none + +Returns: A reference to an array containing the names of all legal timezones + +=back 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