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

compare to tzinfo time_zones in reminder mail

ulferts3e0cc5df025e
diff --git a/app/cells/settings/time_zone_setting_cell.rb b/app/cells/settings/time_zone_setting_cell.rb
index 931a0d36d08..3ea64edf8fd 100644
--- a/app/cells/settings/time_zone_setting_cell.rb
+++ b/app/cells/settings/time_zone_setting_cell.rb
@@ -43,17 +43,11 @@ module Settings
end
def time_zone_entries
- contract
+ UserPreferences::UpdateContract
.assignable_time_zones
.map do |tz|
[tz.to_s, tz.tzinfo.canonical_identifier]
end
end
-
- private
-
- def contract
- @contract ||= UserPreferences::UpdateContract.new(form.object, User.current)
- end
end
end
diff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rb
index 53cbd7d1b32..d0d9ae4d820 100644
--- a/app/contracts/user_preferences/base_contract.rb
+++ b/app/contracts/user_preferences/base_contract.rb
@@ -51,26 +51,44 @@ module UserPreferences
validate :no_duplicate_workdays,
if: -> { model.workdays.present? }
- ##
- # Returns time zones supported by OpenProject. Those include only the subset of all the
- # TZInfo timezones also handled by ActiveSupport::TimeZone.
- # The reason for this is currently:
- # * the reminder mail implementation which could be amended
- # * the select in the form which only displays ActiveSupport::TimeZone as they are more
- # user friendly.
- # As we only store tzinfo compatible data we only provide options, for which the
- # values can later on be retrieved unambiguously. This is not always the case
- # for values in ActiveSupport::TimeZone since multiple AS zones map to single tzinfo zones.
- def assignable_time_zones
- ActiveSupport::TimeZone
- .all
- .group_by { |tz| tz.tzinfo.name }
- .values
- .map do |zones|
- namesake_time_zone(zones)
+ class << self
+ ##
+ # Returns time zones supported by OpenProject. Those include only the subset of all the
+ # TZInfo timezones also handled by ActiveSupport::TimeZone.
+ # The reason for this is currently:
+ # * the reminder mail implementation which could be amended
+ # * the select in the form which only displays ActiveSupport::TimeZone as they are more
+ # user friendly.
+ # As we only store tzinfo compatible data we only provide options, for which the
+ # values can later on be retrieved unambiguously. This is not always the case
+ # for values in ActiveSupport::TimeZone since multiple AS zones map to single tzinfo zones.
+ def assignable_time_zones
+ ActiveSupport::TimeZone
+ .all
+ .group_by { |tz| tz.tzinfo.name }
+ .values
+ .map do |zones|
+ namesake_time_zone(zones)
+ end
+ end
+
+ private
+
+ # If there are multiple AS::TimeZones for a single TZInfo::Timezone, we
+ # only return the one that is the namesake.
+ def namesake_time_zone(time_zones)
+ if time_zones.length == 1
+ time_zones.first
+ else
+ time_zones.detect { |tz| tz.tzinfo.name.include?(tz.name.gsub(' ', '_')) }
+ end
end
end
+ def assignable_time_zones
+ self.class.assignable_time_zones
+ end
+
protected
def time_zone_correctness
@@ -100,15 +118,5 @@ module UserPreferences
errors.add :workdays, :no_duplicates
end
end
-
- # If there are multiple AS::TimeZones for a single TZInfo::Timezone, we
- # only return the one that is the namesake.
- def namesake_time_zone(time_zones)
- if time_zones.length == 1
- time_zones.first
- else
- time_zones.detect { |tz| tz.tzinfo.name.include?(tz.name.gsub(' ', '_')) }
- end
- end
end
end
diff --git a/app/models/users/scopes/having_reminder_mail_to_send.rb b/app/models/users/scopes/having_reminder_mail_to_send.rb
index ab1870d7f38..3bfa2f349b3 100644
--- a/app/models/users/scopes/having_reminder_mail_to_send.rb
+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb
@@ -68,14 +68,16 @@ module Users::Scopes
# Joins the times local to the user preferences and then checks whether:
# * reminders are enabled
# * any of the configured reminder time is the local time
+ # * the local workday is enabled to receive a reminder on
# If no time zone is present, utc is assumed.
# If no reminder settings are present, sending a reminder at 08:00 local time is assumed.
+ # If no workdays are specified, 1 - 5 is assumed which is represents Monday to Friday.
times_sql = arel_table
.grouping(Arel::Nodes::ValuesList.new(local_times))
.as('t(time, zone, workday)')
<<~SQL.squish
JOIN (SELECT * FROM #{times_sql.to_sql}) AS local_times
- ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
+ ON COALESCE(user_preferences.settings->>'time_zone', 'Etc/UTC') = local_times.zone
AND (
user_preferences.settings->'workdays' @> to_jsonb(local_times.workday)
OR (
@@ -104,23 +106,23 @@ module Users::Scopes
end
def times_for_zones(times)
- ActiveSupport::TimeZone
- .all
+ UserPreferences::UpdateContract
+ .assignable_time_zones
.map do |z|
- times.map do |time|
- local_time = time.in_time_zone(z)
+ times.map do |time|
+ local_time = time.in_time_zone(z)
- # Get the iso weekday of the current time to check
- # which users have it enabled as a workday
- workday = local_time.to_date.cwday
+ # Get the iso weekday of the current time to check
+ # which users have it enabled as a workday
+ workday = local_time.to_date.cwday
- # Since only full hours can be configured, we can disregard any local time that is not
- # a full hour.
- next if local_time.min != 0
+ # Since only full hours can be configured, we can disregard any local time that is not
+ # a full hour.
+ next if local_time.min != 0
- [local_time.strftime('%H:00:00+00:00'), z.name.gsub("'", "''"), workday]
+ [local_time.strftime('%H:00:00+00:00'), z.tzinfo.canonical_zone.name, workday]
+ end
end
- end
.flatten(1)
.compact
end
diff --git a/spec/features/notifications/reminder_mail_spec.rb b/spec/features/notifications/reminder_mail_spec.rb
index 69080d5c008..f5e7658e802 100644
--- a/spec/features/notifications/reminder_mail_spec.rb
+++ b/spec/features/notifications/reminder_mail_spec.rb
@@ -14,17 +14,17 @@ describe "Reminder email sending", type: :feature, js: true do
# time zone. For the time zone Hawaii (UTC-10) this means between 8:00:00 and 8:14:59 UTC.
# The job is scheduled to run every 15 min so the run_at will in production always move between the quarters of an hour.
# The current time can be way behind that.
- let(:current_utc_time) { ActiveSupport::TimeZone['Hawaii'].parse("08:34:10").utc }
- let(:job_run_at) { ActiveSupport::TimeZone['Hawaii'].parse("08:00").utc }
+ let(:current_utc_time) { ActiveSupport::TimeZone['Pacific/Honolulu'].parse("08:34:10").utc }
+ let(:job_run_at) { ActiveSupport::TimeZone['Pacific/Honolulu'].parse("08:00").utc }
current_user do
FactoryBot.create(
:user,
preferences: {
- time_zone: "Hawaii",
+ time_zone: 'Pacific/Honolulu',
daily_reminders: {
enabled: true,
- times: [hitting_reminder_slot_for("Hawaii", current_utc_time)]
+ times: [hitting_reminder_slot_for('Pacific/Honolulu', current_utc_time)]
}
},
notification_settings: [
diff --git a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
index 5a4c6d24357..f9227026920 100644
--- a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
+++ b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
@@ -50,9 +50,9 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {
- time_zone: "Paris",
+ time_zone: "Europe/Paris",
workdays: paris_user_workdays,
daily_reminders: paris_user_daily_reminders
}
@@ -62,7 +62,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user_daily_reminders) do
{
enabled: true,
- times: [hitting_reminder_slot_for("Paris", current_time)]
+ times: [hitting_reminder_slot_for("Europe/Paris", current_time)]
}
end
let(:notifications) { FactoryBot.create(:notification, recipient: paris_user, created_at: 5.minutes.ago) }
@@ -101,17 +101,17 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
context 'for a user whose local time is on the previous workday' do
- # 8:00 thursday UTC = 22:00 wednesday Hawaii
+ # 8:00 thursday Etc/UTC = 22:00 wednesday Pacific/Honolulu
let(:hawaii_user) do
FactoryBot.create(
:user,
- firstname: 'Hawaii',
+ firstname: 'Pacific/Honolulu',
preferences: {
- time_zone: "Hawaii",
+ time_zone: "Pacific/Honolulu",
workdays: hawaii_user_workdays,
daily_reminders: {
enabled: true,
- times: [hitting_reminder_slot_for("Hawaii", current_time)]
+ times: [hitting_reminder_slot_for("Pacific/Honolulu", current_time)]
}
}
)
@@ -138,20 +138,20 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
context 'for a user whose local time is on the next workday' do
- # 12:00 thursday UTC = 03:00 friday @ Samoa
+ # 12:00 thursday Etc/UTC = 03:00 friday @ Pacific/Apia
let(:current_time) { "2021-09-30T12:05:59Z".to_datetime }
let(:scope_time) { "2021-09-30T12:00:00Z".to_datetime }
let(:samoa_user) do
FactoryBot.create(
:user,
- firstname: 'Samoa',
+ firstname: 'Pacific/Apia',
preferences: {
- time_zone: "Samoa",
+ time_zone: "Pacific/Apia",
workdays: samoa_user_workdays,
daily_reminders: {
enabled: true,
- times: [hitting_reminder_slot_for("Samoa", current_time)]
+ times: [hitting_reminder_slot_for("Pacific/Apia", current_time)]
}
}
)
@@ -181,12 +181,12 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:moscow_user) do
FactoryBot.create(
:user,
- firstname: 'Moscow',
+ firstname: 'Europe/Moscow',
preferences: {
- time_zone: "Moscow",
+ time_zone: "Europe/Moscow",
daily_reminders: {
enabled: true,
- times: [hitting_reminder_slot_for("Moscow", current_time)]
+ times: [hitting_reminder_slot_for("Europe/Moscow", current_time)]
}
}
)
@@ -207,9 +207,9 @@ describe User, '.having_reminder_mail_to_send', type: :model do
{
enabled: true,
times: [
- hitting_reminder_slot_for("Paris", current_time - 3.hours),
- hitting_reminder_slot_for("Paris", current_time),
- hitting_reminder_slot_for("Paris", current_time + 3.hours)
+ hitting_reminder_slot_for("Europe/Paris", current_time - 3.hours),
+ hitting_reminder_slot_for("Europe/Paris", current_time),
+ hitting_reminder_slot_for("Europe/Paris", current_time + 3.hours)
]
}
end
@@ -225,12 +225,12 @@ describe User, '.having_reminder_mail_to_send', type: :model do
{
enabled: true,
times: [
- hitting_reminder_slot_for("Paris", current_time - 2.hours),
- hitting_reminder_slot_for("Paris", current_time + 3.hours)
+ hitting_reminder_slot_for("Europe/Paris", current_time - 2.hours),
+ hitting_reminder_slot_for("Europe/Paris", current_time + 3.hours)
]
}
end
- let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("06:00") }
+ let(:scope_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("06:00") }
it 'contains the user' do
expect(scope)
@@ -243,8 +243,8 @@ describe User, '.having_reminder_mail_to_send', type: :model do
{
enabled: true,
times: [
- hitting_reminder_slot_for("Paris", current_time - 3.hours),
- hitting_reminder_slot_for("Paris", current_time + 1.hour)
+ hitting_reminder_slot_for("Europe/Paris", current_time - 3.hours),
+ hitting_reminder_slot_for("Europe/Paris", current_time + 1.hour)
]
}
end
@@ -271,7 +271,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user_daily_reminders) do
{
enabled: false,
- times: [hitting_reminder_slot_for("Paris", current_time)]
+ times: [hitting_reminder_slot_for("Europe/Paris", current_time)]
}
end
@@ -285,14 +285,14 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {
- time_zone: "Paris"
+ time_zone: "Europe/Paris"
}
)
end
- let(:current_time) { ActiveSupport::TimeZone['Paris'].parse("2021-09-30T08:09").utc }
- let(:scope_time) { ActiveSupport::TimeZone['Paris'].parse("2021-09-30T08:00") }
+ let(:current_time) { ActiveSupport::TimeZone['Europe/Paris'].parse("2021-09-30T08:09").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Europe/Paris'].parse("2021-09-30T08:00") }
it 'contains the user' do
expect(scope)
@@ -304,14 +304,14 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {
- time_zone: "Paris"
+ time_zone: "Europe/Paris"
}
)
end
- let(:current_time) { ActiveSupport::TimeZone['Paris'].parse("2021-09-30T10:00").utc }
- let(:scope_time) { ActiveSupport::TimeZone['Paris'].parse("2021-09-30T10:00") }
+ let(:current_time) { ActiveSupport::TimeZone['Europe/Paris'].parse("2021-09-30T10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Europe/Paris'].parse("2021-09-30T10:00") }
it 'is empty' do
expect(scope)
@@ -323,9 +323,9 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:kathmandu_user) do
FactoryBot.create(
:user,
- firstname: 'Kathmandu',
+ firstname: 'Asia/Kathmandu',
preferences: {
- time_zone: "Kathmandu",
+ time_zone: "Asia/Kathmandu",
daily_reminders: {
enabled: true,
times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]
@@ -351,9 +351,9 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:kathmandu_user) do
FactoryBot.create(
:user,
- firstname: 'Kathmandu',
+ firstname: 'Asia/Kathmandu',
preferences: {
- time_zone: "Kathmandu",
+ time_zone: "Asia/Kathmandu",
daily_reminders: {
enabled: true,
times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]
@@ -379,9 +379,9 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:kathmandu_user) do
FactoryBot.create(
:user,
- firstname: 'Kathmandu',
+ firstname: 'Asia/Kathmandu',
preferences: {
- time_zone: "Kathmandu",
+ time_zone: "Asia/Kathmandu",
daily_reminders: {
enabled: true,
times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]
@@ -444,7 +444,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user_daily_reminders) do
{
enabled: true,
- times: [hitting_reminder_slot_for("Paris", current_time + 1.hour)]
+ times: [hitting_reminder_slot_for("Europe/Paris", current_time + 1.hour)]
}
end
@@ -458,7 +458,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user_daily_reminders) do
{
enabled: true,
- times: [hitting_reminder_slot_for("Paris", current_time - 1.hour)]
+ times: [hitting_reminder_slot_for("Europe/Paris", current_time - 1.hour)]
}
end
@@ -472,17 +472,17 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {
daily_reminders: {
enabled: true,
- times: [hitting_reminder_slot_for("UTC", current_time)]
+ times: [hitting_reminder_slot_for("Etc/UTC", current_time)]
}
}
)
end
- it 'is including the user as UTC is assumed' do
+ it 'is including the user as Etc/UTC is assumed' do
expect(scope)
.to match_array([paris_user])
end
@@ -492,13 +492,13 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {}
)
end
- let(:current_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T08:00").utc }
+ let(:current_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T08:00").utc }
- it 'is including the user as UTC at 08:00 is assumed' do
+ it 'is including the user as Etc/UTC at 08:00 is assumed' do
expect(scope)
.to match_array([paris_user])
end
@@ -508,14 +508,14 @@ describe User, '.having_reminder_mail_to_send', type: :model do
let(:paris_user) do
FactoryBot.create(
:user,
- firstname: 'Paris',
+ firstname: 'Europe/Paris',
preferences: {}
)
end
- let(:current_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T10:00").utc }
- let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T10:00").utc }
+ let(:current_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T10:00").utc }
- it 'is empty as UTC at 08:00 is assumed' do
+ it 'is empty as Etc/UTC at 08:00 is assumed' do
expect(scope)
.to be_empty
end
@@ -531,28 +531,28 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
context 'for a user without preferences at 08:00' do
- let(:current_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T08:00").utc }
- let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T08:00").utc }
+ let(:current_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T08:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T08:00").utc }
before do
paris_user.pref.destroy
end
- it 'is including the user as UTC at 08:00 is assumed' do
+ it 'is including the user as Etc/UTC at 08:00 is assumed' do
expect(scope)
.to match_array([paris_user])
end
end
context 'for a user without preferences at 10:00' do
- let(:current_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T10:00").utc }
- let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("2021-09-30T10:00").utc }
+ let(:current_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Etc/UTC'].parse("2021-09-30T10:00").utc }
before do
paris_user.pref.destroy
end
- it 'is empty as UTC at 08:00 is assumed' do
+ it 'is empty as Etc/UTC at 08:00 is assumed' do
expect(scope)
.to be_empty
end