Class: SqlPartitioner::TimeUnitConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_partitioner/time_unit_converter.rb

Constant Summary collapse

MINUTE_AS_SECONDS =
60
HOUR_AS_SECONDS =
60 * MINUTE_AS_SECONDS
DAY_AS_SECONDS =
24 * HOUR_AS_SECONDS
SUPPORTED_TIME_UNITS =
[:seconds, :micro_seconds]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_unit) ⇒ TimeUnitConverter

Returns a new instance of TimeUnitConverter.

Parameters:

  • time_unit (Symbol)

    one of SUPPORTED_TIME_UNITS

Raises:

  • (ArgumentError)


12
13
14
15
16
# 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

Class Method Details

.advance_date_time(date_time, calendar_unit, value) ⇒ DateTime

Returns result of advancing the given date_time by the given value.

Parameters:

  • date_time (DateTime)

    to advance

  • calendar_unit (Symbol)

    unit for the following ‘value`, one of [:day(s), :month(s)]

  • value (Fixnum)

    in terms of calendar_unit to add to the date_time

Returns:

  • (DateTime)

    result of advancing the given date_time by the given value



68
69
70
71
72
73
74
75
76
77
# 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

.time_units_per_second(time_unit) ⇒ Fixnum

Returns how many of the given time_unit are in 1 second.

Parameters:

  • time_unit (Symbol)

    one of ‘SUPPORTED_TIME_UNITS`

Returns:

  • (Fixnum)

    how many of the given time_unit are in 1 second



81
82
83
84
85
86
87
88
89
90
# 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

Instance Method Details

#advance(time_units_timestamp, calendar_unit, value) ⇒ Fixnum

Returns new timestamp in configured time units.

Parameters:

  • time_units_timestamp (Fixnum)
  • calendar_unit (Symbol)

    unit for the given value, one of [:day(s), :month(s)]

  • value (Fixnum)

    in terms of calendar_unit to add to the time_units_timestamp

Returns:

  • (Fixnum)

    new timestamp in configured time units



58
59
60
61
62
# 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

#from_days(num_days) ⇒ Fixnum

Returns number of days represented in the configure time units.

Parameters:

  • num_days (Fixnum)

Returns:

  • (Fixnum)

    number of days represented in the configure time units



20
21
22
# File 'lib/sql_partitioner/time_unit_converter.rb', line 20

def from_days(num_days)
  from_seconds(num_days * DAY_AS_SECONDS)
end

#from_seconds(timestamp_seconds) ⇒ Fixnum

converts from seconds to the configured time unit

Parameters:

  • timestamp_seconds (Fixnum)

    timestamp in seconds

Returns:

  • (Fixnum)

    timestamp in configured time units



35
36
37
# File 'lib/sql_partitioner/time_unit_converter.rb', line 35

def from_seconds(timestamp_seconds)
  timestamp_seconds * time_units_per_second
end

#time_units_per_secondFixnum

Returns how many of the configured time_unit are in 1 second.

Returns:

  • (Fixnum)

    how many of the configured time_unit are in 1 second



49
50
51
# File 'lib/sql_partitioner/time_unit_converter.rb', line 49

def time_units_per_second
  self.class.time_units_per_second(@time_unit)
end

#to_date_time(time_units_timestamp) ⇒ DateTime

Returns representation of the given timestamp.

Parameters:

  • time_units_timestamp (Fixnum)

    timestamp in configured time units

Returns:

  • (DateTime)

    representation of the given timestamp



26
27
28
# 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

#to_seconds(time_units_timestamp) ⇒ Fixnum

converts from the configured time unit to seconds

Parameters:

  • time_units_timestamp (Fixnum)

    timestamp in the configured time units

Returns:

  • (Fixnum)

    timestamp in seconds



44
45
46
# File 'lib/sql_partitioner/time_unit_converter.rb', line 44

def to_seconds(time_units_timestamp)
  time_units_timestamp / time_units_per_second
end