]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strftime.3
Replaced tabs with spaces
[thirdparty/man-pages.git] / man3 / strftime.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\" GNU texinfo documentation on glibc date/time functions.
28 .\" Modified Sat Jul 24 18:03:44 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Applied fix by Wolfgang Franke, aeb, 961011
30 .\" Corrected return value, aeb, 970307
31 .\" Added Single Unix Spec conversions and %z, aeb/esr, 990329.
32 .\" 2005-11-22 mtk, added GLIBC NOTES covering optional 'flag' and
33 .\" 'width' components of conversion specifications.
34 .\"
35 .TH STRFTIME 3 2005-11-23 "GNU" "Linux Programmer's Manual"
36 .SH NAME
37 strftime \- format date and time
38 .SH SYNOPSIS
39 .nf
40 .B #include <time.h>
41 .sp
42 .BI "size_t strftime(char *" s ", size_t " max ", const char *" format ,
43 .BI " const struct tm *" tm );
44 .fi
45 .SH DESCRIPTION
46 The \fBstrftime\fP() function formats the broken-down time \fItm\fP
47 according to the format specification \fIformat\fP and places the
48 result in the character array \fIs\fP of size \fImax\fP.
49 .PP
50 Ordinary characters placed in the format string are copied to \fIs\fP
51 without conversion.
52 .I "Conversion specifications"
53 are introduced by a `%'
54 character, and terminated by a
55 .IR "conversion specifier character" ,
56 and are replaced in \fIs\fP as follows:
57 .TP
58 .B %a
59 The abbreviated weekday name according to the current locale.
60 .TP
61 .B %A
62 The full weekday name according to the current locale.
63 .TP
64 .B %b
65 The abbreviated month name according to the current locale.
66 .TP
67 .B %B
68 The full month name according to the current locale.
69 .TP
70 .B %c
71 The preferred date and time representation for the current locale.
72 .TP
73 .B %C
74 The century number (year/100) as a 2-digit integer. (SU)
75 .TP
76 .B %d
77 The day of the month as a decimal number (range 01 to 31).
78 .TP
79 .B %D
80 Equivalent to %m/%d/%y. (Yecch \(em for Americans only.
81 Americans should note that in other countries %d/%m/%y is rather
82 common. This means that in international context this format is
83 ambiguous and should not be used.) (SU)
84 .TP
85 .B %e
86 Like %d, the day of the month as a decimal number, but a leading
87 zero is replaced by a space. (SU)
88 .TP
89 .B %E
90 Modifier: use alternative format, see below. (SU)
91 .TP
92 .B %F
93 Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
94 .TP
95 .B %G
96 The ISO 8601 year with century as a decimal number.
97 The 4-digit year corresponding to the ISO week number (see %V).
98 This has the same format and value as %y, except that if the
99 ISO week number belongs to the previous or next year,
100 that year is used instead. (TZ)
101 .TP
102 .B %g
103 Like %G, but without century, i.e., with a 2-digit year (00-99). (TZ)
104 .TP
105 .B %h
106 Equivalent to %b. (SU)
107 .TP
108 .B %H
109 The hour as a decimal number using a 24-hour clock (range 00 to 23).
110 .TP
111 .B %I
112 The hour as a decimal number using a 12-hour clock (range 01 to 12).
113 .TP
114 .B %j
115 The day of the year as a decimal number (range 001 to 366).
116 .TP
117 .B %k
118 The hour (24-hour clock) as a decimal number (range 0 to 23);
119 single digits are preceded by a blank. (See also %H.) (TZ)
120 .TP
121 .B %l
122 The hour (12-hour clock) as a decimal number (range 1 to 12);
123 single digits are preceded by a blank. (See also %I.) (TZ)
124 .TP
125 .B %m
126 The month as a decimal number (range 01 to 12).
127 .TP
128 .B %M
129 The minute as a decimal number (range 00 to 59).
130 .TP
131 .B %n
132 A newline character. (SU)
133 .TP
134 .B %O
135 Modifier: use alternative format, see below. (SU)
136 .TP
137 .B %p
138 Either `AM' or `PM' according to the given time value, or the
139 corresponding strings for the current locale.
140 Noon is treated as `pm' and midnight as `am'.
141 .TP
142 .B %P
143 Like %p but in lowercase: `am' or `pm' or a corresponding
144 string for the current locale. (GNU)
145 .TP
146 .B %r
147 The time in a.m. or p.m. notation.
148 In the POSIX locale this is equivalent to `%I:%M:%S %p'. (SU)
149 .TP
150 .B %R
151 The time in 24-hour notation (%H:%M). (SU)
152 For a version including the seconds, see %T below.
153 .TP
154 .B %s
155 The number of seconds since the Epoch, i.e., since 1970-01-01
156 00:00:00 UTC. (TZ)
157 .TP
158 .B %S
159 The second as a decimal number (range 00 to 60).
160 (The range is up to 60 to allow for occasional leap seconds.)
161 .TP
162 .B %t
163 A tab character. (SU)
164 .TP
165 .B %T
166 The time in 24-hour notation (%H:%M:%S). (SU)
167 .TP
168 .B %u
169 The day of the week as a decimal, range 1 to 7, Monday being 1.
170 See also %w. (SU)
171 .TP
172 .B %U
173 The week number of the current year as a decimal number,
174 range 00 to 53, starting with the first Sunday as the first day
175 of week 01. See also %V and %W.
176 .TP
177 .B %V
178 The ISO 8601:1988 week number of the current year as a decimal number,
179 range 01 to 53, where week 1 is the first week that has at least
180 4 days in the current year, and with Monday as the first day of
181 the week. See also %U and %W. (SU)
182 .TP
183 .B %w
184 The day of the week as a decimal, range 0 to 6, Sunday being 0.
185 See also %u.
186 .TP
187 .B %W
188 The week number of the current year as a decimal number,
189 range 00 to 53, starting with the first Monday as the first day of week 01.
190 .TP
191 .B %x
192 The preferred date representation for the current locale without the time.
193 .TP
194 .B %X
195 The preferred time representation for the current locale without the date.
196 .TP
197 .B %y
198 The year as a decimal number without a century (range 00 to 99).
199 .TP
200 .B %Y
201 The year as a decimal number including the century.
202 .TP
203 .B %z
204 The time-zone as hour offset from GMT.
205 Required to emit RFC\ 822-conformant dates
206 (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)
207 .TP
208 .B %Z
209 The time zone or name or abbreviation.
210 .TP
211 .B %+
212 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
213 .\" their man pages)
214 The date and time in
215 .BR date (1)
216 format. (TZ)
217 (Not supported in glibc2.)
218 .TP
219 .B %%
220 A literal `%' character.
221 .PP
222 Some conversion specifications can be modified by preceding the
223 conversion specifier character by the E or O
224 .I modifier
225 to indicate that an alternative format should be used.
226 If the alternative format or specification does not exist for
227 the current locale, the behaviour will be as if the unmodified
228 conversion specification were used. (SU)
229 The Single Unix Specification mentions %Ec, %EC, %Ex, %EX,
230 %Ey, %EY, %Od, %Oe, %OH, %OI, %Om, %OM, %OS, %Ou, %OU, %OV,
231 %Ow, %OW, %Oy, where the effect of the O modifier is to use
232 alternative numeric symbols (say, roman numerals), and that of the
233 E modifier is to use a locale-dependent alternative representation.
234 .PP
235 The broken-down time structure \fItm\fP is defined in \fI<time.h>\fP.
236 See also
237 .BR ctime (3).
238 .SH "RETURN VALUE"
239 The \fBstrftime\fP() function returns the number of characters placed
240 in the array \fIs\fP, not including the terminating null byte,
241 provided the string, including the terminating null byte, fits.
242 Otherwise, it returns 0, and the contents of the array is undefined.
243 (Thus at least since libc 4.4.4; very old versions of libc,
244 such as libc 4.4.1, would return \fImax\fP if the array was too small.)
245 .LP
246 Note that the return value 0 does not necessarily indicate an error;
247 for example, in many locales %p yields an empty string.
248 .SH ENVIRONMENT
249 The environment variables TZ and LC_TIME are used.
250 .SH "CONFORMING TO"
251 SVr4, C89, C99.
252 There are strict inclusions between the set of conversions
253 given in ANSI C (unmarked), those given in the Single Unix Specification
254 (marked SU), those given in Olson's timezone package (marked TZ),
255 and those given in glibc (marked GNU), except that %+ is not supported
256 in glibc2. On the other hand glibc2 has several more extensions.
257 POSIX.1 only refers to ANSI C; POSIX.2 describes under
258 .BR date (1)
259 several extensions that could apply to
260 .BR strftime ()
261 as well.
262 The %F conversion is in C99 and POSIX.1-2001.
263
264 In SUSv2, the %S specified allowed a range of 00 to 61,
265 to allow for the theoretical possibility of a minute that
266 included a double leap second
267 (there never has been such a minute).
268 .SH GLIBC NOTES
269 Glibc provides some extensions for conversion specifications.
270 (These extensions are not specified in POSIX.1-2001, but a few other
271 systems provide similar features.)
272 .\" HP-UX and Tru64 also have features like this.
273 Between the % character and the conversion specifier character,
274 an optional
275 .I flag
276 and field
277 .I width
278 may be specified.
279 (These precede the E or O modifiers, if present.)
280
281 The following flag characters are permitted:
282 .TP
283 .B _
284 (underscore)
285 Pad a numeric result string with spaces.
286 .TP
287 .B \-
288 (dash)
289 Do not pad a numeric result string.
290 .TP
291 .B 0
292 Pad a numeric result string with zeros even if the conversion
293 specifier character uses space-padding by default.
294 .TP
295 .B ^
296 Convert alphabetic characters in result string to upper case.
297 .TP
298 .B #
299 Swap the case of the result string.
300 (This flag only works with certain conversion specifier characters,
301 and of these, it is only really useful with %Z).
302 .PP
303 An optional decimal width specifier may follow the (possibly absent) flag.
304 If the natural size of the field is smaller than this width,
305 then the result string is padded (on the left) to the specified width.
306 .SH BUGS
307 Some buggy versions of gcc complain about the use of %c:
308 .IR "warning: `%c' yields only last 2 digits of year in some locales" .
309 Of course programmers are encouraged to use %c, it gives the preferred
310 date and time representation. One meets all kinds of strange obfuscations
311 to circumvent this gcc problem. A relatively clean one is to add an
312 intermediate function
313 .in +0.5i
314 .nf
315
316 size_t
317 my_strftime(char *s, size_t max, const char *fmt,
318 const struct tm *tm)
319 {
320 return strftime(s, max, fmt, tm);
321 }
322 .fi
323 .in -0.5i
324 .SH EXAMPLE
325 The program below can be used to experiment with
326 .BR strftime ().
327 .nf
328
329 #include <time.h>
330 #include <stdio.h>
331 #include <stdlib.h>
332
333 int
334 main(int argc, char *argv[])
335 {
336 char outstr[200];
337 time_t t;
338 struct tm *tmp;
339
340 t = time(NULL);
341 tmp = localtime(&t);
342 if (tmp == NULL) {
343 perror("localtime");
344 exit(EXIT_FAILURE);
345 }
346
347 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
348 fprintf(stderr, "strftime returned 0");
349 exit(EXIT_FAILURE);
350 }
351
352 printf("Result string is \\"%s\\"\\n", outstr);
353 exit(EXIT_SUCCESS);
354 } /* main */
355 .fi
356 .PP
357 Some examples of the result string produced by the glibc implementation of
358 .BR strftime ()
359 are as follows:
360 .nf
361
362 $ ./a.out "%m"
363 Result string is "11"
364 $ ./a.out "%5m"
365 Result string is "00011"
366 $ ./a.out "%_5m"
367 Result string is " 11"
368 .fi
369 .SH "SEE ALSO"
370 .BR date (1),
371 .BR time (2),
372 .BR ctime (3),
373 .BR setlocale (3),
374 .BR sprintf (3),
375 .BR strptime (3)