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 / strftime.rb
1 #
2 #  strftime.rb
3 #  Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 #
5 module Puppet::Parser::Functions
6   newfunction(:strftime, :type => :rvalue, :doc => <<-DOC
7     This function returns formatted time.
8
9     Note that since Puppet 4.8.0 the function with the same name in Puppet will be used instead of this
10     function. It also supports the Timestamp and Timespan data types in the Puppet language.
11
12     *Examples:*
13
14     To return the time since epoch:
15
16         strftime("%s")
17
18     To return the date:
19
20         strftime("%Y-%m-%d")
21
22     *Format meaning:*
23
24         %a - The abbreviated weekday name (``Sun'')
25         %A - The  full  weekday  name (``Sunday'')
26         %b - The abbreviated month name (``Jan'')
27         %B - The  full  month  name (``January'')
28         %c - The preferred local date and time representation
29         %C - Century (20 in 2009)
30         %d - Day of the month (01..31)
31         %D - Date (%m/%d/%y)
32         %e - Day of the month, blank-padded ( 1..31)
33         %F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
34         %h - Equivalent to %b
35         %H - Hour of the day, 24-hour clock (00..23)
36         %I - Hour of the day, 12-hour clock (01..12)
37         %j - Day of the year (001..366)
38         %k - hour, 24-hour clock, blank-padded ( 0..23)
39         %l - hour, 12-hour clock, blank-padded ( 0..12)
40         %L - Millisecond of the second (000..999)
41         %m - Month of the year (01..12)
42         %M - Minute of the hour (00..59)
43         %n - Newline (\n)
44         %N - Fractional seconds digits, default is 9 digits (nanosecond)
45                 %3N  millisecond (3 digits)
46                 %6N  microsecond (6 digits)
47                 %9N  nanosecond (9 digits)
48         %p - Meridian indicator (``AM''  or  ``PM'')
49         %P - Meridian indicator (``am''  or  ``pm'')
50         %r - time, 12-hour (same as %I:%M:%S %p)
51         %R - time, 24-hour (%H:%M)
52         %s - Number of seconds since 1970-01-01 00:00:00 UTC.
53         %S - Second of the minute (00..60)
54         %t - Tab character (\t)
55         %T - time, 24-hour (%H:%M:%S)
56         %u - Day of the week as a decimal, Monday being 1. (1..7)
57         %U - Week  number  of the current year,
58                 starting with the first Sunday as the first
59                 day of the first week (00..53)
60         %v - VMS date (%e-%b-%Y)
61         %V - Week number of year according to ISO 8601 (01..53)
62         %W - Week  number  of the current year,
63                 starting with the first Monday as the first
64                 day of the first week (00..53)
65         %w - Day of the week (Sunday is 0, 0..6)
66         %x - Preferred representation for the date alone, no time
67         %X - Preferred representation for the time alone, no date
68         %y - Year without a century (00..99)
69         %Y - Year with century
70         %z - Time zone as  hour offset from UTC (e.g. +0900)
71         %Z - Time zone name
72         %% - Literal ``%'' character
73     DOC
74              ) do |arguments|
75
76     # Technically we support two arguments but only first is mandatory ...
77     raise(Puppet::ParseError, "strftime(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
78
79     format = arguments[0]
80
81     raise(Puppet::ParseError, 'strftime(): You must provide format for evaluation') if format.empty?
82
83     # The Time Zone argument is optional ...
84     time_zone = arguments[1] if arguments[1]
85
86     time = Time.new
87
88     # There is probably a better way to handle Time Zone ...
89     if time_zone && !time_zone.empty?
90       original_zone = ENV['TZ']
91
92       local_time = time.clone
93       local_time = local_time.utc
94
95       ENV['TZ'] = time_zone
96
97       time = local_time.localtime
98
99       ENV['TZ'] = original_zone
100     end
101
102     result = time.strftime(format)
103
104     return result
105   end
106 end
107
108 # vim: set ts=2 sw=2 et :