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

+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