]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/time.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / time.rb
1 #
2 # time.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:time, :type => :rvalue, :doc => <<-EOS
7 This function will return the current time since epoch as an integer.
8
9 *Examples:*
10
11     time()
12
13 Will return something like: 1311972653
14     EOS
15   ) do |arguments|
16
17     # The Time Zone argument is optional ...
18     time_zone = arguments[0] if arguments[0]
19
20     if (arguments.size != 0) and (arguments.size != 1) then
21       raise(Puppet::ParseError, "time(): Wrong number of arguments "+
22         "given #{arguments.size} for 0 or 1")
23     end
24
25     time = Time.new
26
27     # There is probably a better way to handle Time Zone ...
28     if time_zone and not time_zone.empty?
29       original_zone = ENV['TZ']
30
31       local_time = time.clone
32       local_time = local_time.utc
33
34       ENV['TZ'] = time_zone
35
36       time = local_time.localtime
37
38       ENV['TZ'] = original_zone
39     end
40
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
44
45     return result
46   end
47 end
48
49 # vim: set ts=2 sw=2 et :