Mercurial > repos > other > Puppet
comparison modules/stdlib/lib/puppet/parser/functions/time.rb @ 272:c42fb28cff86
Update to a newer Python module
This also pulls in an EPEL module (which we don't use) and a newer
stdlib version.
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 03 Jan 2020 19:56:04 +0000 |
parents | 956e484adc12 |
children | d9352a684e62 |
comparison
equal
deleted
inserted
replaced
271:c62728474654 | 272:c42fb28cff86 |
---|---|
1 # | 1 # |
2 # time.rb | 2 # time.rb |
3 # | 3 # |
4 module Puppet::Parser::Functions | |
5 newfunction(:time, :type => :rvalue, :doc => <<-DOC | |
6 This function will return the current time since epoch as an integer. | |
4 | 7 |
5 module Puppet::Parser::Functions | 8 *Examples:* |
6 newfunction(:time, :type => :rvalue, :doc => <<-EOS | |
7 This function will return the current time since epoch as an integer. | |
8 | 9 |
9 *Examples:* | 10 time() |
10 | 11 |
11 time() | 12 Will return something like: 1311972653 |
12 | 13 DOC |
13 Will return something like: 1311972653 | 14 ) do |arguments| |
14 EOS | |
15 ) do |arguments| | |
16 | 15 |
17 # The Time Zone argument is optional ... | 16 # The Time Zone argument is optional ... |
18 time_zone = arguments[0] if arguments[0] | 17 time_zone = arguments[0] if arguments[0] |
19 | 18 |
20 if (arguments.size != 0) and (arguments.size != 1) then | 19 if !arguments.empty? && (arguments.size != 1) |
21 raise(Puppet::ParseError, "time(): Wrong number of arguments "+ | 20 raise(Puppet::ParseError, "time(): Wrong number of arguments given #{arguments.size} for 0 or 1") |
22 "given #{arguments.size} for 0 or 1") | |
23 end | 21 end |
24 | 22 |
25 time = Time.new | 23 time = Time.new |
26 | 24 |
27 # There is probably a better way to handle Time Zone ... | 25 # There is probably a better way to handle Time Zone ... |
28 if time_zone and not time_zone.empty? | 26 if time_zone && !time_zone.empty? |
29 original_zone = ENV['TZ'] | 27 original_zone = ENV['TZ'] |
30 | 28 |
31 local_time = time.clone | 29 local_time = time.clone |
32 local_time = local_time.utc | 30 local_time = local_time.utc |
33 | 31 |
34 ENV['TZ'] = time_zone | 32 ENV['TZ'] = time_zone |
35 | 33 |
36 time = local_time.localtime | 34 result = local_time.localtime.strftime('%s') |
37 | 35 |
38 ENV['TZ'] = original_zone | 36 ENV['TZ'] = original_zone |
37 else | |
38 result = time.localtime.strftime('%s') | |
39 end | 39 end |
40 | 40 |
41 # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. | 41 # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. |
42 result = time.strftime('%s') | |
43 result = result.to_i | 42 result = result.to_i |
44 | 43 |
45 return result | 44 return result |
46 end | 45 end |
47 end | 46 end |