lib/groupdate/series_builder.rb in groupdate-5.0.0 vs lib/groupdate/series_builder.rb in groupdate-5.1.0
- old
+ new
@@ -1,16 +1,17 @@
module Groupdate
class SeriesBuilder
- attr_reader :period, :time_zone, :day_start, :week_start, :options
+ attr_reader :period, :time_zone, :day_start, :week_start, :n_seconds, :options
CHECK_PERIODS = [:day, :week, :month, :quarter, :year]
- def initialize(period:, time_zone:, day_start:, week_start:, **options)
+ def initialize(period:, time_zone:, day_start:, week_start:, n_seconds:, **options)
@period = period
@time_zone = time_zone
@week_start = week_start
@day_start = day_start
+ @n_seconds = n_seconds
@options = options
@round_time = {}
@week_start_key = Groupdate::Magic::DAYS[@week_start] if @week_start
end
@@ -71,10 +72,14 @@
result
end
def round_time(time)
+ if period == :custom
+ return time_zone.at((time.to_time.to_i / n_seconds) * n_seconds)
+ end
+
time = time.to_time.in_time_zone(time_zone)
if day_start != 0
# apply day_start to a time object that's not affected by DST
time = change_zone.call(time, utc)
@@ -143,10 +148,12 @@
last += 1.day unless time_range.exclude_end?
time_range = Range.new(time_range.first.in_time_zone(time_zone), last, true)
elsif !time_range && options[:last]
if period == :quarter
step = 3.months
+ elsif period == :custom
+ step = n_seconds
elsif 1.respond_to?(period)
step = 1.send(period)
else
raise ArgumentError, "Cannot use last option with #{period}"
end
@@ -214,17 +221,20 @@
if time_range.begin
series = [round_time(time_range.begin)]
if period == :quarter
step = 3.months
+ elsif period == :custom
+ step = n_seconds
else
step = 1.send(period)
end
last_step = series.last
+ day_start_hour = day_start / 3600
loop do
next_step = last_step + step
- next_step = round_time(next_step) if next_step.hour != day_start # add condition to speed up
+ next_step = round_time(next_step) if next_step.hour != day_start_hour # add condition to speed up
break unless time_range.cover?(next_step)
if next_step == last_step
last_step += step
next