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

Public Instance Methods

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

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

    # File lib/puppet/pops/time/timespan.rb
200 def days
201   total_days
202 end
div(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
183 def div(o)
184   case o
185   when Timespan
186     # Timespan/Timespan yields a Float
187     @nsecs.fdiv(o.nsecs)
188   when Integer, Float
189     Timespan.new(@nsecs.div(o))
190   else
191     raise ArgumentError, _("A Timespan cannot be divided by %{klass}") % { klass: a_an(o) }
192   end
193 end
divmod(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
164 def divmod(o)
165   case o
166   when Integer
167     to_i.divmod(o)
168   when Float
169     to_f.divmod(o)
170   else
171     raise ArgumentError, _("Can not do modulus on a Timespan using a %{klass}") % { klass: a_an(o) }
172   end
173 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
235 def format(format)
236   format = FormatParser.singleton.parse_format(format) unless format.is_a?(Format)
237   format.format(self)
238 end
hours() click to toggle source

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

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

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

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

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

    # File lib/puppet/pops/time/timespan.rb
210 def minutes
211   total_minutes % 60
212 end
modulo(o) click to toggle source
    # File lib/puppet/pops/time/timespan.rb
175 def modulo(o)
176   divmod(o)[1]
177 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
225 def nanoseconds
226   total_nanoseconds % NSECS_PER_SEC
227 end
negative?() click to toggle source

@return [true] if the stored value is negative

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

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

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

@api private

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

@api private

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

@api private

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

@api private

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

@api private

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

@api private

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

@api private

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

Private Instance Methods

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