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