xref: /netbsd-src/lib/libc/time/strftime.3 (revision 18b32cd19a7876a54ee1a4164d407422179e38ed)
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\" 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.\"     from: @(#)strftime.3	5.12 (Berkeley) 6/29/91
33.\"	$NetBSD: strftime.3,v 1.38 2025/01/23 22:44:22 christos Exp $
34.\"
35.Dd October 9, 2020
36.Dt STRFTIME 3
37.Os
38.Sh NAME
39.Nm strftime ,
40.Nm strftime_z
41.Nd format date and time
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In time.h
46.Ft size_t
47.Fn strftime "char * restrict buf" "size_t maxsize" "const char * restrict format" "const struct tm * restrict timeptr"
48.Ft size_t
49.Fn strftime_z "const timezone_t tz" "char * restrict buf" "size_t maxsize" "const char * restrict format" "const struct tm * restrict timeptr"
50.Sh DESCRIPTION
51The
52.Fn strftime
53function formats the information from
54.Fa timeptr
55into the array pointed to by
56.Fa buf
57according to the string pointed to by
58.Fa format .
59.Pp
60The
61.Fa format
62string consists of zero or more conversion specifications and
63ordinary characters.
64All ordinary characters are copied directly into the array.
65A conversion specification consists of a percent sign
66.Ql %
67and one other character.
68.Pp
69No more than
70.Fa maxsize
71bytes will be placed into the array.
72Otherwise, zero is returned.
73.Pp
74Each conversion specification is replaced by the characters as
75follows which are then copied into the array.
76The characters depend on the values of zero or more members of
77.Fa timeptr
78as specified by brackets in the description.
79If a bracketed member name is followed by
80.Dq + ,
81.Nm strftime
82can use the named member even though POSIX.1-2024 does not list it;
83if the name is followed by
84.Dq \&- ,
85.Nm strftime
86ignores the member even though POSIX.1-2024 lists it
87which means portable code should set it.
88For portability,
89.Fa timeptr
90should be initialized as if by a successful call to
91.Xr gmtime 3 ,
92.Xr localtime 3 ,
93.Xr mktime 3 ,
94.Xr timegm 3 ,
95or similar functions.
96.Bl -tag -width "xxxx"
97.It Cm \&%A
98is replaced by the locale's full weekday name.
99.Dv [ tm_wday ]
100.It Cm %a
101is replaced by the locale's abbreviated weekday name.
102.Dv [ tm_wday ]
103.It Cm \&%B
104is replaced by the locale's full month name.
105.Dv [ tm_mon ]
106.It Cm \&%b No or Cm \&%h
107is replaced by the locale's abbreviated month name.
108.RI [ tm_mon ]
109.It Cm \&%C
110is replaced by the century (a year divided by 100 and truncated to an integer)
111as a decimal number, with at least two digits by default.
112.RI [ tm_year ]
113.It Cm \&%c
114is replaced by the locale's appropriate date and time representation.
115.Dv [ tm_year ,
116.Dv tm_yday ,
117.Dv tm_mon ,
118.Dv tm_mday ,
119.Dv tm_wday ,
120.Dv tm_hour ,
121.Dv tm_min ,
122.Dv tm_sec ,
123.Dv tm_gmtoff ,
124.Dv tm_zone ,
125.Dv tm_isdst \&-].
126.It Cm \&%D
127is replaced by the date in the format
128.Dq Li %m/%d/%y .
129Although used in the United States for current dates,
130this format is ambiguous elsewhere
131and for dates that might involve other centuries.
132.Dv [ tm_year ,
133.Dv tm_mon ,
134.Dv tm_mday ]
135.It Cm \&%d
136is replaced by the day of the month as a decimal number [01,31].
137.Dv [ tm_mday ]
138.It Cm \&%e
139is replaced by the day of month as a decimal number [1,31];
140single digits are preceded by a blank.
141.Dv [ tm_mday ]
142.It Cm \&%F
143is equivalent to
144.Dq Li %Y-%m-%d
145(the ISO 8601 date format).
146.Dv [ tm_year ,
147.Dv tm_mon ,
148.Dv tm_mday ]
149.It Cm \&%G
150is replaced by the ISO 8601 year with century as a decimal number.
151See also the
152.Cm \&%V
153conversion specification
154.Dv [ tm_year ,
155.Dv tm_yday ,
156.Dv tm_wday ]
157.It Cm \&%g
158is replaced by the ISO 8601 year without century as a decimal number.
159This is the year that includes the greater part of the week.
160(Monday as the first day of a week).
161See also the
162.Ql \&%V
163conversion specification.
164.Dv [ tm_year ,
165.Dv tm_yday ,
166.Dv tm_wday ]
167.It Cm \&%H
168is replaced by the hour (24-hour clock) as a decimal number [00,23].
169.Dv [ tm_hour ]
170.It Cm \&%I
171is replaced by the hour (12-hour clock) as a decimal number [01,12].
172.Dv [ tm_hour ]
173.It Cm \&%j
174is replaced by the day of the year as a decimal number [001,366].
175.Dv [ tm_yday ]
176.It Cm \&%k
177is replaced by the hour (24-hour clock) as a decimal number [0,23];
178single digits are preceded by a blank.
179.Dv [ tm_hour ]
180.It Cm \&%l
181is replaced by the hour (12-hour clock) as a decimal number [1,12];
182single digits are preceded by a blank.
183.Dv [ tm_hour ]
184.It Cm \&%M
185is replaced by the minute as a decimal number [00,59].
186.Dv [ tm_min ]
187.It Cm %m
188is replaced by the month as a decimal number [01,12].
189.Dv [ tm_mon ]
190.It Cm %n
191is replaced by a newline.
192.It Cm %p
193is replaced by the locale's equivalent of either
194.Dq Tn AM
195or
196.Dq Tn PM .
197.Dv [ tm_hour ]
198.It Cm \&%R
199is replaced by the time in the format
200.Dq Li %H:%M .
201.Dv [ tm_hour ,
202.Dv tm_min ]
203.It Cm \&%r
204is replaced by the locale's representation of 12-hour clock time
205using AM/PM notation.
206.It Cm \&%S
207is replaced by the second as a decimal number [00,60].
208The range of
209seconds is [00-60] instead of [00-59] to allow for the periodic occurrence
210of leap seconds.
211.Dv [ tm_sec ]
212.It Cm \&%s
213is replaced by the number of seconds since the Epoch (see
214.Xr ctime 3 ) .
215Although %s is reliable in this implementation,
216it can have glitches on other platforms
217(notably obsolescent platforms lacking
218.Fa tm_gmtoff
219or where
220.Tp time_t
221is no wider than int), and POSIX allows
222.Nm strftime
223to set
224.Dv errno
225to
226.Dv EINVAL
227or
228.Dv EOVERFLOW
229and return 0 if the number of seconds would be negative or out of range for
230.Tp time_t .
231Portable code should therefore format a
232.Tp time_t
233value directly via something like
234.Xr snprintf 3
235instead of via
236.Xr localtime 3
237followed by
238.Nm strftime
239with "%s".
240.Dv [ tm_year ,
241.Dv tm_mon ,
242.Dv tm_mday ,
243.Dv tm_hour ,
244.Dv tm_min ,
245.Dv tm_sec ,
246.Dv tm_gmtoff +,
247.Dv tm_isdst \&-].
248.It Cm \&%T
249is replaced by the time in the format
250.Dq Li %H:%M:%S .
251.Dv [ tm_hour ,
252.Dv tm_min ,
253.Dv tm_sec ]
254.It Cm \&%t
255is replaced by a tab.
256.It Cm \&%U
257is replaced by the week number of the year (Sunday as the first day of
258the week) as a decimal number [00,53].
259.Dv [ tm_wday ,
260.Dv tm_yday ,
261.Dv tm_year \&-]
262.It Cm \&%u
263is replaced by the weekday (Monday as the first day of the week)
264as a decimal number [1,7].
265.Dv [ tm_wday ]
266.It Cm \&%V
267is replaced by the week number of the year (Monday as the first day of
268the week) as a decimal number [01,53]. According to ISO 8601 the week
269containing January 1 is week 1 if it has four or more days in the new year,
270otherwise it is week 53 of the previous year, and the next week is week 1.
271The year is given by the
272.Ql \&%G
273conversion specification.
274.Dv [ tm_year ,
275.Dv tm_yday ,
276.Dv tm_wday ]
277.It Cm \&%v
278is replaced by the date in the format
279.Dq Li %e-%b-%Y .
280.Dv [ tm_year ,
281.Dv tm_yday ,
282.Dv tm_wday ]
283.It Cm \&%W
284is replaced by the week number of the year (Monday as the first day of
285the week) as a decimal number [00,53].
286.Dv [ tm_yday ,
287.Dv tm_wday ]
288.It Cm \&%w
289is replaced by the weekday (Sunday as the first day of the week)
290as a decimal number [0,6].
291.Dv [ tm_year ,
292.Dv tm_yday ,
293.Dv tm_wday ]
294.It Cm \&%X
295is replaced by the locale's appropriate time representation.
296.Dv [ tm_year \&-,
297.Dv tm_yday \&-,
298.Dv tm_mon \&-,
299.Dv tm_mday \&-,
300.Dv tm_wday \&-,
301.Dv tm_hour ,
302.Dv tm_min ,
303.Dv tm_sec ,
304.Dv tm_gmtoff ,
305.Dv tm_zone ,
306.Dv tm_isdst \&-].
307.It Cm \&%x
308is replaced by the locale's appropriate date representation.
309.Dv [ tm_year ,
310.Dv tm_yday ,
311.Dv tm_mon ,
312.Dv tm_mday ,
313.Dv tm_wday ,
314.Dv tm_hour \&-,
315.Dv tm_min \&-,
316.Dv tm_sec \&-,
317.Dv tm_gmtoff \&-,
318.Dv tm_zone \&-,
319.Dv tm_isdst \&-].
320.It Cm \&%Y
321is replaced by the year with century as a decimal number.
322.Dv [ tm_year ]
323.It Cm \&%y
324is replaced by the year without century as a decimal number [00,99].
325.Dv [ tm_year ]
326.It Cm \&%Z
327is replaced by the time zone abbreviation,
328or the empty string if this is not determinable.
329.Dv [ tm_zone ,
330.Dv tm_isdst \&-]
331.It Cm \&%z
332is replaced by the offset from the Prime Meridian in the format
333+HHMM or -HHMM (ISO 8601) as appropriate, with positive values representing
334locations east of Greenwich, or by the empty string if this is
335not determinable.
336The numeric time zone abbreviation \&-0000 is used when the time is
337Universal Time
338but local time is indeterminate; by convention this is used for
339locations while uninhabited, and corresponds to a zero offset when the
340time zone abbreviation begins with
341.Dq Li [-] .
342.It Cm %+
343is replaced by locale's date and time in
344.Xr date 1
345format.
346On
347.Nx
348currently this only works for the C locale.
349.Dv [ tm_year ,
350.Dv tm_yday ,
351.Dv tm_mon ,
352.Dv tm_mday ,
353.Dv tm_wday ,
354.Dv tm_hour ,
355.Dv tm_min ,
356.Dv tm_sec ,
357.Dv tm_gmtoff ,
358.Dv tm_zone ]
359.It Cm %-*
360GNU libc extension.
361Do not do any padding when performing numerical outputs.
362.It Cm %_*
363GNU libc extension.
364Explicitly specify space for padding.
365.It Cm %0*
366GNU libc extension.
367Explicitly specify zero for padding.
368.It Cm %%
369is replaced by as single
370.Ql % .
371.El
372.Pp
373As a side effect,
374.Nm strftime
375also behaves as if
376.Xr tzset 3
377were called.
378This is for compatibility with older platforms, as required by POSIX;
379it is not needed for
380.Nm strftime
381's
382own use.
383.Pp
384The
385.Fn strftime_z
386function is similar to
387.Fn strftime ,
388but it also takes a
389.Ft "const timezone_t"
390.Fa tz
391argument.
392.Sh RETURN VALUES
393If the conversion is successful,
394.Nm
395returns the number of bytes placed into the array, not counting the
396terminating
397.Dv NUL ;
398.Va errno
399is unchanged if the returned value is zero.
400Otherwise,
401.Va errno
402is set to indicate the error, zero is returned,
403and the array contents are unspecified.
404.Sh ERRORS
405This function fails if:
406.Bl -tag -width Er
407.It Bq Er ERANGE
408The specified file offset is invalid.
409The total number of resulting bytes, including the terminating
410.Dv NUL
411character, is more than
412.Fa maxsize .
413.It Bq Er EOVERFLOW
414The format includes an
415.Cm \&%s
416conversion and the number of seconds since the Epoch cannot be represented
417in a
418.Ft time_t .
419.El
420.Sh SEE ALSO
421.Xr date 1 ,
422.Xr printf 1 ,
423.Xr ctime 3 ,
424.Xr printf 3 ,
425.Xr strptime 3 ,
426.Xr tm 3
427.Sh STANDARDS
428The
429.Fn strftime
430function
431conforms to
432.St -isoC-99 .
433The
434.Ql \&%C ,
435.Ql \&%D ,
436.Ql \&%e ,
437.Ql \&%g ,
438.Ql \&%G ,
439.Ql \&%h ,
440.Ql \&%k ,
441.Ql \&%l ,
442.Ql \&%n ,
443.Ql \&%r ,
444.Ql \&%R ,
445.Ql \&%s ,
446.Ql \&%t ,
447.Ql \&%T ,
448.Ql \&%u ,
449.Ql \&%V ,
450and
451.Ql \&%v
452conversion specifications are extensions.
453.Pp
454Use of the ISO 8601 conversions may produce non-intuitive results.
455Week 01 of a year is per definition the first week which has the Thursday
456in this year, which is equivalent to the week which contains the fourth
457day of January.
458In other words, the first week of a new year is the week which has the
459majority of its days in the new year.
460Week 01 might also contain days from the previous year and the week
461before week 01 of a year is the last week (52 or 53) of the previous
462year even if it contains days from the new year.
463A week starts with Monday (day 1) and ends with Sunday (day 7).
464For example, the first week of the year 1997 lasts from
4651996-12-30 to 1997-01-05.
466.Sh BUGS
467There is no conversion specification for the phase of the moon.
468.Pp
469A return value of zero does not necessarily indicate an error.
470If the resulting string is an empty string, the result value is
471zero and it is not possible to distinguish between success and error.
472For example, in many locales
473.Cm \&%p
474yields an empty string.
475This problem can be avoided by inserting an extra space at the
476beginning of the format string and then skipping over it or removing
477it from the result.
478