Puppet Function: to_milliseconds

Defined in:
puppet/modules/wmflib/lib/puppet/parser/functions/to_milliseconds.rb
Function type:
Ruby 3.x API

Overview

to_milliseconds()Any

Returns:

  • (Any)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/to_milliseconds.rb', line 13

newfunction(:to_milliseconds, :type => :rvalue, :arity => 1) do |args|
  time_spec = args.first
  /^([0-9.+e]+)\s*(.*).?$/ =~ time_spec.downcase
  count, unit = $1, $2
  factor = case unit
           when /^n/         then 1.0e-6      # nanoseconds
           when /^u/         then 1.0e-3      # microseconds
           when /^(ms|mil)/  then 1.0         # milliseconds
           when /^s/         then 1.0e3       # seconds
           when /^(m|min)/   then 6.0e4       # minutes
           when /^h/         then 3.6e6       # hours
           when /^d/         then 8.64e7      # days
           when /^w/         then 6.048e8     # weeks
           when /^mo/        then 2.62974e9   # months
           when /^y/         then 3.15569e10  # years
           else fail(ArgumentError, "to_milliseconds(): Invalid time spec #{time_spec.inspect}")
  end
  ms = factor * Float(count)
  ms.to_i == ms ? ms.to_i : ms
end