class Puppet::Pops::Time::Timespan

Public Class Methods

from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0) click to toggle source
   # File lib/puppet/pops/time/timespan.rb
81 def self.from_fields(negative, days, hours, minutes, seconds, milliseconds = 0, microseconds = 0, nanoseconds = 0)
82   ns = (((((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000 + milliseconds) * 1000 + microseconds) * 1000 + nanoseconds
83   new(negative ? -ns : ns)
84 end
from_fields_hash(hash) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
 94 def self.from_fields_hash(hash)
 95   from_fields(
 96     hash[KEY_NEGATIVE] || false,
 97     hash[KEY_DAYS] || 0,
 98     hash[KEY_HOURS] || 0,
 99     hash[KEY_MINUTES] || 0,
100     hash[KEY_SECONDS] || 0,
101     hash[KEY_MILLISECONDS] || 0,
102     hash[KEY_MICROSECONDS] || 0,
103     hash[KEY_NANOSECONDS] || 0)
104 end
from_hash(hash) click to toggle source
   # File lib/puppet/pops/time/timespan.rb
86 def self.from_hash(hash)
87   hash.include?('string') ? from_string_hash(hash) : from_fields_hash(hash)
88 end
from_string_hash(hash) click to toggle source
   # File lib/puppet/pops/time/timespan.rb
90 def self.from_string_hash(hash)
91   parse(hash[KEY_STRING], hash[KEY_FORMAT] || Format::DEFAULTS)
92 end
parse(str, format = Format::DEFAULTS) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
106 def self.parse(str, format = Format::DEFAULTS)
107   if format.is_a?(::Array)
108     format.each do |fmt|
109       fmt = FormatParser.singleton.parse_format(fmt) unless fmt.is_a?(Format)
110       begin
111         return fmt.parse(str)
112       rescue ArgumentError
113       end
114     end
115     raise ArgumentError, _("Unable to parse '%{str}' using any of the formats %{formats}") % { str: str, formats: format.join(', ') }
116   end
117   format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format)
118   format.parse(str)
119 end

Public Instance Methods

%(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
180 def %(o)
181   modulo(o)
182 end
*(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
156 def *(o)
157   case o
158   when Integer, Float
159     Timespan.new((@nsecs * o).to_i)
160   else
161     raise ArgumentError, _("A Timestamp cannot be multiplied by %{klass}") % { klass: a_an(o) }
162   end
163 end
+(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
126 def +(o)
127   case o
128   when Timestamp
129     Timestamp.new(@nsecs + o.nsecs)
130   when Timespan
131     Timespan.new(@nsecs + o.nsecs)
132   when Integer, Float
133     # Add seconds
134     Timespan.new(@nsecs + (o * NSECS_PER_SEC).to_i)
135   else
136     raise ArgumentError, _("%{klass} cannot be added to a Timespan") % { klass: a_an_uc(o) } unless o.is_a?(Timespan)
137   end
138 end
-(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
140 def -(o)
141   case o
142   when Timespan
143     Timespan.new(@nsecs - o.nsecs)
144   when Integer, Float
145     # Subtract seconds
146     Timespan.new(@nsecs - (o * NSECS_PER_SEC).to_i)
147   else
148     raise ArgumentError, _("%{klass} cannot be subtracted from a Timespan") % { klass: a_an_uc(o) }
149   end
150 end
-@() click to toggle source
    # File lib/puppet/pops/time/timespan.rb
152 def -@
153   Timespan.new(-@nsecs)
154 end
/(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
196 def /(o)
197   div(o)
198 end
days() click to toggle source

@return [Integer] a positive integer denoting the number of days

    # File lib/puppet/pops/time/timespan.rb
201 def days
202   total_days
203 end
div(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
184 def div(o)
185   case o
186   when Timespan
187     # Timespan/Timespan yields a Float
188     @nsecs.fdiv(o.nsecs)
189   when Integer, Float
190     Timespan.new(@nsecs.div(o))
191   else
192     raise ArgumentError, _("A Timespan cannot be divided by %{klass}") % { klass: a_an(o) }
193   end
194 end
divmod(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
165 def divmod(o)
166   case o
167   when Integer
168     to_i.divmod(o)
169   when Float
170     to_f.divmod(o)
171   else
172     raise ArgumentError, _("Can not do modulus on a Timespan using a %{klass}") % { klass: a_an(o) }
173   end
174 end
format(format) click to toggle source

Formats this timestamp into a string according to the given `format`

@param [String,Format] format The format to use when producing the string @return [String] the string representing the formatted timestamp @raise [ArgumentError] if the format is a string with illegal format characters @api public

    # File lib/puppet/pops/time/timespan.rb
236 def format(format)
237   format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format)
238   format.format(self)
239 end
hours() click to toggle source

@return [Integer] a positive integer, 0 - 23 denoting hours of day

    # File lib/puppet/pops/time/timespan.rb
206 def hours
207   total_hours % 24
208 end
milliseconds() click to toggle source

@return [Integer] a positive integer, 0 - 999 denoting milliseconds of second

    # File lib/puppet/pops/time/timespan.rb
221 def milliseconds
222   total_milliseconds % 1000
223 end
minutes() click to toggle source

@return [Integer] a positive integer, 0 - 59 denoting minutes of hour

    # File lib/puppet/pops/time/timespan.rb
211 def minutes
212   total_minutes % 60
213 end
modulo(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
176 def modulo(o)
177   divmod(o)[1]
178 end
nanoseconds() click to toggle source

@return [Integer] a positive integer, 0 - 999.999.999 denoting nanoseconds of second

    # File lib/puppet/pops/time/timespan.rb
226 def nanoseconds
227   total_nanoseconds % NSECS_PER_SEC
228 end
negative?() click to toggle source

@return [true] if the stored value is negative

    # File lib/puppet/pops/time/timespan.rb
122 def negative?
123   @nsecs < 0
124 end
seconds() click to toggle source

@return [Integer] a positive integer, 0 - 59 denoting seconds of minute

    # File lib/puppet/pops/time/timespan.rb
216 def seconds
217   total_seconds % 60
218 end
to_hash(compact = false) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
249 def to_hash(compact = false)
250   result = {}
251   n = nanoseconds
252   if compact
253     s = total_seconds
254     result[KEY_SECONDS] = negative? ? -s : s
255     result[KEY_NANOSECONDS] = negative? ? -n : n unless n == 0
256   else
257     add_unless_zero(result, KEY_DAYS, days)
258     add_unless_zero(result, KEY_HOURS, hours)
259     add_unless_zero(result, KEY_MINUTES, minutes)
260     add_unless_zero(result, KEY_SECONDS, seconds)
261     unless n == 0
262       add_unless_zero(result, KEY_NANOSECONDS, n % 1000)
263       n /= 1000
264       add_unless_zero(result, KEY_MICROSECONDS, n % 1000)
265       add_unless_zero(result, KEY_MILLISECONDS, n / 1000)
266     end
267     result[KEY_NEGATIVE] = true if negative?
268   end
269   result
270 end
to_s() click to toggle source

Formats this timestamp into a string according to {Format::DEFAULTS}

@return [String] the string representing the formatted timestamp @api public

    # File lib/puppet/pops/time/timespan.rb
245 def to_s
246   format(Format::DEFAULTS[0])
247 end
total_days() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
278 def total_days
279   total_nanoseconds / NSECS_PER_DAY
280 end
total_hours() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
283 def total_hours
284   total_nanoseconds / NSECS_PER_HOUR
285 end
total_microseconds() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
303 def total_microseconds
304   total_nanoseconds / NSECS_PER_USEC
305 end
total_milliseconds() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
298 def total_milliseconds
299   total_nanoseconds / NSECS_PER_MSEC
300 end
total_minutes() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
288 def total_minutes
289   total_nanoseconds / NSECS_PER_MIN
290 end
total_nanoseconds() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
308 def total_nanoseconds
309   @nsecs.abs
310 end
total_seconds() click to toggle source

@api private

    # File lib/puppet/pops/time/timespan.rb
293 def total_seconds
294   total_nanoseconds / NSECS_PER_SEC
295 end

Private Instance Methods

add_unless_zero(result, key, value) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
272 def add_unless_zero(result, key, value)
273   result[key] = value unless value == 0
274 end