Skip to content

Commit

Permalink
Simplify the next/previous rotate calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
wishdev committed Jan 10, 2025
1 parent 3ac8b17 commit cc46a50
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions lib/logger/period.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
# frozen_string_literal: true

require 'date'

class Logger
module Period
module_function

SiD = 24 * 60 * 60

def next_rotate_time(now, shift_age)
case shift_age
when 'daily', :daily
t = Time.mktime(now.year, now.month, now.mday) + SiD
(now.to_date + 1).to_time
when 'weekly', :weekly
t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday)
(now.to_date + 7 - now.wday).to_time
when 'monthly', :monthly
t = Time.mktime(now.year, now.month, 1) + SiD * 32
return Time.mktime(t.year, t.month, 1)
if now.month == 12
Time.mktime(now.year + 1, 1, 1)
else
Time.mktime(now.year, now.month + 1, 1)
end
when 'now', 'everytime', :now, :everytime
return now
now
else
raise ArgumentError, "invalid :shift_age #{shift_age.inspect}, should be daily, weekly, monthly, or everytime"
end
if t.hour.nonzero? or t.min.nonzero? or t.sec.nonzero?
hour = t.hour
t = Time.mktime(t.year, t.month, t.mday)
t += SiD if hour > 12
end
t
end

def previous_period_end(now, shift_age)
case shift_age
when 'daily', :daily
t = Time.mktime(now.year, now.month, now.mday) - SiD / 2
now.to_date.to_time - 1
when 'weekly', :weekly
t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + SiD / 2)
(now.to_date - now.wday).to_time - 1
when 'monthly', :monthly
t = Time.mktime(now.year, now.month, 1) - SiD / 2
Time.mktime(now.year, now.month, 1) - 1
when 'now', 'everytime', :now, :everytime
return now
now
else
raise ArgumentError, "invalid :shift_age #{shift_age.inspect}, should be daily, weekly, monthly, or everytime"
end
Time.mktime(t.year, t.month, t.mday, 23, 59, 59)
end
end
end

0 comments on commit cc46a50

Please sign in to comment.