Class: RdfContext::Duration
- Inherits:
-
Object
- Object
- RdfContext::Duration
- Defined in:
- lib/rdf_context/duration.rb
Instance Attribute Summary collapse
-
#da ⇒ Object
Returns the value of attribute da.
-
#hr ⇒ Object
Returns the value of attribute hr.
-
#mi ⇒ Object
Returns the value of attribute mi.
-
#mo ⇒ Object
Returns the value of attribute mo.
-
#ne ⇒ Object
Returns the value of attribute ne.
-
#se ⇒ Object
Returns the value of attribute se.
-
#yr ⇒ Object
Returns the value of attribute yr.
Class Method Summary collapse
-
.parse(value) ⇒ Duration
Reverse convert from XSD version of duration XSD allows -P1111Y22M33DT44H55M66.666S with any combination in regular order We assume 1M == 30D, but are out of spec in this regard We only output up to hours.
Instance Method Summary collapse
- #eql?(something) ⇒ Boolean (also: #==)
-
#initialize(value) ⇒ Duration
constructor
-
Given an integer, assumes that it is milliseconds * Given a time, extract second * Given a Flaat, use value direcly * Given a String, parse as xsd:duration.
-
-
#normalize ⇒ Object
protected
Normalize representation by adding everything up and then breaking it back down again.
- #to_f ⇒ Float
- #to_i ⇒ Integer
- #to_s(format = nil) ⇒ String
Constructor Details
#initialize(value) ⇒ Duration
-
Given an integer, assumes that it is milliseconds
-
Given a time, extract second
-
Given a Flaat, use value direcly
-
Given a String, parse as xsd:duration
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rdf_context/duration.rb', line 12 def initialize(value) case value when Hash @ne = value[:ne] || 1 @yr = value[:yr] || value[:years] || 0 @mo = value[:mo] || value[:months] || 0 @da = value[:da] || value[:days] || 0 @hr = value[:hr] || value[:hours] || 0 @mi = value[:mi] || value[:minutes] || 0 @se = value[:se] || value[:seconds] || 0 when Duration @se = value.to_f when Numeric @se = value else @se = value.to_i end self.normalize end |
Instance Attribute Details
#da ⇒ Object
Returns the value of attribute da.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def da @da end |
#hr ⇒ Object
Returns the value of attribute hr.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def hr @hr end |
#mi ⇒ Object
Returns the value of attribute mi.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def mi @mi end |
#mo ⇒ Object
Returns the value of attribute mo.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def mo @mo end |
#ne ⇒ Object
Returns the value of attribute ne.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def ne @ne end |
#se ⇒ Object
Returns the value of attribute se.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def se @se end |
#yr ⇒ Object
Returns the value of attribute yr.
4 5 6 |
# File 'lib/rdf_context/duration.rb', line 4 def yr @yr end |
Class Method Details
.parse(value) ⇒ Duration
Reverse convert from XSD version of duration XSD allows -P1111Y22M33DT44H55M66.666S with any combination in regular order We assume 1M == 30D, but are out of spec in this regard We only output up to hours
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rdf_context/duration.rb', line 41 def self.parse(value) if value.to_s.match(/^(-?)P(\d+Y)?(\d+M)?(\d+D)?T?(\d+H)?(\d+M)?([\d\.]+S)?$/) hash = {} hash[:ne] = $1 == "-" ? -1 : 1 hash[:yr] = $2.to_i hash[:mo] = $3.to_i hash[:da] = $4.to_i hash[:hr] = $5.to_i hash[:mi] = $6.to_i hash[:se] = $7.to_f value = hash end self.new(value) end |
Instance Method Details
#eql?(something) ⇒ Boolean Also known as: ==
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rdf_context/duration.rb', line 67 def eql?(something) case something when Duration self.to_f == something.to_f when String self.to_s(:xml) == something when Numeric self.to_f == something else false end end |
#normalize ⇒ Object (protected)
Normalize representation by adding everything up and then breaking it back down again
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rdf_context/duration.rb', line 114 def normalize s = self.to_f @ne = s < 0 ? -1 : 1 s = s * @ne _mi, @se = s.divmod(60) _hr, @mi = _mi.to_i.divmod(60) _da, @hr = _hr.divmod(24) _mo, @da = _da.divmod(30) @yr, @mo = _mo.divmod(12) end |
#to_f ⇒ Float
58 59 60 |
# File 'lib/rdf_context/duration.rb', line 58 def to_f (((((@yr.to_i * 12 + @mo.to_i) * 30 + @da.to_i) * 24 + @hr.to_i) * 60 + @mi.to_i) * 60 + @se.to_f) * (@ne || 1) end |
#to_i ⇒ Integer
63 |
# File 'lib/rdf_context/duration.rb', line 63 def to_i; Integer(self.to_f); end |
#to_s(format = nil) ⇒ String
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rdf_context/duration.rb', line 83 def to_s(format = nil) usec = (@se * 1000).to_i % 1000 sec_str = usec > 0 ? "%2.3f" % @se : @se.to_i.to_s if format == :xml str = @ne < 0 ? "-P" : "P" str << "%dY" % @yr if @yr > 0 str << "%dM" % @mo if @mo > 0 str << "%dD" % @da if @da > 0 str << "T" if @hr + @mi + @se > 0 str << "%dH" % @hr if @hr > 0 str << "%dM" % @mi if @mi > 0 str << "#{sec_str}S" if @se > 0 else ar = [] ar << "%d years" % @yr if @yr > 0 ar << "%d months" % @mo if @mo > 0 ar << "%d days" % @da if @da > 0 ar << "%d hours" % @hr if @hr > 0 ar << "%d minutes" % @mi if @mi > 0 ar << "%s seconds" % sec_str if @se > 0 last = ar.pop first = ar.join(", ") res = first.empty? ? last : "#{first} and #{last}" ne < 0 ? "#{res} ago" : res end end |