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