class SqlPartitioner::TimeUnitConverter
Constants
- DAY_AS_SECONDS
- HOUR_AS_SECONDS
- MINUTE_AS_SECONDS
- SUPPORTED_TIME_UNITS
Public Class Methods
@param [DateTime] date_time to advance @param [Symbol] calendar_unit unit for the following ‘value`, one of [:day(s), :month(s)] @param [Fixnum] value in terms of calendar_unit to add to the date_time @return [DateTime] result of advancing the given date_time by the given value
# File lib/sql_partitioner/time_unit_converter.rb, line 68 def self.advance_date_time(date_time, calendar_unit, value) new_time = case calendar_unit when :days, :day date_time + value when :months, :month date_time >> value end new_time end
@param [Symbol] time_unit one of SUPPORTED_TIME_UNITS
# File lib/sql_partitioner/time_unit_converter.rb, line 12 def initialize(time_unit) raise ArgumentError.new("Invalid time unit #{time_unit} passed") if !SUPPORTED_TIME_UNITS.include?(time_unit) @time_unit = time_unit end
@param [Symbol] time_unit one of ‘SUPPORTED_TIME_UNITS` @return [Fixnum] how many of the given time_unit are in 1 second
# File lib/sql_partitioner/time_unit_converter.rb, line 81 def self.time_units_per_second(time_unit) case time_unit when :micro_seconds 1_000_000 when :seconds 1 else raise "unknown time_unit #{time_unit.inspect}" end end
Public Instance Methods
@param [Fixnum] time_units_timestamp @param [Symbol] calendar_unit unit for the given value, one of [:day(s), :month(s)] @param [Fixnum] value in terms of calendar_unit to add to the time_units_timestamp
@return [Fixnum] new timestamp in configured time units
# File lib/sql_partitioner/time_unit_converter.rb, line 58 def advance(time_units_timestamp, calendar_unit, value) date_time = to_date_time(time_units_timestamp) date_time = self.class.advance_date_time(date_time, calendar_unit, value) from_seconds(date_time.to_time.to_i) end
@param [Fixnum] num_days @return [Fixnum] number of days represented in the configure time units
# File lib/sql_partitioner/time_unit_converter.rb, line 20 def from_days(num_days) from_seconds(num_days * DAY_AS_SECONDS) end
converts from seconds to the configured time unit
@param [Fixnum] timestamp_seconds timestamp in seconds
@return [Fixnum] timestamp in configured time units
# File lib/sql_partitioner/time_unit_converter.rb, line 35 def from_seconds(timestamp_seconds) timestamp_seconds * time_units_per_second end
@return [Fixnum] how many of the configured time_unit are in 1 second
# File lib/sql_partitioner/time_unit_converter.rb, line 49 def time_units_per_second self.class.time_units_per_second(@time_unit) end
@param [Fixnum] time_units_timestamp timestamp in configured time units @return [DateTime] representation of the given timestamp
# File lib/sql_partitioner/time_unit_converter.rb, line 26 def to_date_time(time_units_timestamp) DateTime.strptime("#{to_seconds(time_units_timestamp)}", '%s') end
converts from the configured time unit to seconds
@param [Fixnum] time_units_timestamp timestamp in the configured time units
@return [Fixnum] timestamp in seconds
# File lib/sql_partitioner/time_unit_converter.rb, line 44 def to_seconds(time_units_timestamp) time_units_timestamp / time_units_per_second end