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.
5 module Puppet::Parser::Functions
6 newfunction(:strftime, :type => :rvalue, :doc => <<-DOC
7 This function returns formatted time.
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.
14 To return the time since epoch:
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)
32 %e - Day of the month, blank-padded ( 1..31)
33 %F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
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)
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)
72 %% - Literal ``%'' character
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?
81 raise(Puppet::ParseError, 'strftime(): You must provide format for evaluation') if format.empty?
83 # The Time Zone argument is optional ...
84 time_zone = arguments[1] if arguments[1]
88 # There is probably a better way to handle Time Zone ...
89 if time_zone && !time_zone.empty?
90 original_zone = ENV['TZ']
92 local_time = time.clone
93 local_time = local_time.utc
97 time = local_time.localtime
99 ENV['TZ'] = original_zone
102 result = time.strftime(format)
108 # vim: set ts=2 sw=2 et :