Add a closing time that stops timers

A new Closing time setting (e.g. 17:00, in each user's time zone): a timer started before closing stops
at closing time and its entry's comment notes it, so no time is logged after closing, even when nobody
stops it until the next day. A timer started after closing is overtime and keeps running, up to the
maximum timer length.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-31 08:41:57 -04:00
1 parent 7266f7842a
commit 7349d9cf5a
6 files changed
+108 -4

No files matched your search

+3
View File
@@ -31,6 +31,9 @@ Rick Barrette (2026):
- **Timer safety (October 2026):**
- *Maximum timer length* setting (default 10 hours): a forgotten timer is stopped at the limit and
its time entry's comment is flagged, instead of adding a night or weekend of hours.
- *Closing time* setting (e.g. 17:00, in each user's time zone): a timer started before closing
stops at closing time, so no time is logged after it, and the entry's comment says so. A timer
started after closing is overtime and keeps running, up to the maximum timer length. Blank for none.
- Running timers are saved every minute on the server (a background job), so issues show the time
live even with no browser open; open tabs keep saving too. Saves lock the timer, so several tabs
can't add the same time twice.
+6
View File
@@ -9,6 +9,12 @@
<em class="info"><%= t '.text_max_timer_hours' %></em>
</p>
<p>
<label for="settings_stop_time"><%= t '.label_stop_time' %></label>
<%= time_field_tag 'settings[stop_time]', Stopwatch.settings['stop_time'], id: 'settings_stop_time' %>
<em class="info"><%= t '.text_stop_time' %></em>
</p>
<p>
<label for="settings_highlighted_links"><%= t '.label_highlighted_links' %> </label>
<%= hidden_field_tag 'settings[highlighted_links]', '0', id: nil %>
+3
View File
@@ -21,6 +21,7 @@ en:
permission_view_running_timers: View running timers
permission_stop_running_timers: Stop other users' timers
text_stopwatch_timer_capped: "[Timer stopped automatically at the %{hours} hour limit - please check these hours]"
text_stopwatch_timer_closed: "[Timer stopped automatically at closing time (%{time})]"
stopwatch:
settings:
label_always_ask: 'Always ask'
@@ -28,6 +29,8 @@ en:
label_highlighted_links: "Color-code Start/Stop"
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."
label_system: 'Use system default'
stopwatch_timers:
entries_list:
+20
View File
@@ -51,6 +51,26 @@ module Stopwatch
hours.positive? ? hours : nil
end
# Closing time (stop time), as [hour, minute], or nil when none is set. Timers started before closing
# stop at closing time; timers started after closing (overtime) keep running.
def self.stop_time
value = settings['stop_time'].to_s.strip
return nil unless (m = value.match(/\A(\d{1,2}):(\d{2})\z/))
hour, minute = m[1].to_i, m[2].to_i
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
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)
end
# Colour-code the issue Start/Stop links (green to start, red to stop)
def self.highlighted_links
settings['highlighted_links'].to_s == '1'
+41 -4
View File
@@ -120,6 +120,25 @@ module Stopwatch
data[:tick_token]
end
# When this timer stops for closing time: closing time on the day it was started, if it was started
# before then. A timer started after closing (overtime) has none; the maximum length still applies.
def closes_at
start = data[:session_started_at] || data[:started_at]
return nil unless start
started = Time.at(start.to_i)
closing = Stopwatch.closing_time_on(started, user)
closing if closing && started < closing
end
# True when the running timer has reached closing time and should be stopped
def past_closing?
return false unless running?
closing = closes_at
closing.present? && Time.now >= closing
end
# True when the running timer has reached the maximum timer length and should be stopped
def over_limit?
max = Stopwatch.max_timer_hours
@@ -175,27 +194,35 @@ module Stopwatch
end
end
# Running time not yet saved into the entry, limited by the maximum timer length
# Running time not yet saved into the entry, limited by the maximum timer length and by closing time
def unsaved_hours
hours = runtime_hours || 0
max = Stopwatch.max_timer_hours
max ? [hours, [max - data[:session_hours].to_f, 0].max].min : hours
hours = [hours, [max - data[:session_hours].to_f, 0].max].min if max
closing = closes_at
if closing && data[:started_at]
hours = [hours, [(closing.to_i - data[:started_at].to_i).to_f / 1.hour, 0].max].min
end
hours
end
def apply_elapsed_time(stop:)
hours = unsaved_hours
capped = over_limit?
closed = !capped && past_closing?
entry = time_entry
if entry && (hours.positive? || capped)
if entry && (hours.positive? || capped || closed)
attributes = {}
attributes[:hours] = entry.read_attribute(:hours).to_f + hours if hours.positive?
attributes[:comments] = capped_comment(entry) if capped
attributes[:comments] = closing_comment(entry) if closed
entry.update_columns(attributes) if attributes.any?
end
if stop || capped
if stop || capped || closed
Rails.logger.info "[Stopwatch] Timer for user ##{user.id} stopped at the #{Stopwatch.max_timer_hours}h limit" if capped
Rails.logger.info "[Stopwatch] Timer for user ##{user.id} stopped at closing time" if closed
data[:started_at] = nil
data[:session_hours] = 0.0
data[:session_started_at] = nil
@@ -214,6 +241,16 @@ module Stopwatch
[comments.presence, note].compact.join(' ').truncate(1024)
end
# Notes on the entry that the timer stopped at closing time
def closing_comment(entry)
hour, minute = Stopwatch.stop_time
note = I18n.t(:text_stopwatch_timer_closed, time: format('%02d:%02d', hour, minute))
comments = entry.comments.to_s
return comments if comments.include?(note)
[comments.presence, note].compact.join(' ').truncate(1024)
end
def runtime_hours
if start = data[:started_at]
runtime = Time.now.to_i - start
+35
View File
@@ -200,4 +200,39 @@ class TimerTest < ActiveSupport::TestCase
@timer.stop_for(by: @user)
refute_includes @time_entry.reload.comments.to_s, 'Timer stopped by'
end
# 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)
end
test "timer started before closing stops at closing time" do
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '17:00')
hours = @time_entry.hours
travel_to(closing_today - 2.hours) { @timer.start @time_entry }
travel_to(closing_today + 3.hours) { @timer.update }
@time_entry.reload
assert_in_delta hours + 2, @time_entry.hours, 0.01
refute @timer.running?
assert_includes @time_entry.comments, '17:00'
ensure
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '')
end
test "timer started after closing keeps running" do
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '17:00')
hours = @time_entry.hours
travel_to(closing_today + 1.hour) { @timer.start @time_entry }
travel_to(closing_today + 3.hours) { @timer.update }
@time_entry.reload
assert_in_delta hours + 2, @time_entry.hours, 0.01
assert @timer.running?
assert_equal 'work', @time_entry.comments
ensure
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('stop_time' => '')
end
end