From 9c937c6d6b4410de7b3da9550a78c2469d19b933 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 2 Nov 2026 18:07:44 -0500 Subject: [PATCH] Closing time in local time for users without a time zone Users without a time zone in their preferences fell back to UTC, so a 17:00 closing time stopped their timers hours early (e.g. at noon on a US Eastern server). Like Redmine (User#today), they now get the server's local time: for closing time, the Running timers page's "stopped at" field, and the "Timer stopped by" note. Co-Authored-By: Claude Opus 5.5 --- .../stopwatch_running_timers_controller.rb | 6 +++-- .../stopwatch_running_timers/index.html.erb | 4 ++-- config/locales/en.yml | 2 +- lib/stopwatch.rb | 13 ++++++---- lib/stopwatch/timer.rb | 4 ++-- test/integration/running_timers_test.rb | 3 ++- test/unit/timer_test.rb | 24 ++++++++++++++++--- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/controllers/stopwatch_running_timers_controller.rb b/app/controllers/stopwatch_running_timers_controller.rb index 108c278..5476ae3 100644 --- a/app/controllers/stopwatch_running_timers_controller.rb +++ b/app/controllers/stopwatch_running_timers_controller.rb @@ -40,9 +40,11 @@ class StopwatchRunningTimersController < StopwatchController User.current.allowed_to?(:view_running_timers, nil, global: true) or deny_access end - # The "stopped at" field is a local date and time in the viewer's time zone + # The "stopped at" field is a local date and time in the viewer's time zone, or the server's local time for + # a viewer without one (as Redmine shows times) def parse_user_time(value) - (User.current.time_zone || Time.zone).parse(value.to_s) + zone = User.current.time_zone + zone ? zone.parse(value.to_s) : Time.parse(value.to_s) rescue ArgumentError nil end diff --git a/app/views/stopwatch_running_timers/index.html.erb b/app/views/stopwatch_running_timers/index.html.erb index b0c528f..c3e0716 100644 --- a/app/views/stopwatch_running_timers/index.html.erb +++ b/app/views/stopwatch_running_timers/index.html.erb @@ -45,8 +45,8 @@ <% if row.started_at %> <%# Optional: when the person actually stopped working; blank = now %> <%= datetime_local_field_tag :stopped_at, nil, - min: row.started_at.in_time_zone(User.current.time_zone || Time.zone).strftime('%Y-%m-%dT%H:%M'), - max: Time.now.in_time_zone(User.current.time_zone || Time.zone).strftime('%Y-%m-%dT%H:%M'), + min: User.current.convert_time_to_user_timezone(row.started_at).strftime('%Y-%m-%dT%H:%M'), + max: User.current.convert_time_to_user_timezone(Time.now).strftime('%Y-%m-%dT%H:%M'), title: l(:text_stopwatch_stopped_at), 'aria-label': l(:label_stopwatch_stopped_at), id: "stopped_at_#{row.user.id}" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 60a3e82..5ffbe93 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,7 +30,7 @@ en: label_max_timer_hours: "Maximum timer length (hours)" text_max_timer_hours: "A timer running longer than this is stopped at the limit and its entry is flagged in the comment. Leave blank or 0 for no limit." label_stop_time: "Closing time" - text_stop_time: "Timers started before this time stop at it (in each user's time zone), so no time is logged after closing; the entry's comment notes it. Timers started after closing (overtime) keep running, up to the maximum timer length. Leave blank for no closing time." + text_stop_time: "Timers started before this time stop at it (in each user's time zone, or the server's for users without one), so no time is logged after closing; the entry's comment notes it. Timers started after closing (overtime) keep running, up to the maximum timer length. Leave blank for no closing time." label_system: 'Use system default' stopwatch_timers: entries_list: diff --git a/lib/stopwatch.rb b/lib/stopwatch.rb index f0ad03a..2c485ea 100644 --- a/lib/stopwatch.rb +++ b/lib/stopwatch.rb @@ -61,14 +61,19 @@ module Stopwatch hour < 24 && minute < 60 ? [hour, minute] : nil end - # Closing time on the day of the given time, in the user's time zone, or nil when no stop time is set + # Closing time on the day of the given time, in the user's time zone, or nil when no stop time is set. + # A user without a time zone gets the server's local time, as Redmine does (User#today), not UTC. def self.closing_time_on(time, user) hour, minute = stop_time return nil unless hour - zone = user.time_zone || Time.zone - day = time.in_time_zone(zone) - zone.local(day.year, day.month, day.day, hour, minute) + if (zone = user.time_zone) + day = time.in_time_zone(zone) + zone.local(day.year, day.month, day.day, hour, minute) + else + day = time.getlocal + Time.local(day.year, day.month, day.day, hour, minute) + end end # Colour-code the issue Start/Stop links (green to start, red to stop) diff --git a/lib/stopwatch/timer.rb b/lib/stopwatch/timer.rb index be3d4cb..6f38e93 100644 --- a/lib/stopwatch/timer.rb +++ b/lib/stopwatch/timer.rb @@ -81,9 +81,9 @@ module Stopwatch end if entry && by && by != user - zone = user.time_zone || Time.zone + # The user's time zone, or the server's local time for a user without one (as Redmine shows times) note = I18n.t(:text_stopwatch_timer_stopped_by, name: by.name, - time: stopped_at.in_time_zone(zone).strftime('%Y-%m-%d %H:%M')) + time: user.convert_time_to_user_timezone(stopped_at).strftime('%Y-%m-%d %H:%M')) entry.update_columns(comments: [entry.comments.to_s.presence, note].compact.join(' ').truncate(1024)) end diff --git a/test/integration/running_timers_test.rb b/test/integration/running_timers_test.rb index 8b6d8bc..06b3f25 100644 --- a/test/integration/running_timers_test.rb +++ b/test/integration/running_timers_test.rb @@ -54,7 +54,8 @@ class RunningTimersTest < Redmine::IntegrationTest get '/stopwatch/running_timers' assert_select "tr#stopwatch-running-timer-#{@worker.id} form.stopwatch-stop-form" - left_at = (2.hours.ago).in_time_zone(Time.zone).strftime('%Y-%m-%dT%H:%M') # 1 hour after starting + # jsmith has no time zone, so the field is in the server's local time + left_at = 2.hours.ago.getlocal.strftime('%Y-%m-%dT%H:%M') # 1 hour after starting post "/stopwatch/running_timers/#{@worker.id}/stop", params: { stopped_at: left_at } assert_redirected_to '/stopwatch/running_timers' diff --git a/test/unit/timer_test.rb b/test/unit/timer_test.rb index 73ca2de..0449bdf 100644 --- a/test/unit/timer_test.rb +++ b/test/unit/timer_test.rb @@ -203,9 +203,13 @@ class TimerTest < ActiveSupport::TestCase # Closing time: timers started before closing stop at it; timers started after closing are overtime def closing_today(hour = 17) - zone = @user.time_zone || Time.zone - now = Time.now.in_time_zone(zone) - zone.local(now.year, now.month, now.day, hour, 0) + if (zone = @user.time_zone) + now = Time.now.in_time_zone(zone) + zone.local(now.year, now.month, now.day, hour, 0) + else + now = Time.now + Time.local(now.year, now.month, now.day, hour, 0) + end end test "timer started before closing stops at closing time" do @@ -235,4 +239,18 @@ class TimerTest < ActiveSupport::TestCase ensure Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '') end + + # Redmine treats a user without a time zone as the server's local time (User#today), not UTC + test "closing time without a user time zone is in the server's local time" do + Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '17:00') + @user.pref.update!(time_zone: '') + user = User.find(@user.id) + assert_nil user.time_zone + + closing = Stopwatch.closing_time_on(Time.now, user) + assert_equal [17, 0], [closing.getlocal.hour, closing.getlocal.min] + assert_equal Time.now.to_date, closing.getlocal.to_date + ensure + Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '') + end end