Class: SqlPartitioner::TimeUnitConverter
- Inherits:
-
Object
- Object
- SqlPartitioner::TimeUnitConverter
- 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
-
.advance_date_time(date_time, calendar_unit, value) ⇒ DateTime
Result of advancing the given date_time by the given value.
-
.time_units_per_second(time_unit) ⇒ Fixnum
How many of the given time_unit are in 1 second.
Instance Method Summary collapse
-
#advance(time_units_timestamp, calendar_unit, value) ⇒ Fixnum
New timestamp in configured time units.
-
#from_days(num_days) ⇒ Fixnum
Number of days represented in the configure time units.
-
#from_seconds(timestamp_seconds) ⇒ Fixnum
converts from seconds to the configured time unit.
-
#initialize(time_unit) ⇒ TimeUnitConverter
constructor
A new instance of TimeUnitConverter.
-
#time_units_per_second ⇒ Fixnum
How many of the configured time_unit are in 1 second.
-
#to_date_time(time_units_timestamp) ⇒ DateTime
Representation of the given timestamp.
-
#to_seconds(time_units_timestamp) ⇒ Fixnum
converts from the configured time unit to seconds.
Constructor Details
#initialize(time_unit) ⇒ TimeUnitConverter
Returns a new instance of TimeUnitConverter.
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.
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.
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.
58 59 60 61 62 |
# File 'lib/sql_partitioner/time_unit_converter.rb', line 58 def advance(, calendar_unit, value) date_time = to_date_time() 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.
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
35 36 37 |
# File 'lib/sql_partitioner/time_unit_converter.rb', line 35 def from_seconds() * time_units_per_second end |
#time_units_per_second ⇒ Fixnum
Returns 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.
26 27 28 |
# File 'lib/sql_partitioner/time_unit_converter.rb', line 26 def to_date_time() DateTime.strptime("#{to_seconds()}", '%s') end |
#to_seconds(time_units_timestamp) ⇒ Fixnum
converts from the configured time unit to seconds
44 45 46 |
# File 'lib/sql_partitioner/time_unit_converter.rb', line 44 def to_seconds() / time_units_per_second end |