Class: Puppet::Pops::Time::TimeData
- Includes:
- LabelProvider
- Defined in:
- lib/puppet/pops/time/timespan.rb
Overview
TimeData is a Numeric that stores its value internally as nano-seconds but will be considered to be seconds and fractions of seconds when used in arithmetic or comparison with other Numeric types.
Constant Summary
Constants included from LabelProvider
LabelProvider::A, LabelProvider::AN, LabelProvider::SKIPPED_CHARACTERS, LabelProvider::VOWELS
Instance Attribute Summary collapse
-
#nsecs ⇒ Object
readonly
Returns the value of attribute nsecs.
Instance Method Summary collapse
- #<=>(o) ⇒ Object
-
#initialize(nanoseconds) ⇒ TimeData
constructor
A new instance of TimeData.
- #label(o) ⇒ Object
-
#to_c ⇒ Complex
Short for ‘#to_f.to_c`.
-
#to_f ⇒ Float
The number of seconds.
- #to_i ⇒ Object
-
#to_int ⇒ Integer
The number of seconds with fraction part truncated.
-
#to_r ⇒ Rational
Initial numerator is nano-seconds and denominator is nano-seconds per second.
Methods included from LabelProvider
#a_an, #a_an_uc, #article, #combine_strings, #plural_s, #the, #the_uc
Constructor Details
#initialize(nanoseconds) ⇒ TimeData
Returns a new instance of TimeData.
33 34 35 |
# File 'lib/puppet/pops/time/timespan.rb', line 33 def initialize(nanoseconds) @nsecs = nanoseconds end |
Instance Attribute Details
#nsecs ⇒ Object (readonly)
Returns the value of attribute nsecs.
31 32 33 |
# File 'lib/puppet/pops/time/timespan.rb', line 31 def nsecs @nsecs end |
Instance Method Details
#<=>(o) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/puppet/pops/time/timespan.rb', line 37 def <=>(o) case o when self.class @nsecs <=> o.nsecs when Integer to_int <=> o when Float to_f <=> o else nil end end |
#label(o) ⇒ Object
50 51 52 |
# File 'lib/puppet/pops/time/timespan.rb', line 50 def label(o) Utils.name_to_segments(o.class.name).last end |
#to_c ⇒ Complex
Returns short for ‘#to_f.to_c`.
69 70 71 |
# File 'lib/puppet/pops/time/timespan.rb', line 69 def to_c to_f.to_c end |
#to_f ⇒ Float
Returns the number of seconds.
55 56 57 |
# File 'lib/puppet/pops/time/timespan.rb', line 55 def to_f @nsecs.fdiv(NSECS_PER_SEC) end |
#to_i ⇒ Object
64 65 66 |
# File 'lib/puppet/pops/time/timespan.rb', line 64 def to_i to_int end |
#to_int ⇒ Integer
Returns the number of seconds with fraction part truncated.
60 61 62 |
# File 'lib/puppet/pops/time/timespan.rb', line 60 def to_int @nsecs / NSECS_PER_SEC end |
#to_r ⇒ Rational
Returns initial numerator is nano-seconds and denominator is nano-seconds per second.
74 75 76 |
# File 'lib/puppet/pops/time/timespan.rb', line 74 def to_r Rational(@nsecs, NSECS_PER_SEC) end |