]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strftime.3
getpt.3: tfix
[thirdparty/man-pages.git] / man3 / strftime.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" 386BSD man pages
29 .\" GNU texinfo documentation on glibc date/time functions.
30 .\" Modified Sat Jul 24 18:03:44 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Applied fix by Wolfgang Franke, aeb, 961011
32 .\" Corrected return value, aeb, 970307
33 .\" Added Single UNIX Spec conversions and %z, aeb/esr, 990329.
34 .\" 2005-11-22 mtk, added Glibc Notes covering optional 'flag' and
35 .\" 'width' components of conversion specifications.
36 .\"
37 .TH STRFTIME 3 2019-03-06 "GNU" "Linux Programmer's Manual"
38 .SH NAME
39 strftime \- format date and time
40 .SH SYNOPSIS
41 .nf
42 .B #include <time.h>
43 .PP
44 .BI "size_t strftime(char *" s ", size_t " max ", const char *" format ,
45 .BI " const struct tm *" tm );
46 .fi
47 .SH DESCRIPTION
48 The
49 .BR strftime ()
50 function formats the broken-down time
51 .I tm
52 according to the format specification
53 .I format
54 and places the
55 result in the character array
56 .I s
57 of size
58 .IR max .
59 The broken-down time structure
60 .I tm
61 is defined in
62 .IR <time.h> .
63 See also
64 .BR ctime (3).
65 .\" FIXME . POSIX says: Local timezone information is used as though
66 .\" strftime() called tzset(). But this doesn't appear to be the case
67 .PP
68 The format specification is a null-terminated string and may contain
69 special character sequences called
70 .IR "conversion specifications",
71 each of which is introduced by a \(aq%\(aq character and terminated by
72 some other character known as a
73 .IR "conversion specifier character".
74 All other character sequences are
75 .IR "ordinary character sequences".
76 .PP
77 The characters of ordinary character sequences (including the null byte)
78 are copied verbatim from
79 .I format
80 to
81 .IR s .
82 However, the characters
83 of conversion specifications are replaced as shown in the list below.
84 In this list, the field(s) employed from the
85 .I tm
86 structure are also shown.
87 .TP
88 .B %a
89 The abbreviated name of the day of the week according to the current locale.
90 (Calculated from
91 .IR tm_wday .)
92 .TP
93 .B %A
94 The full name of the day of the week according to the current locale.
95 (Calculated from
96 .IR tm_wday .)
97 .TP
98 .B %b
99 The abbreviated month name according to the current locale.
100 (Calculated from
101 .IR tm_mon .)
102 .TP
103 .B %B
104 The full month name according to the current locale.
105 (Calculated from
106 .IR tm_mon .)
107 .TP
108 .B %c
109 The preferred date and time representation for the current locale.
110 .TP
111 .B %C
112 The century number (year/100) as a 2-digit integer. (SU)
113 (Calculated from
114 .IR tm_year .)
115 .TP
116 .B %d
117 The day of the month as a decimal number (range 01 to 31).
118 (Calculated from
119 .IR tm_mday .)
120 .TP
121 .B %D
122 Equivalent to
123 .BR %m/%d/%y .
124 (Yecch\(emfor Americans only.
125 Americans should note that in other countries
126 .B %d/%m/%y
127 is rather common.
128 This means that in international context this format is
129 ambiguous and should not be used.) (SU)
130 .TP
131 .B %e
132 Like
133 .BR %d ,
134 the day of the month as a decimal number, but a leading
135 zero is replaced by a space. (SU)
136 (Calculated from
137 .IR tm_mday .)
138 .TP
139 .B %E
140 Modifier: use alternative format, see below. (SU)
141 .TP
142 .B %F
143 Equivalent to
144 .B %Y-%m-%d
145 (the ISO\ 8601 date format). (C99)
146 .TP
147 .B %G
148 The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
149 The 4-digit year corresponding to the ISO week number (see
150 .BR %V ).
151 This has the same format and value as
152 .BR %Y ,
153 except that if the ISO week number belongs to the previous or next year,
154 that year is used instead. (TZ)
155 (Calculated from
156 .IR tm_year ,
157 .IR tm_yday ,
158 and
159 .IR tm_wday .)
160 .TP
161 .B %g
162 Like
163 .BR %G ,
164 but without century, that is, with a 2-digit year (00\(en99). (TZ)
165 (Calculated from
166 .IR tm_year ,
167 .IR tm_yday ,
168 and
169 .IR tm_wday .)
170 .TP
171 .B %h
172 Equivalent to
173 .BR %b .
174 (SU)
175 .TP
176 .B %H
177 The hour as a decimal number using a 24-hour clock (range 00 to 23).
178 (Calculated from
179 .IR tm_hour .)
180 .TP
181 .B %I
182 The hour as a decimal number using a 12-hour clock (range 01 to 12).
183 (Calculated from
184 .IR tm_hour .)
185 .TP
186 .B %j
187 The day of the year as a decimal number (range 001 to 366).
188 (Calculated from
189 .IR tm_yday .)
190 .TP
191 .B %k
192 The hour (24-hour clock) as a decimal number (range 0 to 23);
193 single digits are preceded by a blank.
194 (See also
195 .BR %H .)
196 (Calculated from
197 .IR tm_hour .)
198 (TZ)
199 .TP
200 .B %l
201 The hour (12-hour clock) as a decimal number (range 1 to 12);
202 single digits are preceded by a blank.
203 (See also
204 .BR %I .)
205 (Calculated from
206 .IR tm_hour .)
207 (TZ)
208 .TP
209 .B %m
210 The month as a decimal number (range 01 to 12).
211 (Calculated from
212 .IR tm_mon .)
213 .TP
214 .B %M
215 The minute as a decimal number (range 00 to 59).
216 (Calculated from
217 .IR tm_min .)
218 .TP
219 .B %n
220 A newline character. (SU)
221 .TP
222 .B %O
223 Modifier: use alternative format, see below. (SU)
224 .TP
225 .B %p
226 Either "AM" or "PM" according to the given time value, or the
227 corresponding strings for the current locale.
228 Noon is treated as "PM" and midnight as "AM".
229 (Calculated from
230 .IR tm_hour .)
231 .TP
232 .B %P
233 Like
234 .B %p
235 but in lowercase: "am" or "pm" or a corresponding
236 string for the current locale.
237 (Calculated from
238 .IR tm_hour .)
239 (GNU)
240 .TP
241 .B %r
242 The time in a.m. or p.m. notation.
243 In the POSIX locale this is equivalent to
244 .BR "%I:%M:%S %p" .
245 (SU)
246 .TP
247 .B %R
248 The time in 24-hour notation
249 .RB ( %H:%M ).
250 (SU)
251 For a version including the seconds, see
252 .B %T
253 below.
254 .TP
255 .B %s
256 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
257 (Calculated from
258 .IR mktime(tm) .)
259 .TP
260 .B %S
261 The second as a decimal number (range 00 to 60).
262 (The range is up to 60 to allow for occasional leap seconds.)
263 (Calculated from
264 .IR tm_sec .)
265 .TP
266 .B %t
267 A tab character. (SU)
268 .TP
269 .B %T
270 The time in 24-hour notation
271 .RB ( %H:%M:%S ).
272 (SU)
273 .TP
274 .B %u
275 The day of the week as a decimal, range 1 to 7, Monday being 1.
276 See also
277 .BR %w .
278 (Calculated from
279 .IR tm_wday .)
280 (SU)
281 .TP
282 .B %U
283 The week number of the current year as a decimal number,
284 range 00 to 53, starting with the first Sunday as the first day
285 of week 01.
286 See also
287 .B %V
288 and
289 .BR %W .
290 (Calculated from
291 .IR tm_yday
292 and
293 .IR tm_wday .)
294 .TP
295 .B %V
296 The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
297 range 01 to 53, where week 1 is the first week that has at least
298 4 days in the new year.
299 See also
300 .B %U
301 and
302 .BR %W .
303 (Calculated from
304 .IR tm_year ,
305 .IR tm_yday ,
306 and
307 .IR tm_wday .)
308 (SU)
309 .TP
310 .B %w
311 The day of the week as a decimal, range 0 to 6, Sunday being 0.
312 See also
313 .BR %u .
314 (Calculated from
315 .IR tm_wday .)
316 .TP
317 .B %W
318 The week number of the current year as a decimal number,
319 range 00 to 53, starting with the first Monday as the first day of week 01.
320 (Calculated from
321 .IR tm_yday
322 and
323 .IR tm_wday .)
324 .TP
325 .B %x
326 The preferred date representation for the current locale without the time.
327 .TP
328 .B %X
329 The preferred time representation for the current locale without the date.
330 .TP
331 .B %y
332 The year as a decimal number without a century (range 00 to 99).
333 (Calculated from
334 .IR tm_year )
335 .TP
336 .B %Y
337 The year as a decimal number including the century.
338 (Calculated from
339 .IR tm_year )
340 .TP
341 .B %z
342 The
343 .I +hhmm
344 or
345 .I -hhmm
346 numeric timezone (that is, the hour and minute offset from UTC). (SU)
347 .TP
348 .B %Z
349 The timezone name or abbreviation.
350 .TP
351 .B %+
352 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
353 .\" their man pages)
354 The date and time in
355 .BR date (1)
356 format. (TZ)
357 (Not supported in glibc2.)
358 .TP
359 .B %%
360 A literal \(aq%\(aq character.
361 .PP
362 Some conversion specifications can be modified by preceding the
363 conversion specifier character by the
364 .B E
365 or
366 .B O
367 .I modifier
368 to indicate that an alternative format should be used.
369 If the alternative format or specification does not exist for
370 the current locale, the behavior will be as if the unmodified
371 conversion specification were used. (SU)
372 The Single UNIX Specification mentions
373 .BR %Ec ,
374 .BR %EC ,
375 .BR %Ex ,
376 .BR %EX ,
377 .BR %Ey ,
378 .BR %EY ,
379 .BR %Od ,
380 .BR %Oe ,
381 .BR %OH ,
382 .BR %OI ,
383 .BR %Om ,
384 .BR %OM ,
385 .BR %OS ,
386 .BR %Ou ,
387 .BR %OU ,
388 .BR %OV ,
389 .BR %Ow ,
390 .BR %OW ,
391 .BR %Oy ,
392 where the effect of the
393 .B O
394 modifier is to use
395 alternative numeric symbols (say, roman numerals), and that of the
396 E modifier is to use a locale-dependent alternative representation.
397 .SH RETURN VALUE
398 Provided that the result string,
399 including the terminating null byte, does not exceed
400 .I max
401 bytes,
402 .BR strftime ()
403 returns the number of bytes (excluding the terminating null byte)
404 placed in the array
405 .IR s .
406 If the length of the result string (including the terminating null byte)
407 would exceed
408 .I max
409 bytes, then
410 .BR strftime ()
411 returns 0, and the contents of the array are undefined.
412 .\" (This behavior applies since at least libc 4.4.4;
413 .\" very old versions of libc, such as libc 4.4.1,
414 .\" would return
415 .\" .I max
416 .\" if the array was too small.)
417 .PP
418 Note that the return value 0 does not necessarily indicate an error.
419 For example, in many locales
420 .B %p
421 yields an empty string.
422 An empty
423 .I format
424 string will likewise yield an empty string.
425 .SH ENVIRONMENT
426 The environment variables
427 .B TZ
428 and
429 .B LC_TIME
430 are used.
431 .SH ATTRIBUTES
432 For an explanation of the terms used in this section, see
433 .BR attributes (7).
434 .TS
435 allbox;
436 lb lb lb
437 l l l.
438 Interface Attribute Value
439 T{
440 .BR strftime ()
441 T} Thread safety MT-Safe env locale
442 .TE
443 .SH CONFORMING TO
444 SVr4, C89, C99.
445 .\" FIXME strftime() is in POSIX.1-2001 and POSIX.1-2008, but the details
446 .\" in the standards changed across versions. Investigate and
447 .\" write up.
448 There are strict inclusions between the set of conversions
449 given in ANSI C (unmarked), those given in the Single UNIX Specification
450 (marked SU), those given in Olson's timezone package (marked TZ),
451 and those given in glibc (marked GNU), except that
452 .B %+
453 is not supported in glibc2.
454 On the other hand glibc2 has several more extensions.
455 POSIX.1 only refers to ANSI C; POSIX.2 describes under
456 .BR date (1)
457 several extensions that could apply to
458 .BR strftime ()
459 as well.
460 The
461 .B %F
462 conversion is in C99 and POSIX.1-2001.
463 .PP
464 In SUSv2, the
465 .B %S
466 specifier allowed a range of 00 to 61,
467 to allow for the theoretical possibility of a minute that
468 included a double leap second
469 (there never has been such a minute).
470 .SH NOTES
471 .SS ISO 8601 week dates
472 .BR %G ,
473 .BR %g ,
474 and
475 .BR %V
476 yield values calculated from the week-based year defined by the
477 ISO\ 8601 standard.
478 In this system, weeks start on a Monday, and are numbered from 01,
479 for the first week, up to 52 or 53, for the last week.
480 Week 1 is the first week where four or more days fall within the
481 new year (or, synonymously, week 01 is:
482 the first week of the year that contains a Thursday;
483 or, the week that has 4 January in it).
484 When three of fewer days of the first calendar week of the new year fall
485 within that year,
486 then the ISO 8601 week-based system counts those days as part of week 53
487 of the preceding year.
488 For example, 1 January 2010 is a Friday,
489 meaning that just three days of that calendar week fall in 2010.
490 Thus, the ISO\ 8601 week-based system considers these days to be part of
491 week 53
492 .RB ( %V )
493 of the year 2009
494 .RB ( %G );
495 week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
496 .SS Glibc notes
497 Glibc provides some extensions for conversion specifications.
498 (These extensions are not specified in POSIX.1-2001, but a few other
499 systems provide similar features.)
500 .\" HP-UX and Tru64 also have features like this.
501 Between the \(aq%\(aq character and the conversion specifier character,
502 an optional
503 .I flag
504 and field
505 .I width
506 may be specified.
507 (These precede the
508 .B E
509 or
510 .B O
511 modifiers, if present.)
512 .PP
513 The following flag characters are permitted:
514 .TP
515 .B _
516 (underscore)
517 Pad a numeric result string with spaces.
518 .TP
519 .B \-
520 (dash)
521 Do not pad a numeric result string.
522 .TP
523 .B 0
524 Pad a numeric result string with zeros even if the conversion
525 specifier character uses space-padding by default.
526 .TP
527 .B ^
528 Convert alphabetic characters in result string to uppercase.
529 .TP
530 .B #
531 Swap the case of the result string.
532 (This flag works only with certain conversion specifier characters,
533 and of these, it is only really useful with
534 .BR %Z .)
535 .PP
536 An optional decimal width specifier may follow the (possibly absent) flag.
537 If the natural size of the field is smaller than this width,
538 then the result string is padded (on the left) to the specified width.
539 .SH BUGS
540 If the output string would exceed
541 .I max
542 bytes,
543 .I errno
544 is
545 .I not
546 set.
547 This makes it impossible to distinguish this error case from cases where the
548 .I format
549 string legitimately produces a zero-length output string.
550 POSIX.1-2001
551 does
552 .I not
553 specify any
554 .I errno
555 settings for
556 .BR strftime ().
557 .PP
558 Some buggy versions of
559 .BR gcc (1)
560 complain about the use of
561 .BR %c :
562 .IR "warning: `%c' yields only last 2 digits of year in some locales" .
563 Of course programmers are encouraged to use
564 .BR %c ,
565 it gives the preferred date and time representation.
566 One meets all kinds of strange obfuscations
567 to circumvent this
568 .BR gcc (1)
569 problem.
570 A relatively clean one is to add an
571 intermediate function
572 .PP
573 .in +4n
574 .EX
575 size_t
576 my_strftime(char *s, size_t max, const char *fmt,
577 const struct tm *tm)
578 {
579 return strftime(s, max, fmt, tm);
580 }
581 .EE
582 .in
583 .PP
584 Nowadays,
585 .BR gcc (1)
586 provides the
587 .IR \-Wno\-format\-y2k
588 option to prevent the warning,
589 so that the above workaround is no longer required.
590 .SH EXAMPLE
591 .BR "RFC\ 2822-compliant date format"
592 (with an English locale for %a and %b)
593 .PP
594 .in +2n
595 "%a,\ %d\ %b\ %Y\ %T\ %z"
596 .PP
597 .BR "RFC\ 822-compliant date format"
598 (with an English locale for %a and %b)
599 .PP
600 .in +2n
601 "%a,\ %d\ %b\ %y\ %T\ %z"
602 .SS Example program
603 The program below can be used to experiment with
604 .BR strftime ().
605 .PP
606 Some examples of the result string produced by the glibc implementation of
607 .BR strftime ()
608 are as follows:
609 .PP
610 .in +4n
611 .EX
612 .RB "$" " ./a.out \(aq%m\(aq"
613 Result string is "11"
614 .RB "$" " ./a.out \(aq%5m\(aq"
615 Result string is "00011"
616 .RB "$" " ./a.out \(aq%_5m\(aq"
617 Result string is " 11"
618 .EE
619 .in
620 .SS Program source
621 \&
622 .EX
623 #include <time.h>
624 #include <stdio.h>
625 #include <stdlib.h>
626
627 int
628 main(int argc, char *argv[])
629 {
630 char outstr[200];
631 time_t t;
632 struct tm *tmp;
633
634 t = time(NULL);
635 tmp = localtime(&t);
636 if (tmp == NULL) {
637 perror("localtime");
638 exit(EXIT_FAILURE);
639 }
640
641 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
642 fprintf(stderr, "strftime returned 0");
643 exit(EXIT_FAILURE);
644 }
645
646 printf("Result string is \e"%s\e"\en", outstr);
647 exit(EXIT_SUCCESS);
648 }
649 .EE
650 .SH SEE ALSO
651 .BR date (1),
652 .BR time (2),
653 .BR ctime (3),
654 .BR setlocale (3),
655 .BR sprintf (3),
656 .BR strptime (3)