Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / 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 given #{arguments.size} for 0 or 1")
22     end
23
24     time = Time.new
25
26     # There is probably a better way to handle Time Zone ...
27     if time_zone and not time_zone.empty?
28       original_zone = ENV['TZ']
29
30       local_time = time.clone
31       local_time = local_time.utc
32
33       ENV['TZ'] = time_zone
34
35       result = local_time.localtime.strftime('%s')
36
37       ENV['TZ'] = original_zone
38     else
39       result = time.localtime.strftime('%s')
40     end
41
42     # Calling Time#to_i on a receiver changes it.  Trust me I am the Doctor.
43     result = result.to_i
44
45     return result
46   end
47 end
48
49 # vim: set ts=2 sw=2 et :