Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use UTC time zone as user pref if non is explicitly configured

ulferts6298b8762de4
diff --git a/app/components/settings/time_zone_setting_component.rb b/app/components/settings/time_zone_setting_component.rb
index 674ad6a53ce..772c8e35b01 100644
--- a/app/components/settings/time_zone_setting_component.rb
+++ b/app/components/settings/time_zone_setting_component.rb
@@ -30,7 +30,7 @@
module Settings
##
- # A text field to enter numeric values.
+ # A select field to select a time zone from.
class TimeZoneSettingComponent < ::ApplicationComponent
options :form, :title
options container_class: "-wide"
diff --git a/app/models/anonymous_user.rb b/app/models/anonymous_user.rb
index 27e4514d656..e73178ebddf 100644
--- a/app/models/anonymous_user.rb
+++ b/app/models/anonymous_user.rb
@@ -49,7 +49,7 @@ class AnonymousUser < User
def mail; nil end
- def time_zone; nil end
+ def time_zone; ActiveSupport::TimeZone["Etc/UTC"] end
def rss_key; nil end
diff --git a/app/models/user.rb b/app/models/user.rb
index 23e85c02b3b..7d6bc21a5f7 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -398,7 +398,7 @@ class User < Principal
end
def log_successful_login
- update_attribute(:last_login_on, Time.now)
+ update_attribute(:last_login_on, Time.current)
end
def pref
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb
index a2f49ede552..769ff84773f 100644
--- a/app/models/user_preference.rb
+++ b/app/models/user_preference.rb
@@ -132,7 +132,7 @@ class UserPreference < ApplicationRecord
end
def time_zone
- super.presence || Setting.user_default_timezone.presence
+ super.presence || Setting.user_default_timezone.presence || "Etc/UTC"
end
def daily_reminders
diff --git a/app/views/users/_preferences.html.erb b/app/views/users/_preferences.html.erb
index 44a513b0527..45399a77431 100644
--- a/app/views/users/_preferences.html.erb
+++ b/app/views/users/_preferences.html.erb
@@ -30,8 +30,10 @@ See COPYRIGHT and LICENSE files for more details.
<%= render Settings::TimeZoneSettingComponent.new(
"time_zone",
form: pref_fields,
+ include_blank: false,
container_class: (defined? input_size) ? "-#{input_size}" : "-wide"
)
+
%>
<div class="form--field">
<%= pref_fields.select :theme, theme_options_for_select, container_class: '-middle' %>
diff --git a/lib_static/redmine/i18n.rb b/lib_static/redmine/i18n.rb
index 6604ba31fc6..5c107025668 100644
--- a/lib_static/redmine/i18n.rb
+++ b/lib_static/redmine/i18n.rb
@@ -138,17 +138,14 @@ module Redmine
time = time.to_time if time.is_a?(String)
zone = User.current.time_zone
- local = if zone
- time.in_time_zone(zone)
- else
- (time.utc? ? time.to_time.localtime : time)
- end
+ local = time.in_time_zone(zone)
+
(include_date ? "#{format_date(local)} " : "") +
(Setting.time_format.blank? ? ::I18n.l(local, format: :time) : local.strftime(Setting.time_format))
end
def format_time_zone
- (User.current.time_zone || Time.zone).to_s[/\((.*?)\)/m, 1]
+ User.current.time_zone.to_s[/\((.*?)\)/m, 1]
end
def day_name(day)
diff --git a/spec/lib/redmine/i18n_spec.rb b/spec/lib/redmine/i18n_spec.rb
index 384a08475f3..e2dabf1727b 100644
--- a/spec/lib/redmine/i18n_spec.rb
+++ b/spec/lib/redmine/i18n_spec.rb
@@ -273,7 +273,10 @@ module OpenProject
time_format: "%H %M",
date_format: "%d %m %Y"
} do
- let!(:now) { Time.parse("2011-02-20 15:45:22") }
+ let(:user_time_zone) { "" }
+ let(:now) { Time.zone.parse("2011-02-20 15:45:22") }
+
+ current_user { build_stubbed(:user, preferences: { time_zone: user_time_zone }) }
it "with date and hours" do
expect(format_time(now))
@@ -285,14 +288,19 @@ module OpenProject
.to eql now.strftime("%H %M")
end
- it "with a utc to date and hours" do
- expect(format_time(now.utc))
- .to eql now.localtime.strftime("%d %m %Y %H %M")
- end
+ context "with another time zone configured for the user" do
+ # Kathmandu has a +05:45 offset
+ let(:user_time_zone) { "Kathmandu" }
- it "with a utce to only hours" do
- expect(format_time(now.utc, false))
- .to eql now.localtime.strftime("%H %M")
+ it "renders correctly for data and hours" do
+ expect(format_time(now))
+ .to eql "20 02 2011 21 30"
+ end
+
+ it "renders correctly for only hours" do
+ expect(format_time(now, false))
+ .to eql "21 30"
+ end
end
context "with a different format defined", with_settings: {
diff --git a/spec/models/user_preference_spec.rb b/spec/models/user_preference_spec.rb
index b0cd3fdff46..ee7c3286a1b 100644
--- a/spec/models/user_preference_spec.rb
+++ b/spec/models/user_preference_spec.rb
@@ -222,4 +222,26 @@ RSpec.describe UserPreference do
expect(subject[:auto_hide_popups]).to eql(value_auto_hide_popups)
end
end
+
+ describe "#time_zone" do
+ context "with a time zone set and a default configured", with_settings: { user_default_timezone: "America/Los_Angeles" } do
+ let(:settings) { { "time_zone" => "Africa/Algiers" } }
+
+ it "returns the time zone set" do
+ expect(preference.time_zone).to eql "Africa/Algiers"
+ end
+ end
+
+ context "with no time zone configured but a default", with_settings: { user_default_timezone: "America/Los_Angeles" } do
+ it "returns the default time zone" do
+ expect(preference.time_zone).to eql "America/Los_Angeles"
+ end
+ end
+
+ context "with neiter a time zone configured nor a default one", with_settings: { user_default_timezone: "" } do
+ it "returns UTC" do
+ expect(preference.time_zone).to eql "Etc/UTC"
+ end
+ end
+ end
end