Skip to content

Object Time Scale #19

@codisart

Description

@codisart
# frozen_string_literal: true

# @attr [DateTime] start
# @attr [Duration] step
# @attr [Enumerator] range
class TimeScale
  attr_reader :start, :step

  # @param [DateTime] start_time
  # @param [DateTime] end_time
  # @param [Duration] step
  def initialize(start_time, end_time, step = 1.second)
    @start = start_time
    @step = step
    @range = (start_time.to_i..end_time.to_i).step(@step)
  end

  # @param [TimeScale]
  def self.new_for_moment(moment)
    new(moment, moment)
  end

  def each
    @range.each do |timestamp|
      yield Time.at(timestamp).utc.to_datetime
    end
  end

  def map
    @range.map do |timestamp|
      yield Time.at(timestamp).utc.to_datetime
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions