Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / time.rb
1 #
2 # time.rb
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.
7
8     *Examples:*
9
10         time()
11
12     Will return something like: 1311972653
13
14     Note that since Puppet 4.8.0 the Puppet language has the data types Timestamp (a point in time) and
15     Timespan (a duration). The following example is equivalent to calling time() without
16     any arguments:
17
18         Timestamp()
19
20     DOC
21              ) do |arguments|
22
23     # The Time Zone argument is optional ...
24     time_zone = arguments[0] if arguments[0]
25
26     if !arguments.empty? && (arguments.size != 1)
27       raise(Puppet::ParseError, "time(): Wrong number of arguments given #{arguments.size} for 0 or 1")
28     end
29
30     time = Time.new
31
32     # There is probably a better way to handle Time Zone ...
33     if time_zone && !time_zone.empty?
34       original_zone = ENV['TZ']
35
36       local_time = time.clone
37       local_time = local_time.utc
38
39       ENV['TZ'] = time_zone
40
41       result = local_time.localtime.strftime('%s')
42
43       ENV['TZ'] = original_zone
44     else
45       result = time.localtime.strftime('%s')
46     end
47
48     # Calling Time#to_i on a receiver changes it.  Trust me I am the Doctor.
49     result = result.to_i
50
51     return result
52   end
53 end
54
55 # vim: set ts=2 sw=2 et :