xref: /openbsd-src/lib/libc/time/strftime.3 (revision be38755c412cc72cb8d40f51ea70c9893196afff)
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.\"	$OpenBSD: strftime.3,v 1.27 2011/09/16 19:36:07 jmc Exp $
34.\"
35.Dd $Mdocdate: September 16 2011 $
36.Dt STRFTIME 3
37.Os
38.Sh NAME
39.Nm strftime
40.Nd format date and time
41.Sh SYNOPSIS
42.Fd #include <time.h>
43.Ft size_t
44.Fn strftime "char *buf" "size_t maxsize" "const char *format" "const struct tm *timeptr"
45.Sh DESCRIPTION
46The
47.Fn strftime
48function formats the information from
49.Fa timeptr
50into the buffer
51.Fa buf
52according to the string pointed to by
53.Fa format .
54.Pp
55The
56.Fa format
57string consists of zero or more conversion specifications and
58ordinary characters.
59All ordinary characters are copied directly into the buffer.
60A conversion specification consists of a percent sign
61.Ql %
62and one other character.
63.Pp
64No more than
65.Fa maxsize
66characters will be placed into the array.
67If the total number of resulting characters, including the terminating
68NUL character, is not more than
69.Fa maxsize ,
70.Fn strftime
71returns the number of characters in the array, not counting the
72terminating NUL.
73Otherwise, zero is returned.
74.Pp
75Each conversion specification is replaced by the characters as
76follows which are then copied into the buffer.
77.Bl -tag -width "xxxx"
78.It Cm \&%A
79is replaced by the locale's full weekday name.
80.It Cm \&%a
81is replaced by the locale's abbreviated weekday name.
82.It Cm \&%B
83is replaced by the locale's full month name.
84.It Cm \&%b No or Cm \&%h
85is replaced by the locale's abbreviated month name.
86.It Cm \&%C
87is replaced by the century (a year divided by 100 and truncated to an integer)
88as a decimal number (00\-99).
89.It Cm \&%c
90is replaced by the locale's appropriate date and time representation.
91.It Cm \&%D
92is replaced by the date in the format
93.Dq Li %m/%d/%y .
94.It Cm \&%d
95is replaced by the day of the month as a decimal number (01\-31).
96.It Cm \&%e
97is replaced by the day of month as a decimal number (1\-31);
98single digits are preceded by a blank.
99.It Cm \&%F
100is replaced by the date in the format
101.Dq Li %Y-%m-%d .
102.It Cm \&%G
103is replaced by the
104.St -iso8601
105year with century as a decimal number.
106.It Cm \&%g
107is replaced by the
108.St -iso8601
109year without century as a decimal number (00\-99).
110.It Cm \&%H
111is replaced by the hour (24-hour clock) as a decimal number (00\-23).
112.It Cm \&%I
113is replaced by the hour (12-hour clock) as a decimal number (01\-12).
114.It Cm \&%j
115is replaced by the day of the year as a decimal number (001\-366).
116.It Cm \&%k
117is replaced by the hour (24-hour clock) as a decimal number (0\-23);
118single digits are preceded by a blank.
119.It Cm \&%l
120is replaced by the hour (12-hour clock) as a decimal number (1\-12);
121single digits are preceded by a blank.
122.It Cm \&%M
123is replaced by the minute as a decimal number (00\-59).
124.It Cm %m
125is replaced by the month as a decimal number (01\-12).
126.It Cm %n
127is replaced by a newline.
128.It Cm %p
129is replaced by the locale's equivalent of either
130.Dq Tn AM
131or
132.Dq Tn PM .
133.It Cm \&%R
134is replaced by the time in the format
135.Dq Li %H:%M .
136.It Cm \&%r
137is replaced by the locale's representation of 12-hour clock time
138using AM/PM notation.
139.It Cm \&%S
140is replaced by the second as a decimal number (00\-60).
141The range of
142seconds is (00\-60) instead of (00\-59) to allow for the periodic occurrence
143of leap seconds.
144.It Cm %s
145is replaced by the number of seconds since the Epoch, UTC (see
146.Xr mktime 3 ) .
147.It Cm \&%T
148is replaced by the time in the format
149.Dq Li %H:%M:%S .
150.It Cm \&%t
151is replaced by a tab.
152.It Cm \&%U
153is replaced by the week number of the year (Sunday as the first day of
154the week) as a decimal number (00\-53).
155.It Cm \&%u
156is replaced by the weekday (Monday as the first day of the week)
157as a decimal number (1\-7).
158.It Cm \&%V
159is replaced by the week number of the year (Monday as the first day of
160the week) as a decimal number (01\-53).
161If the week containing January
1621 has four or more days in the new year, then it is week 1; otherwise
163it is week 53 of the previous year, and the next week is week 1.
164.It Cm \&%v
165is replaced by the date in the format
166.Dq Li "%e-%b-%Y" .
167.It Cm \&%W
168is replaced by the week number of the year (Monday as the first day of
169the week) as a decimal number (00\-53).
170.It Cm \&%w
171is replaced by the weekday (Sunday as the first day of the week)
172as a decimal number (0\-6).
173.It Cm \&%X
174is replaced by the locale's appropriate time representation.
175.It Cm \&%x
176is replaced by the locale's appropriate date representation.
177.It Cm \&%Y
178is replaced by the year with century as a decimal number.
179.It Cm \&%y
180is replaced by the year without century as a decimal number (00\-99).
181.It Cm \&%Z
182is replaced by the time zone name,
183or by the empty string if this is not determinable.
184.It Cm \&%z
185is replaced by the offset from UTC in the format
186.Dq Li +HHMM
187or
188.Dq Li -HHMM
189as appropriate, with positive values representing locations east
190of Greenwich, or by the empty string if this is not determinable.
191.It Cm %%
192is replaced by
193.Ql % .
194.It Cm %+
195is replaced by the date and time in
196.Xr date 1
197format.
198.El
199.Sh SEE ALSO
200.Xr date 1 ,
201.Xr printf 1 ,
202.Xr ctime 3 ,
203.Xr getenv 3 ,
204.Xr printf 3 ,
205.Xr strptime 3 ,
206.Xr time 3 ,
207.Xr tzset 3 ,
208.Xr tzfile 5
209.Sh STANDARDS
210The
211.Fn strftime
212function
213conforms to
214.St -ansiC .
215.Pp
216The
217.Ql \&%G ,
218.Ql \&%g ,
219.Ql \&%k ,
220.Ql \&%l ,
221.Ql \&%s ,
222.Ql \&%v ,
223and
224.Ql \&%+
225conversion specifications are extensions.
226.Pp
227Use of the
228.St -iso8601
229conversions may produce non-intuitive results.
230Week 01 of a year is per definition the first week which has the Thursday
231in this year, which is equivalent to the week which contains the fourth
232day of January.
233In other words, the first week of a new year is the week which has the
234majority of its days in the new year.
235Week 01 might also contain days from the previous year and the week
236before week 01 of a year is the last week (52 or 53) of the previous
237year even if it contains days from the new year.
238A week starts with Monday (day 1) and ends with Sunday (day 7).
239For example, the first week of the year 1997 lasts from
2401996-12-30 to 1997-01-05.
241.Sh BUGS
242There is no conversion specification for the phase of the moon.
243.Pp
244Note that while this implementation of
245.Fn strftime
246will always NUL terminate
247.Fa buf ,
248other implementations may not do so when
249.Fa maxsize
250is not large enough to store the entire time string.
251The contents of
252.Fa buf
253are implementation specific in this case.
254.\" @(#)newstrftime.3	8.2
255