mirror of
https://github.com/rickbarrette/stopwatch.git
synced 2026-11-17 11:50:36 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
82ffe7ed49
commit
9c937c6d6b
7 files changed
+41
-15
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 %>
|
||||
|
||||
@@ -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:
|
||||
|
||||
+9
-4
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
+21
-3
@@ -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
|
||||
Reference in new issue
Block a user