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