Skip to content

Commit

Permalink
move the friendly_timezone_name helper into Redmine::I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Jan 16, 2025
1 parent 19f597f commit 96fa068
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 41 deletions.
15 changes: 15 additions & 0 deletions lib_static/redmine/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ def formatted_time_zone_offset(user: User.current)
"UTC#{user.time_zone.now.formatted_offset}"
end

##
# Formats an ActiveSupport::TimeZone object into a user-friendly string.
# @param time_zone [ActiveSupport::TimeZone] The time zone to format.
# @return [String] The formatted time zone string.
def friendly_timezone_name(time_zone)
tz_info = time_zone.tzinfo

if tz_info.canonical_zone.name == "Etc/UTC"
"UTC"
else
friendly_names = ActiveSupport::TimeZone::MAPPING.select { |_, v| v == tz_info.canonical_zone.name }.keys.sort
"(UTC#{ActiveSupport::TimeZone.seconds_to_utc_offset(tz_info.base_utc_offset)}) #{friendly_names.join(', ')}"
end
end

def day_name(day)
::I18n.t("date.day_names")[day % 7]
end
Expand Down
3 changes: 2 additions & 1 deletion modules/costs/app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class TimeEntriesController < ApplicationController
include OpTurbo::ComponentStream
include OpTurbo::DialogStreamHelper
include Redmine::I18n

before_action :require_login

Expand All @@ -57,7 +58,7 @@ def dialog
def user_tz_caption
user = User.visible.find_by(id: params[:user_id])
caption = if user && user.time_zone != User.current.time_zone
I18n.t("notice_different_time_zones", tz: helpers.friendly_timezone_name(user.time_zone))
I18n.t("notice_different_time_zones", tz: friendly_timezone_name(user.time_zone))
else
""
end
Expand Down
38 changes: 0 additions & 38 deletions modules/costs/app/helpers/time_entries_helper.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper.rb")

RSpec.describe TimeEntriesController do
include Redmine::I18n
let(:user) { create(:user) }
let(:other_user) { create(:user) }

Expand Down Expand Up @@ -271,7 +272,7 @@
get :user_tz_caption, params: { user_id: other_user.id }, format: :turbo_stream
expect(response.body).to include(
"caption=\"#{I18n.t('notice_different_time_zones',
tz: described_class.helpers.friendly_timezone_name(other_user.time_zone))}\""
tz: friendly_timezone_name(other_user.time_zone))}\""
)
end
end
Expand Down
4 changes: 3 additions & 1 deletion modules/costs/spec/features/time_entry_dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper.rb")

RSpec.describe "time entry dialog", :js, with_flag: :track_start_and_end_times_for_time_entries do
include Redmine::I18n

shared_let(:project) { create(:project_with_types) }

shared_let(:work_package_a) { create(:work_package, subject: "WP A", project:) }
Expand Down Expand Up @@ -118,7 +120,7 @@
time_logging_modal.update_field("user_id", other_user.name)

time_logging_modal.expect_user(other_user)
time_logging_modal.shows_caption(I18n.t("notice_different_time_zones", tz: "(UTC+09:00) Osaka"))
time_logging_modal.shows_caption(I18n.t("notice_different_time_zones", tz: friendly_timezone_name(other_user.time_zone)))
end
end

Expand Down

0 comments on commit 96fa068

Please sign in to comment.