xref: /freebsd/lib/libc/stdtime/strftime.3 (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1.\" Copyright (c) 1989, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd June 25, 2012
33.Dt STRFTIME 3
34.Os
35.Sh NAME
36.Nm strftime
37.Nd format date and time
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In time.h
42.Ft size_t
43.Fo strftime
44.Fa "char * restrict buf"
45.Fa "size_t maxsize"
46.Fa "const char * restrict format"
47.Fa "const struct tm * restrict timeptr"
48.Fc
49.Ft size_t
50.Fn strftime_l "char *restrict buf" "size_t maxsize" "const char * restrict format" "const struct tm *restrict timeptr" "locale_t loc"
51.Sh DESCRIPTION
52The
53.Fn strftime
54function formats the information from
55.Fa timeptr
56into the buffer
57.Fa buf
58according to the string pointed to by
59.Fa format .
60The function
61.Fn strftime_l
62does the same as
63.Fn strftime
64but takes an explicit locale rather than using the current locale.
65.Pp
66The
67.Fa format
68string consists of zero or more conversion specifications and
69ordinary characters.
70All ordinary characters are copied directly into the buffer.
71A conversion specification consists of a percent sign
72.Dq Ql %
73and one other character.
74.Pp
75No more than
76.Fa maxsize
77characters will be placed into the array.
78If the total number of resulting characters, including the terminating
79NUL character, is not more than
80.Fa maxsize ,
81.Fn strftime
82returns the number of characters in the array, not counting the
83terminating NUL.
84Otherwise, zero is returned and the buffer contents are indeterminate.
85.Pp
86The conversion specifications are copied to the buffer after expansion
87as follows:
88.Bl -tag -width "xxxx"
89.It Cm \&%A
90is replaced by national representation of the full weekday name.
91.It Cm %a
92is replaced by national representation of
93the abbreviated weekday name.
94.It Cm \&%B
95is replaced by national representation of the full month name.
96.It Cm %b
97is replaced by national representation of
98the abbreviated month name.
99.It Cm \&%C
100is replaced by (year / 100) as decimal number; single
101digits are preceded by a zero.
102.It Cm %c
103is replaced by national representation of time and date.
104.It Cm \&%D
105is equivalent to
106.Dq Li %m/%d/%y .
107.It Cm %d
108is replaced by the day of the month as a decimal number (01-31).
109.It Cm %E* %O*
110POSIX locale extensions.
111The sequences
112%Ec %EC %Ex %EX %Ey %EY
113%Od %Oe %OH %OI %Om %OM
114%OS %Ou %OU %OV %Ow %OW %Oy
115are supposed to provide alternate
116representations.
117.Pp
118Additionally %OB implemented
119to represent alternative months names
120(used standalone, without day mentioned).
121.It Cm %e
122is replaced by the day of the month as a decimal number (1-31); single
123digits are preceded by a blank.
124.It Cm \&%F
125is equivalent to
126.Dq Li %Y-%m-%d .
127.It Cm \&%G
128is replaced by a year as a decimal number with century.
129This year is the one that contains the greater part of
130the week (Monday as the first day of the week).
131.It Cm %g
132is replaced by the same year as in
133.Dq Li %G ,
134but as a decimal number without century (00-99).
135.It Cm \&%H
136is replaced by the hour (24-hour clock) as a decimal number (00-23).
137.It Cm %h
138the same as
139.Cm %b .
140.It Cm \&%I
141is replaced by the hour (12-hour clock) as a decimal number (01-12).
142.It Cm %j
143is replaced by the day of the year as a decimal number (001-366).
144.It Cm %k
145is replaced by the hour (24-hour clock) as a decimal number (0-23);
146single digits are preceded by a blank.
147.It Cm %l
148is replaced by the hour (12-hour clock) as a decimal number (1-12);
149single digits are preceded by a blank.
150.It Cm \&%M
151is replaced by the minute as a decimal number (00-59).
152.It Cm %m
153is replaced by the month as a decimal number (01-12).
154.It Cm %n
155is replaced by a newline.
156.It Cm %O*
157the same as
158.Cm %E* .
159.It Cm %p
160is replaced by national representation of either
161"ante meridiem" (a.m.)
162or
163"post meridiem" (p.m.)
164as appropriate.
165.It Cm \&%R
166is equivalent to
167.Dq Li %H:%M .
168.It Cm %r
169is equivalent to
170.Dq Li %I:%M:%S %p .
171.It Cm \&%S
172is replaced by the second as a decimal number (00-60).
173.It Cm %s
174is replaced by the number of seconds since the Epoch, UTC (see
175.Xr mktime 3 ) .
176.It Cm \&%T
177is equivalent to
178.Dq Li %H:%M:%S .
179.It Cm %t
180is replaced by a tab.
181.It Cm \&%U
182is replaced by the week number of the year (Sunday as the first day of
183the week) as a decimal number (00-53).
184.It Cm %u
185is replaced by the weekday (Monday as the first day of the week)
186as a decimal number (1-7).
187.It Cm \&%V
188is replaced by the week number of the year (Monday as the first day of
189the week) as a decimal number (01-53).
190If the week containing January
1911 has four or more days in the new year, then it is week 1; otherwise
192it is the last week of the previous year, and the next week is week 1.
193.It Cm %v
194is equivalent to
195.Dq Li %e-%b-%Y .
196.It Cm \&%W
197is replaced by the week number of the year (Monday as the first day of
198the week) as a decimal number (00-53).
199.It Cm %w
200is replaced by the weekday (Sunday as the first day of the week)
201as a decimal number (0-6).
202.It Cm \&%X
203is replaced by national representation of the time.
204.It Cm %x
205is replaced by national representation of the date.
206.It Cm \&%Y
207is replaced by the year with century as a decimal number.
208.It Cm %y
209is replaced by the year without century as a decimal number (00-99).
210.It Cm \&%Z
211is replaced by the time zone name.
212.It Cm %z
213is replaced by the time zone offset from UTC; a leading plus sign stands for
214east of UTC, a minus sign for west of UTC, hours and minutes follow
215with two digits each and no delimiter between them (common form for
216RFC 822 date headers).
217.It Cm %+
218is replaced by national representation of the date and time
219(the format is similar to that produced by
220.Xr date 1 ) .
221.It Cm %-*
222GNU libc extension.
223Do not do any padding when performing numerical outputs.
224.It Cm %_*
225GNU libc extension.
226Explicitly specify space for padding.
227.It Cm %0*
228GNU libc extension.
229Explicitly specify zero for padding.
230.It Cm %%
231is replaced by
232.Ql % .
233.El
234.Sh SEE ALSO
235.Xr date 1 ,
236.Xr printf 1 ,
237.Xr ctime 3 ,
238.Xr printf 3 ,
239.Xr strptime 3 ,
240.Xr wcsftime 3
241.Sh STANDARDS
242The
243.Fn strftime
244function
245conforms to
246.St -isoC
247with a lot of extensions including
248.Ql \&%C ,
249.Ql \&%D ,
250.Ql %E* ,
251.Ql %e ,
252.Ql %G ,
253.Ql %g ,
254.Ql %h ,
255.Ql %k ,
256.Ql %l ,
257.Ql %n ,
258.Ql %O* ,
259.Ql \&%R ,
260.Ql %r ,
261.Ql %s ,
262.Ql \&%T ,
263.Ql %t ,
264.Ql %u ,
265.Ql \&%V ,
266.Ql %z ,
267.Ql %+ .
268.Pp
269The peculiar week number and year in the replacements of
270.Ql %G ,
271.Ql %g
272and
273.Ql \&%V
274are defined in
275.St -iso8601 .
276The
277.Fn strftime_l
278function conforms to
279.St -p1003.1-2008 .
280.Sh BUGS
281There is no conversion specification for the phase of the moon.
282.Pp
283The
284.Fn strftime
285function does not correctly handle multibyte characters in the
286.Fa format
287argument.
288