1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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.
37 .TH STRFTIME 3 2021-03-22 "GNU" "Linux Programmer's Manual"
39 strftime \- format date and time
44 .BI "size_t strftime(char *restrict " s ", size_t " max ,
45 .BI " const char *restrict " format ,
46 .BI " const struct tm *restrict " tm );
51 function formats the broken-down time
53 according to the format specification
56 result in the character array
60 The broken-down time structure
66 .\" FIXME . POSIX says: Local timezone information is used as though
67 .\" strftime() called tzset(). But this doesn't appear to be the case
69 The format specification is a null-terminated string and may contain
70 special character sequences called
71 .IR "conversion specifications",
72 each of which is introduced by a \(aq%\(aq character and terminated by
73 some other character known as a
74 .IR "conversion specifier character".
75 All other character sequences are
76 .IR "ordinary character sequences".
78 The characters of ordinary character sequences (including the null byte)
79 are copied verbatim from
83 However, the characters
84 of conversion specifications are replaced as shown in the list below.
85 In this list, the field(s) employed from the
87 structure are also shown.
90 The abbreviated name of the day of the week according to the current locale.
93 (The specific names used in the current locale can be obtained by calling
96 .BR ABDAY_ { 1 \(en 7 }
100 The full name of the day of the week according to the current locale.
103 (The specific names used in the current locale can be obtained by calling
106 .BR DAY_ { 1 \(en 7 }
110 The abbreviated month name according to the current locale.
113 (The specific names used in the current locale can be obtained by calling
116 .BR ABMON_ { 1 \(en 12 }
120 The full month name according to the current locale.
123 (The specific names used in the current locale can be obtained by calling
126 .BR MON_ { 1 \(en 12 }
130 The preferred date and time representation for the current locale.
131 (The specific format used in the current locale can be obtained by calling
135 as an argument for the
137 conversion specification, and with
141 conversion specification.)
142 (In the POSIX locale this is equivalent to
143 .BR "%a %b %e %H:%M:%S %Y" .)
146 The century number (year/100) as a 2-digit integer. (SU)
149 conversion specification corresponds to the name of the era.)
154 The day of the month as a decimal number (range 01 to 31).
161 (Yecch\(emfor Americans only.
162 Americans should note that in other countries
165 This means that in international context this format is
166 ambiguous and should not be used.) (SU)
171 the day of the month as a decimal number, but a leading
172 zero is replaced by a space. (SU)
177 Modifier: use alternative ("era-based") format, see below. (SU)
182 (the ISO\ 8601 date format). (C99)
185 The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
186 The 4-digit year corresponding to the ISO week number (see
188 This has the same format and value as
190 except that if the ISO week number belongs to the previous or next year,
191 that year is used instead. (TZ)
201 but without century, that is, with a 2-digit year (00\(en99). (TZ)
214 The hour as a decimal number using a 24-hour clock (range 00 to 23).
219 The hour as a decimal number using a 12-hour clock (range 01 to 12).
224 The day of the year as a decimal number (range 001 to 366).
229 The hour (24-hour clock) as a decimal number (range 0 to 23);
230 single digits are preceded by a blank.
238 The hour (12-hour clock) as a decimal number (range 1 to 12);
239 single digits are preceded by a blank.
247 The month as a decimal number (range 01 to 12).
252 The minute as a decimal number (range 00 to 59).
257 A newline character. (SU)
260 Modifier: use alternative numeric symbols, see below. (SU)
263 Either "AM" or "PM" according to the given time value, or the
264 corresponding strings for the current locale.
265 Noon is treated as "PM" and midnight as "AM".
268 (The specific string representations used for "AM" and "PM"
269 in the current locale can be obtained by calling
272 .BR AM_STR " and " PM_STR ,
278 but in lowercase: "am" or "pm" or a corresponding
279 string for the current locale.
285 The time in a.m. or p.m. notation.
287 (The specific format used in the current locale can be obtained by calling
292 (In the POSIX locale this is equivalent to
296 The time in 24-hour notation
299 For a version including the seconds, see
304 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
309 The second as a decimal number (range 00 to 60).
310 (The range is up to 60 to allow for occasional leap seconds.)
315 A tab character. (SU)
318 The time in 24-hour notation
323 The day of the week as a decimal, range 1 to 7, Monday being 1.
331 The week number of the current year as a decimal number,
332 range 00 to 53, starting with the first Sunday as the first day
344 The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
345 range 01 to 53, where week 1 is the first week that has at least
346 4 days in the new year.
359 The day of the week as a decimal, range 0 to 6, Sunday being 0.
366 The week number of the current year as a decimal number,
367 range 00 to 53, starting with the first Monday as the first day of week 01.
374 The preferred date representation for the current locale without the time.
375 (The specific format used in the current locale can be obtained by calling
379 as an argument for the
381 conversion specification, and with
385 conversion specification.)
386 (In the POSIX locale this is equivalent to
390 The preferred time representation for the current locale without the date.
391 (The specific format used in the current locale can be obtained by calling
395 as an argument for the
397 conversion specification, and with
401 conversion specification.)
402 (In the POSIX locale this is equivalent to
406 The year as a decimal number without a century (range 00 to 99).
409 conversion specification corresponds to the year since the beginning of the era
412 conversion specification.)
417 The year as a decimal number including the century.
420 conversion specification corresponds to the full alternative year representation.)
429 numeric timezone (that is, the hour and minute offset from UTC). (SU)
432 The timezone name or abbreviation.
435 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
440 (Not supported in glibc2.)
443 A literal \(aq%\(aq character.
445 Some conversion specifications can be modified by preceding the
446 conversion specifier character by the
451 to indicate that an alternative format should be used.
452 If the alternative format or specification does not exist for
453 the current locale, the behavior will be as if the unmodified
454 conversion specification were used. (SU)
455 The Single UNIX Specification mentions
475 where the effect of the
478 alternative numeric symbols (say, roman numerals), and that of the
480 modifier is to use a locale-dependent alternative representation.
481 The rules governing date representation with the
483 modifier can be obtained by supplying
487 One example of such alternative forms is the Japanese era calendar scheme in the
491 Provided that the result string,
492 including the terminating null byte, does not exceed
496 returns the number of bytes (excluding the terminating null byte)
499 If the length of the result string (including the terminating null byte)
504 returns 0, and the contents of the array are undefined.
505 .\" (This behavior applies since at least libc 4.4.4;
506 .\" very old versions of libc, such as libc 4.4.1,
509 .\" if the array was too small.)
511 Note that the return value 0 does not necessarily indicate an error.
512 For example, in many locales
514 yields an empty string.
517 string will likewise yield an empty string.
519 The environment variables
525 For an explanation of the terms used in this section, see
533 Interface Attribute Value
536 T} Thread safety MT-Safe env locale
543 .\" FIXME strftime() is in POSIX.1-2001 and POSIX.1-2008, but the details
544 .\" in the standards changed across versions. Investigate and
546 There are strict inclusions between the set of conversions
547 given in ANSI C (unmarked), those given in the Single UNIX Specification
548 (marked SU), those given in Olson's timezone package (marked TZ),
549 and those given in glibc (marked GNU), except that
551 is not supported in glibc2.
552 On the other hand glibc2 has several more extensions.
553 POSIX.1 only refers to ANSI C; POSIX.2 describes under
555 several extensions that could apply to
560 conversion is in C99 and POSIX.1-2001.
564 specifier allowed a range of 00 to 61,
565 to allow for the theoretical possibility of a minute that
566 included a double leap second
567 (there never has been such a minute).
569 .SS ISO 8601 week dates
574 yield values calculated from the week-based year defined by the
576 In this system, weeks start on a Monday, and are numbered from 01,
577 for the first week, up to 52 or 53, for the last week.
578 Week 1 is the first week where four or more days fall within the
579 new year (or, synonymously, week 01 is:
580 the first week of the year that contains a Thursday;
581 or, the week that has 4 January in it).
582 When three or fewer days of the first calendar week of the new year fall
584 then the ISO 8601 week-based system counts those days as part of week 52
585 or 53 of the preceding year.
586 For example, 1 January 2010 is a Friday,
587 meaning that just three days of that calendar week fall in 2010.
588 Thus, the ISO\ 8601 week-based system considers these days to be part of
593 week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
594 Similarly, the first two days of January 2011 are considered to be part
595 of week 52 of the year 2010.
597 Glibc provides some extensions for conversion specifications.
598 (These extensions are not specified in POSIX.1-2001, but a few other
599 systems provide similar features.)
600 .\" HP-UX and Tru64 also have features like this.
601 Between the \(aq%\(aq character and the conversion specifier character,
611 modifiers, if present.)
613 The following flag characters are permitted:
617 Pad a numeric result string with spaces.
621 Do not pad a numeric result string.
624 Pad a numeric result string with zeros even if the conversion
625 specifier character uses space-padding by default.
628 Convert alphabetic characters in result string to uppercase.
631 Swap the case of the result string.
632 (This flag works only with certain conversion specifier characters,
633 and of these, it is only really useful with
636 An optional decimal width specifier may follow the (possibly absent) flag.
637 If the natural size of the field is smaller than this width,
638 then the result string is padded (on the left) to the specified width.
640 If the output string would exceed
647 This makes it impossible to distinguish this error case from cases where the
649 string legitimately produces a zero-length output string.
658 Some buggy versions of
660 complain about the use of
662 .IR "warning: \`%c\(aq yields only last 2 digits of year in some locales" .
663 Of course programmers are encouraged to use
665 as it gives the preferred date and time representation.
666 One meets all kinds of strange obfuscations
670 A relatively clean one is to add an
671 intermediate function
676 my_strftime(char *s, size_t max, const char *fmt,
679 return strftime(s, max, fmt, tm);
687 .IR \-Wno\-format\-y2k
688 option to prevent the warning,
689 so that the above workaround is no longer required.
691 .BR "RFC\ 2822-compliant date format"
692 (with an English locale for %a and %b)
696 "%a,\ %d\ %b\ %Y\ %T\ %z"
700 .BR "RFC\ 822-compliant date format"
701 (with an English locale for %a and %b)
705 "%a,\ %d\ %b\ %y\ %T\ %z"
709 The program below can be used to experiment with
712 Some examples of the result string produced by the glibc implementation of
718 .RB "$" " ./a.out \(aq%m\(aq"
719 Result string is "11"
720 .RB "$" " ./a.out \(aq%5m\(aq"
721 Result string is "00011"
722 .RB "$" " ./a.out \(aq%_5m\(aq"
723 Result string is " 11"
734 main(int argc, char *argv[])
747 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
748 fprintf(stderr, "strftime returned 0");
752 printf("Result string is \e"%s\e"\en", outstr);