Module: TimeCalc::Units
- Defined in:
- lib/time_calc/units.rb
Overview
Unit-related constants and utilities for fetching their values
Constant Summary collapse
- ALL =
%i[year month week day hour min sec].freeze
- NATURAL =
%i[year month day hour min sec].freeze
- STRUCTURAL =
%i[year month day hour min sec subsec].freeze
- SYNONYMS =
{ second: :sec, seconds: :sec, minute: :min, minutes: :min, hours: :hour, days: :day, weeks: :week, months: :month, years: :year }.freeze
- DEFAULTS =
{ month: 1, day: 1, hour: 0, min: 0, sec: 0, subsec: 0 }.freeze
- MULTIPLIERS =
{ sec: 1, min: 60, hour: 60 * 60, day: 24 * 60 * 60 }.freeze
Class Method Summary collapse
Class Method Details
.call(unit) ⇒ Object
39 40 41 42 |
# File 'lib/time_calc/units.rb', line 39 def self.call(unit) SYNONYMS.fetch(unit, unit) .tap { |u| ALL.include?(u) or fail ArgumentError, "Unsupported unit: #{u}" } end |
.multiplier_for(klass, unit, precise: false) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/time_calc/units.rb', line 44 def self.multiplier_for(klass, unit, precise: false) res = MULTIPLIERS.fetch(unit) d = MULTIPLIERS.fetch(:day) case klass.name when 'Time' res when 'DateTime' res / d.to_f when 'Date' precise ? res / d.to_f : res / d end end |