Class: Puppet::Pops::Time::TimeData

Inherits:
Numeric
  • Object
show all
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.

Direct Known Subclasses

Timespan, Timestamp

Constant Summary

Constants included from LabelProvider

LabelProvider::A, LabelProvider::AN, LabelProvider::SKIPPED_CHARACTERS, LabelProvider::VOWELS

Instance Attribute Summary collapse

Instance Method Summary collapse

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.


31
32
33
# File 'lib/puppet/pops/time/timespan.rb', line 31

def initialize(nanoseconds)
  @nsecs = nanoseconds
end

Instance Attribute Details

#nsecsObject (readonly)

Returns the value of attribute nsecs


29
30
31
# File 'lib/puppet/pops/time/timespan.rb', line 29

def nsecs
  @nsecs
end

Instance Method Details

#<=>(o) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet/pops/time/timespan.rb', line 35

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


48
49
50
# File 'lib/puppet/pops/time/timespan.rb', line 48

def label(o)
  Utils.name_to_segments(o.class.name).last
end

#to_cComplex

Returns short for `#to_f.to_c`.

Returns:

  • (Complex)

    short for `#to_f.to_c`


67
68
69
# File 'lib/puppet/pops/time/timespan.rb', line 67

def to_c
  to_f.to_c
end

#to_fFloat

Returns the number of seconds.

Returns:

  • (Float)

    the number of seconds


53
54
55
# File 'lib/puppet/pops/time/timespan.rb', line 53

def to_f
  @nsecs.fdiv(NSECS_PER_SEC)
end

#to_iObject


62
63
64
# File 'lib/puppet/pops/time/timespan.rb', line 62

def to_i
  to_int
end

#to_intInteger

Returns the number of seconds with fraction part truncated.

Returns:

  • (Integer)

    the number of seconds with fraction part truncated


58
59
60
# File 'lib/puppet/pops/time/timespan.rb', line 58

def to_int
  @nsecs / NSECS_PER_SEC
end

#to_rRational

Returns initial numerator is nano-seconds and denominator is nano-seconds per second.

Returns:

  • (Rational)

    initial numerator is nano-seconds and denominator is nano-seconds per second


72
73
74
# File 'lib/puppet/pops/time/timespan.rb', line 72

def to_r
  Rational(@nsecs, NSECS_PER_SEC)
end