Class: Temporal::Duration
- Inherits:
-
Object
- Object
- Temporal::Duration
- Defined in:
- lib/temporal/duration.rb
Constant Summary collapse
- UNIT_LIMITS =
{ year: 4_294_967_296, # 2**32 month: 4_294_967_296, # 2**32 week: 4_294_967_296, # 2**32 days: 104_249_991_375, # (2**53 / 86_400.0).ceil | 2**53 is based hours: 2_501_999_792_984, # (2**53 / 86_400.0).ceil | on EcmaScript minutes: 150_119_987_579_017, # (2**53 / 86_400.0).ceil | Number.MAX_SAFE_INTEGER seconds: 9_007_199_254_740_992, # 2**53 }.freeze
- UNITS =
i[years months days hours minutes seconds weeks milliseconds microseconds nanoseconds].freeze
Instance Method Summary collapse
-
#initialize(years = nil, months = nil, weeks = nil, days = nil, hours = nil, minutes = nil, seconds = nil, milliseconds = nil, microseconds = nil, nanoseconds = nil) ⇒ Duration
constructor
A new instance of Duration.
- #total(unit) ⇒ Object
Constructor Details
#initialize(years = nil, months = nil, weeks = nil, days = nil, hours = nil, minutes = nil, seconds = nil, milliseconds = nil, microseconds = nil, nanoseconds = nil) ⇒ Duration
Returns a new instance of Duration.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/temporal/duration.rb', line 21 def initialize(years = nil, months = nil, weeks = nil, days = nil, hours = nil, minutes = nil, seconds = nil, milliseconds = nil, microseconds = nil, nanoseconds = nil) values = [years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds] values.map! do |v| case v in 0 then nil in String then v.to_i in Numeric then v else nil end end values.uniq!.compact! unless values.all?(&:positive?) || values.all?(&:negative?) raise RangeError, "Can't mix positive and negative numbers" end self.years = years self.months = months self.weeks = weeks self.days = days self.hours = hours self.minutes = minutes self.seconds = seconds self.milliseconds = milliseconds self.microseconds = microseconds self.nanoseconds = nanoseconds overboard end |
Instance Method Details
#total(unit) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/temporal/duration.rb', line 56 def total(unit) if unit == :seconds (days * 86_400) + (hours * 3600) + (minutes * 60) + seconds + (((milliseconds * 1_000_000) + (microseconds * 1_000) + nanoseconds ) / 1_000_000_000.0) else 0 end end |