Module: AWSCronParser::Helpers::Clock
- Defined in:
- lib/aws_cron_parser/helpers/clock.rb
Overview
Deliberately not named Time: every class that includes Helpers would then
resolve a bare Time to this module instead of ::Time through the
ancestor chain, silently breaking default arguments and Time.zone calls.
Instance Method Summary collapse
-
#adjust_seconds(time, target_second) ⇒ Object
Adjust time to the target second, rolling over minute if needed.
-
#ensure_time(now, time_source = nil) ⇒ Object
Ensure the given 'now' is a Time object.
-
#next_month_start(search_date, time_source = nil) ⇒ Object
Get the start of the next month from a given date.
Instance Method Details
#adjust_seconds(time, target_second) ⇒ Object
Adjust time to the target second, rolling over minute if needed
39 40 41 42 43 44 45 46 47 |
# File 'lib/aws_cron_parser/helpers/clock.rb', line 39 def adjust_seconds(time, target_second) if time.sec == target_second time elsif time.sec < target_second time.change(sec: target_second) else (time + 60.seconds).change(sec: target_second) end end |
#ensure_time(now, time_source = nil) ⇒ Object
Ensure the given 'now' is a Time object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/aws_cron_parser/helpers/clock.rb', line 10 def ensure_time(now, time_source = nil) time_source ||= @time_source || ::Time return now if now.is_a?(::Time) # If it's a Date or DateTime, convert preserving timezone info if now.respond_to?(:hour) && now.respond_to?(:min) # It's a DateTime time = time_source.local(now.year, now.month, now.day, now.hour, now.min, 0) now.respond_to?(:utc?) && now.utc? ? time.utc : time else # It's a Date time_source.local(now.year, now.month, now.day, 0, 0, 0) end end |
#next_month_start(search_date, time_source = nil) ⇒ Object
Get the start of the next month from a given date
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/aws_cron_parser/helpers/clock.rb', line 26 def next_month_start(search_date, time_source = nil) time_source ||= @time_source || ::Time next_month = Date.new(search_date.year, search_date.month, 1).next_month # Handle both Time.zone (Rails) and ::Time (Ruby) if time_source.respond_to?(:local) time_source.local(next_month.year, next_month.month, 1, 0, 0, 0) else ::Time.zone.local(next_month.year, next_month.month, 1, 0, 0, 0) end end |