Class: Chronos::Duration
- Inherits:
-
Object
- Object
- Chronos::Duration
- Defined in:
- lib/chronos/duration.rb,
lib/chronos/duration/gregorian.rb
Overview
An immutable class representing the amount of intervening time in a time interval. A duration has no start- nor end-point. Also see Interval
Direct Known Subclasses
Defined Under Namespace
Classes: Gregorian
Constant Summary collapse
- FormatToS =
"%dd %dps (%s)".freeze
- FormatInspect =
"#<%s:0x%08x %dd %dps (%s)>".freeze
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#picoseconds ⇒ Object
readonly
Returns the value of attribute picoseconds.
Class Method Summary collapse
- .days(n, language = nil) ⇒ Object
- .import(duration) ⇒ Object
- .seconds(n, language = nil) ⇒ Object
- .with(parts) ⇒ Object
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #div(other) ⇒ Object
- #durations_at(*keys) ⇒ Object
-
#initialize(days, picoseconds, language = nil) ⇒ Duration
constructor
Create a Duration of given picoseconds length.
-
#inspect ⇒ Object
:nodoc:.
- #quo(other) ⇒ Object
-
#split ⇒ Object
Split the duration into durations with each only one of the atomic units set.
-
#to_a(exclude_language = nil) ⇒ Object
An array with the atomic units and the language of this Duration.
- #to_duration ⇒ Object
- #to_hash ⇒ Object
-
#to_s ⇒ Object
return a readable representation.
- #values_at(*keys) ⇒ Object
Constructor Details
#initialize(days, picoseconds, language = nil) ⇒ Duration
Create a Duration of given picoseconds length
47 48 49 50 51 |
# File 'lib/chronos/duration.rb', line 47 def initialize(days, picoseconds, language=nil) @days, @picoseconds = *picoseconds.divmod(PS_IN_DAY) @days += days @language = Chronos.language(language) end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
34 35 36 |
# File 'lib/chronos/duration.rb', line 34 def days @days end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
36 37 38 |
# File 'lib/chronos/duration.rb', line 36 def language @language end |
#picoseconds ⇒ Object (readonly)
Returns the value of attribute picoseconds.
35 36 37 |
# File 'lib/chronos/duration.rb', line 35 def picoseconds @picoseconds end |
Class Method Details
.days(n, language = nil) ⇒ Object
38 39 40 |
# File 'lib/chronos/duration.rb', line 38 def self.days(n, language=nil) new(n, 0, language) end |
.import(duration) ⇒ Object
30 31 32 |
# File 'lib/chronos/duration.rb', line 30 def self.import(duration) duration.to_duration end |
.seconds(n, language = nil) ⇒ Object
42 43 44 |
# File 'lib/chronos/duration.rb', line 42 def self.seconds(n, language=nil) new(0, n*PS_IN_SECOND, language) end |
.with(parts) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/chronos/duration.rb', line 22 def self.with(parts) new( parts[:days] || parts[:d] || 0, parts[:picoseconds] || parts[:ps] || 0, parts[:language] ) end |
Instance Method Details
#%(other) ⇒ Object
85 86 87 88 89 |
# File 'lib/chronos/duration.rb', line 85 def %(other) raise "Not yet implemented" # Duration % Duration -> modulo per unit, e.g. duration % 1.hour -> ps % (1*PS_IN_HOUR) # Duration % Symbol -> shortcut, e.g. duration % :hour -> duration % 1.hour end |
#*(other) ⇒ Object
69 70 71 |
# File 'lib/chronos/duration.rb', line 69 def *(other) self.class.new(*(self.to_a(true).map { |e| e*other }+[@language])) end |
#+(other) ⇒ Object
61 62 63 |
# File 'lib/chronos/duration.rb', line 61 def +(other) self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a+b }+[@language])) end |
#+@ ⇒ Object
53 54 55 |
# File 'lib/chronos/duration.rb', line 53 def +@ self end |
#-(other) ⇒ Object
65 66 67 |
# File 'lib/chronos/duration.rb', line 65 def -(other) self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a-b }+[@language])) end |
#-@ ⇒ Object
57 58 59 |
# File 'lib/chronos/duration.rb', line 57 def -@ self.class.new(*(self.to_a(true).map { |e| -e }+[@language])) end |
#/(other) ⇒ Object
73 74 75 |
# File 'lib/chronos/duration.rb', line 73 def /(other) self.class.new(*(self.to_a(true).map { |e| e/other }+[@language])) end |
#div(other) ⇒ Object
77 78 79 |
# File 'lib/chronos/duration.rb', line 77 def div(other) self.class.new(*(self.to_a(true).map { |e| e.div(other) }+[@language])) end |
#durations_at(*keys) ⇒ Object
126 127 128 129 130 |
# File 'lib/chronos/duration.rb', line 126 def durations_at(*keys) keys.zip(values_at(*keys)).map { |key, value| self.class.with(key => value, :language => @language) } end |
#inspect ⇒ Object
:nodoc:
137 138 139 |
# File 'lib/chronos/duration.rb', line 137 def inspect # :nodoc: sprintf(self.class::FormatInspect, self.class, object_id<<1, *self) end |
#quo(other) ⇒ Object
81 82 83 |
# File 'lib/chronos/duration.rb', line 81 def quo(other) self.class.new(*(self.to_a(true).map { |e| e.quo(other) }+[@language])) end |
#split ⇒ Object
Split the duration into durations with each only one of the atomic units set
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/chronos/duration.rb', line 92 def split lang = [@language] klass = self.class ary = to_a(true) (0...(ary.size)).zip(ary).map { |i,e| init = Array.new(ary.size, 0)+lang init[i] = e klass.new(*init) } end |
#to_a(exclude_language = nil) ⇒ Object
An array with the atomic units and the language of this Duration
104 105 106 |
# File 'lib/chronos/duration.rb', line 104 def to_a(exclude_language=nil) exclude_language ? [@days, @picoseconds] : [@days, @picoseconds, @language] end |
#to_duration ⇒ Object
118 119 120 |
# File 'lib/chronos/duration.rb', line 118 def to_duration self end |
#to_hash ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/chronos/duration.rb', line 108 def to_hash { :days => @days, :d => @days, :ps => @picoseconds, :picoseconds => @picoseconds, :language => @language, } end |
#to_s ⇒ Object
return a readable representation
133 134 135 |
# File 'lib/chronos/duration.rb', line 133 def to_s sprintf(self.class::FormatToS, *self) end |
#values_at(*keys) ⇒ Object
122 123 124 |
# File 'lib/chronos/duration.rb', line 122 def values_at(*keys) to_hash.values_at(*keys) end |