]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strftime.3
timerfd_create.2, ctime.3, frexp.3, strftime.3, strtok.3, tty_ioctl.4, iso_8859-7...
[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 2008-10-29 "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
47 .BR strftime ()
48 function formats the broken-down time \fItm\fP
49 according to the format specification \fIformat\fP and places the
50 result in the character array \fIs\fP of size \fImax\fP.
51 .\" FIXME POSIX says: Local timezone information is used as though
52 .\" strftime() called tzset(). But this doesn't appear to be the case
53 .PP
54 Ordinary characters placed in the format string are copied to \fIs\fP
55 without conversion.
56 .I "Conversion specifications"
57 are introduced by a \(aq%\(aq
58 character, and terminated by a
59 .IR "conversion specifier character" ,
60 and are replaced in \fIs\fP as follows:
61 .TP
62 .B %a
63 The abbreviated weekday name according to the current locale.
64 .TP
65 .B %A
66 The full weekday name according to the current locale.
67 .TP
68 .B %b
69 The abbreviated month name according to the current locale.
70 .TP
71 .B %B
72 The full month name according to the current locale.
73 .TP
74 .B %c
75 The preferred date and time representation for the current locale.
76 .TP
77 .B %C
78 The century number (year/100) as a 2-digit integer. (SU)
79 .TP
80 .B %d
81 The day of the month as a decimal number (range 01 to 31).
82 .TP
83 .B %D
84 Equivalent to
85 .BR %m/%d/%y .
86 (Yecch \(em for Americans only.
87 Americans should note that in other countries
88 .B %d/%m/%y
89 is rather common.
90 This means that in international context this format is
91 ambiguous and should not be used.) (SU)
92 .TP
93 .B %e
94 Like
95 .BR %d ,
96 the day of the month as a decimal number, but a leading
97 zero is replaced by a space. (SU)
98 .TP
99 .B %E
100 Modifier: use alternative format, see below. (SU)
101 .TP
102 .B %F
103 Equivalent to
104 .B %Y-%m-%d
105 (the ISO 8601 date format). (C99)
106 .TP
107 .B %G
108 The ISO 8601 year with century as a decimal number.
109 The 4-digit year corresponding to the ISO week number (see
110 .BR %V ).
111 This has the same format and value as
112 .BR %y ,
113 except that if the
114 ISO week number belongs to the previous or next year,
115 that year is used instead. (TZ)
116 .TP
117 .B %g
118 Like
119 .BR %G ,
120 but without century, that is, with a 2-digit year (00-99). (TZ)
121 .TP
122 .B %h
123 Equivalent to
124 .BR %b .
125 (SU)
126 .TP
127 .B %H
128 The hour as a decimal number using a 24-hour clock (range 00 to 23).
129 .TP
130 .B %I
131 The hour as a decimal number using a 12-hour clock (range 01 to 12).
132 .TP
133 .B %j
134 The day of the year as a decimal number (range 001 to 366).
135 .TP
136 .B %k
137 The hour (24-hour clock) as a decimal number (range 0 to 23);
138 single digits are preceded by a blank.
139 (See also
140 .BR %H .)
141 (TZ)
142 .TP
143 .B %l
144 The hour (12-hour clock) as a decimal number (range 1 to 12);
145 single digits are preceded by a blank.
146 (See also
147 .BR %I .)
148 (TZ)
149 .TP
150 .B %m
151 The month as a decimal number (range 01 to 12).
152 .TP
153 .B %M
154 The minute as a decimal number (range 00 to 59).
155 .TP
156 .B %n
157 A newline character. (SU)
158 .TP
159 .B %O
160 Modifier: use alternative format, see below. (SU)
161 .TP
162 .B %p
163 Either "AM" or "PM" according to the given time value, or the
164 corresponding strings for the current locale.
165 Noon is treated as "PM" and midnight as "AM".
166 .TP
167 .B %P
168 Like
169 .B %p
170 but in lowercase: "am" or "pm" or a corresponding
171 string for the current locale. (GNU)
172 .TP
173 .B %r
174 The time in a.m. or p.m. notation.
175 In the POSIX locale this is equivalent to
176 .BR "%I:%M:%S %p" .
177 (SU)
178 .TP
179 .B %R
180 The time in 24-hour notation (\fB%H:%M\fP). (SU)
181 For a version including the seconds, see
182 .B %T
183 below.
184 .TP
185 .B %s
186 The number of seconds since the Epoch, that is, since 1970-01-01
187 00:00:00 UTC. (TZ)
188 .TP
189 .B %S
190 The second as a decimal number (range 00 to 60).
191 (The range is up to 60 to allow for occasional leap seconds.)
192 .TP
193 .B %t
194 A tab character. (SU)
195 .TP
196 .B %T
197 The time in 24-hour notation (\fB%H:%M:%S\fP). (SU)
198 .TP
199 .B %u
200 The day of the week as a decimal, range 1 to 7, Monday being 1.
201 See also
202 .BR %w .
203 (SU)
204 .TP
205 .B %U
206 The week number of the current year as a decimal number,
207 range 00 to 53, starting with the first Sunday as the first day
208 of week 01.
209 See also
210 .B %V
211 and
212 .BR %W .
213 .TP
214 .B %V
215 The ISO 8601:1988 week number of the current year as a decimal number,
216 range 01 to 53, where week 1 is the first week that has at least
217 4 days in the current year, and with Monday as the first day of
218 the week.
219 See also
220 .B %U
221 and
222 .BR %W .
223 (SU)
224 .TP
225 .B %w
226 The day of the week as a decimal, range 0 to 6, Sunday being 0.
227 See also
228 .BR %u .
229 .TP
230 .B %W
231 The week number of the current year as a decimal number,
232 range 00 to 53, starting with the first Monday as the first day of week 01.
233 .TP
234 .B %x
235 The preferred date representation for the current locale without the time.
236 .TP
237 .B %X
238 The preferred time representation for the current locale without the date.
239 .TP
240 .B %y
241 The year as a decimal number without a century (range 00 to 99).
242 .TP
243 .B %Y
244 The year as a decimal number including the century.
245 .TP
246 .B %z
247 The time-zone as hour offset from GMT.
248 Required to emit RFC\ 822-conformant dates
249 (using "%a,\ %d\ %b\ %Y\ %H:%M:%S\ %z"). (GNU)
250 .TP
251 .B %Z
252 The timezone or name or abbreviation.
253 .TP
254 .B %+
255 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
256 .\" their man pages)
257 The date and time in
258 .BR date (1)
259 format. (TZ)
260 (Not supported in glibc2.)
261 .TP
262 .B %%
263 A literal \(aq%\(aq character.
264 .PP
265 Some conversion specifications can be modified by preceding the
266 conversion specifier character by the
267 .B E
268 or
269 .B O
270 .I modifier
271 to indicate that an alternative format should be used.
272 If the alternative format or specification does not exist for
273 the current locale, the behavior will be as if the unmodified
274 conversion specification were used. (SU)
275 The Single Unix Specification mentions
276 .BR %Ec ,
277 .BR %EC ,
278 .BR %Ex ,
279 .BR %EX ,
280 .BR %Ey ,
281 .BR %EY ,
282 .BR %Od ,
283 .BR %Oe ,
284 .BR %OH ,
285 .BR %OI ,
286 .BR %Om ,
287 .BR %OM ,
288 .BR %OS ,
289 .BR %Ou ,
290 .BR %OU ,
291 .BR %OV ,
292 .BR %Ow ,
293 .BR %OW ,
294 .BR %Oy ,
295 where the effect of the
296 .B O
297 modifier is to use
298 alternative numeric symbols (say, roman numerals), and that of the
299 E modifier is to use a locale-dependent alternative representation.
300 .PP
301 The broken-down time structure \fItm\fP is defined in \fI<time.h>\fP.
302 See also
303 .BR ctime (3).
304 .SH "RETURN VALUE"
305 The
306 .BR strftime ()
307 function returns the number of characters placed
308 in the array \fIs\fP, not including the terminating null byte,
309 provided the string, including the terminating null byte, fits.
310 Otherwise, it returns 0, and the contents of the array is undefined.
311 (This behavior applies since at least libc 4.4.4;
312 very old versions of libc, such as libc 4.4.1,
313 would return \fImax\fP if the array was too small.)
314 .LP
315 Note that the return value 0 does not necessarily indicate an error;
316 for example, in many locales
317 .B %p
318 yields an empty string.
319 .SH ENVIRONMENT
320 The environment variables
321 .B TZ
322 and
323 .B LC_TIME
324 are used.
325 .SH "CONFORMING TO"
326 SVr4, C89, C99.
327 There are strict inclusions between the set of conversions
328 given in ANSI C (unmarked), those given in the Single Unix Specification
329 (marked SU), those given in Olson's timezone package (marked TZ),
330 and those given in glibc (marked GNU), except that
331 .B %+
332 is not supported in glibc2.
333 On the other hand glibc2 has several more extensions.
334 POSIX.1 only refers to ANSI C; POSIX.2 describes under
335 .BR date (1)
336 several extensions that could apply to
337 .BR strftime ()
338 as well.
339 The
340 .B %F
341 conversion is in C99 and POSIX.1-2001.
342
343 In SUSv2, the
344 .B %S
345 specifier allowed a range of 00 to 61,
346 to allow for the theoretical possibility of a minute that
347 included a double leap second
348 (there never has been such a minute).
349 .SH NOTES
350 .SS Glibc Notes
351 Glibc provides some extensions for conversion specifications.
352 (These extensions are not specified in POSIX.1-2001, but a few other
353 systems provide similar features.)
354 .\" HP-UX and Tru64 also have features like this.
355 Between the \(aq%\(aq character and the conversion specifier character,
356 an optional
357 .I flag
358 and field
359 .I width
360 may be specified.
361 (These precede the
362 .B E
363 or
364 .B O
365 modifiers, if present.)
366
367 The following flag characters are permitted:
368 .TP
369 .B _
370 (underscore)
371 Pad a numeric result string with spaces.
372 .TP
373 .B \-
374 (dash)
375 Do not pad a numeric result string.
376 .TP
377 .B 0
378 Pad a numeric result string with zeros even if the conversion
379 specifier character uses space-padding by default.
380 .TP
381 .B ^
382 Convert alphabetic characters in result string to upper case.
383 .TP
384 .B #
385 Swap the case of the result string.
386 (This flag only works with certain conversion specifier characters,
387 and of these, it is only really useful with
388 .BR %Z .)
389 .PP
390 An optional decimal width specifier may follow the (possibly absent) flag.
391 If the natural size of the field is smaller than this width,
392 then the result string is padded (on the left) to the specified width.
393 .SH BUGS
394 Some buggy versions of
395 .BR gcc (1)
396 complain about the use of
397 .BR %c :
398 .IR "warning: `%c' yields only last 2 digits of year in some locales" .
399 Of course programmers are encouraged to use
400 .BR %c ,
401 it gives the preferred date and time representation.
402 One meets all kinds of strange obfuscations
403 to circumvent this
404 .BR gcc (1)
405 problem.
406 A relatively clean one is to add an
407 intermediate function
408 .in +4n
409 .nf
410
411 size_t
412 my_strftime(char *s, size_t max, const char *fmt,
413 const struct tm *tm)
414 {
415 return strftime(s, max, fmt, tm);
416 }
417 .fi
418 .in
419
420 Nowadays,
421 .BR gcc (1)
422 provides the \fI\-Wno\-format\-y2k\fP option to prevent the warning,
423 so that the above workaround is no longer required.
424 .SH EXAMPLE
425 The program below can be used to experiment with
426 .BR strftime ().
427 .PP
428 Some examples of the result string produced by the glibc implementation of
429 .BR strftime ()
430 are as follows:
431 .in +4n
432 .nf
433
434 .RB "$" " ./a.out \(aq%m\(aq"
435 Result string is "11"
436 .RB "$" " ./a.out \(aq%5m\(aq"
437 Result string is "00011"
438 .RB "$" " ./a.out \(aq%_5m\(aq"
439 Result string is " 11"
440 .fi
441 .in
442 .SS Program source
443 .R " "
444 .nf
445 #include <time.h>
446 #include <stdio.h>
447 #include <stdlib.h>
448
449 int
450 main(int argc, char *argv[])
451 {
452 char outstr[200];
453 time_t t;
454 struct tm *tmp;
455
456 t = time(NULL);
457 tmp = localtime(&t);
458 if (tmp == NULL) {
459 perror("localtime");
460 exit(EXIT_FAILURE);
461 }
462
463 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
464 fprintf(stderr, "strftime returned 0");
465 exit(EXIT_FAILURE);
466 }
467
468 printf("Result string is \\"%s\\"\\n", outstr);
469 exit(EXIT_SUCCESS);
470 } /* main */
471 .fi
472 .SH "SEE ALSO"
473 .BR date (1),
474 .BR time (2),
475 .BR ctime (3),
476 .BR setlocale (3),
477 .BR sprintf (3),
478 .BR strptime (3)