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