1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" GNU texinfo documentation on glibc date/time functions.
10 .\" Modified Sat Jul 24 18:03:44 1993 by Rik Faith (faith@cs.unc.edu)
11 .\" Applied fix by Wolfgang Franke, aeb, 961011
12 .\" Corrected return value, aeb, 970307
13 .\" Added Single UNIX Spec conversions and %z, aeb/esr, 990329.
14 .\" 2005-11-22 mtk, added Glibc Notes covering optional 'flag' and
15 .\" 'width' components of conversion specifications.
17 .TH STRFTIME 3 2021-03-22 "Linux man-pages (unreleased)"
19 strftime \- format date and time
22 .RI ( libc ", " \-lc )
27 .BI "size_t strftime(char *restrict " s ", size_t " max ,
28 .BI " const char *restrict " format ,
29 .BI " const struct tm *restrict " tm );
31 .BI "size_t strftime_l(char *restrict " s ", size_t " max ,
32 .BI " const char *restrict " format ,
33 .BI " const struct tm *restrict " tm ,
34 .BI " locale_t " locale );
39 function formats the broken-down time
41 according to the format specification
44 result in the character array
48 The broken-down time structure
54 .\" FIXME . POSIX says: Local timezone information is used as though
55 .\" strftime() called tzset(). But this doesn't appear to be the case
57 The format specification is a null-terminated string and may contain
58 special character sequences called
59 .IR "conversion specifications",
60 each of which is introduced by a \(aq%\(aq character and terminated by
61 some other character known as a
62 .IR "conversion specifier character".
63 All other character sequences are
64 .IR "ordinary character sequences".
66 The characters of ordinary character sequences (including the null byte)
67 are copied verbatim from
71 However, the characters
72 of conversion specifications are replaced as shown in the list below.
73 In this list, the field(s) employed from the
75 structure are also shown.
78 The abbreviated name of the day of the week according to the current locale.
81 (The specific names used in the current locale can be obtained by calling
84 .BR ABDAY_ { 1 \(en 7 }
88 The full name of the day of the week according to the current locale.
91 (The specific names used in the current locale can be obtained by calling
98 The abbreviated month name according to the current locale.
101 (The specific names used in the current locale can be obtained by calling
104 .BR ABMON_ { 1 \(en 12 }
108 The full month name according to the current locale.
111 (The specific names used in the current locale can be obtained by calling
114 .BR MON_ { 1 \(en 12 }
118 The preferred date and time representation for the current locale.
119 (The specific format used in the current locale can be obtained by calling
123 as an argument for the
125 conversion specification, and with
129 conversion specification.)
130 (In the POSIX locale this is equivalent to
131 .BR "%a %b %e %H:%M:%S %Y" .)
134 The century number (year/100) as a 2-digit integer. (SU)
137 conversion specification corresponds to the name of the era.)
142 The day of the month as a decimal number (range 01 to 31).
149 (Yecch\(emfor Americans only.
150 Americans should note that in other countries
153 This means that in international context this format is
154 ambiguous and should not be used.) (SU)
159 the day of the month as a decimal number, but a leading
160 zero is replaced by a space. (SU)
165 Modifier: use alternative ("era-based") format, see below. (SU)
170 (the ISO\ 8601 date format). (C99)
173 The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
174 The 4-digit year corresponding to the ISO week number (see
176 This has the same format and value as
178 except that if the ISO week number belongs to the previous or next year,
179 that year is used instead. (TZ)
189 but without century, that is, with a 2-digit year (00\(en99). (TZ)
202 The hour as a decimal number using a 24-hour clock (range 00 to 23).
207 The hour as a decimal number using a 12-hour clock (range 01 to 12).
212 The day of the year as a decimal number (range 001 to 366).
217 The hour (24-hour clock) as a decimal number (range 0 to 23);
218 single digits are preceded by a blank.
226 The hour (12-hour clock) as a decimal number (range 1 to 12);
227 single digits are preceded by a blank.
235 The month as a decimal number (range 01 to 12).
240 The minute as a decimal number (range 00 to 59).
245 A newline character. (SU)
248 Modifier: use alternative numeric symbols, see below. (SU)
251 Either "AM" or "PM" according to the given time value, or the
252 corresponding strings for the current locale.
253 Noon is treated as "PM" and midnight as "AM".
256 (The specific string representations used for "AM" and "PM"
257 in the current locale can be obtained by calling
260 .BR AM_STR " and " PM_STR ,
266 but in lowercase: "am" or "pm" or a corresponding
267 string for the current locale.
273 The time in a.m. or p.m. notation.
275 (The specific format used in the current locale can be obtained by calling
280 (In the POSIX locale this is equivalent to
284 The time in 24-hour notation
287 For a version including the seconds, see
292 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
297 The second as a decimal number (range 00 to 60).
298 (The range is up to 60 to allow for occasional leap seconds.)
303 A tab character. (SU)
306 The time in 24-hour notation
311 The day of the week as a decimal, range 1 to 7, Monday being 1.
319 The week number of the current year as a decimal number,
320 range 00 to 53, starting with the first Sunday as the first day
332 The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
333 range 01 to 53, where week 1 is the first week that has at least
334 4 days in the new year.
347 The day of the week as a decimal, range 0 to 6, Sunday being 0.
354 The week number of the current year as a decimal number,
355 range 00 to 53, starting with the first Monday as the first day of week 01.
362 The preferred date representation for the current locale without the time.
363 (The specific format used in the current locale can be obtained by calling
367 as an argument for the
369 conversion specification, and with
373 conversion specification.)
374 (In the POSIX locale this is equivalent to
378 The preferred time representation for the current locale without the date.
379 (The specific format used in the current locale can be obtained by calling
383 as an argument for the
385 conversion specification, and with
389 conversion specification.)
390 (In the POSIX locale this is equivalent to
394 The year as a decimal number without a century (range 00 to 99).
397 conversion specification corresponds to the year since the beginning of the era
400 conversion specification.)
405 The year as a decimal number including the century.
408 conversion specification corresponds to
409 the full alternative year representation.)
418 numeric timezone (that is, the hour and minute offset from UTC). (SU)
421 The timezone name or abbreviation.
424 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
429 (Not supported in glibc2.)
432 A literal \(aq%\(aq character.
434 Some conversion specifications can be modified by preceding the
435 conversion specifier character by the
440 to indicate that an alternative format should be used.
441 If the alternative format or specification does not exist for
442 the current locale, the behavior will be as if the unmodified
443 conversion specification were used. (SU)
444 The Single UNIX Specification mentions
464 where the effect of the
467 alternative numeric symbols (say, roman numerals), and that of the
469 modifier is to use a locale-dependent alternative representation.
470 The rules governing date representation with the
472 modifier can be obtained by supplying
476 One example of such alternative forms is the Japanese era calendar scheme in the
483 except it uses the specified
485 instead of the current locale.
486 The behaviour is undefined if
489 .BR LC_GLOBAL_LOCALE .
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
537 T} Thread safety MT-Safe env locale
551 .\" FIXME strftime() is in POSIX.1-2001 and POSIX.1-2008, but the details
552 .\" in the standards changed across versions. Investigate and
554 There are strict inclusions between the set of conversions
555 given in ANSI C (unmarked), those given in the Single UNIX Specification
556 (marked SU), those given in Olson's timezone package (marked TZ),
557 and those given in glibc (marked GNU), except that
559 is not supported in glibc2.
560 On the other hand glibc2 has several more extensions.
561 POSIX.1 only refers to ANSI C; POSIX.2 describes under
563 several extensions that could apply to
568 conversion is in C99 and POSIX.1-2001.
572 specifier allowed a range of 00 to 61,
573 to allow for the theoretical possibility of a minute that
574 included a double leap second
575 (there never has been such a minute).
577 .SS ISO 8601 week dates
582 yield values calculated from the week-based year defined by the
584 In this system, weeks start on a Monday, and are numbered from 01,
585 for the first week, up to 52 or 53, for the last week.
586 Week 1 is the first week where four or more days fall within the
587 new year (or, synonymously, week 01 is:
588 the first week of the year that contains a Thursday;
589 or, the week that has 4 January in it).
590 When three or fewer days of the first calendar week of the new year fall
592 then the ISO 8601 week-based system counts those days as part of week 52
593 or 53 of the preceding year.
594 For example, 1 January 2010 is a Friday,
595 meaning that just three days of that calendar week fall in 2010.
596 Thus, the ISO\ 8601 week-based system considers these days to be part of
601 week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
602 Similarly, the first two days of January 2011 are considered to be part
603 of week 52 of the year 2010.
605 Glibc provides some extensions for conversion specifications.
606 (These extensions are not specified in POSIX.1-2001, but a few other
607 systems provide similar features.)
608 .\" HP-UX and Tru64 also have features like this.
609 Between the \(aq%\(aq character and the conversion specifier character,
619 modifiers, if present.)
621 The following flag characters are permitted:
625 Pad a numeric result string with spaces.
629 Do not pad a numeric result string.
632 Pad a numeric result string with zeros even if the conversion
633 specifier character uses space-padding by default.
636 Convert alphabetic characters in result string to uppercase.
639 Swap the case of the result string.
640 (This flag works only with certain conversion specifier characters,
641 and of these, it is only really useful with
644 An optional decimal width specifier may follow the (possibly absent) flag.
645 If the natural size of the field is smaller than this width,
646 then the result string is padded (on the left) to the specified width.
648 If the output string would exceed
655 This makes it impossible to distinguish this error case from cases where the
657 string legitimately produces a zero-length output string.
666 Some buggy versions of
668 complain about the use of
670 .IR "warning: \`%c\(aq yields only last 2 digits of year in some locales" .
671 Of course programmers are encouraged to use
673 as it gives the preferred date and time representation.
674 One meets all kinds of strange obfuscations
678 A relatively clean one is to add an
679 intermediate function
684 my_strftime(char *s, size_t max, const char *fmt,
687 return strftime(s, max, fmt, tm);
695 .I \-Wno\-format\-y2k
696 option to prevent the warning,
697 so that the above workaround is no longer required.
699 .B RFC\~2822-compliant date format
700 (with an English locale for %a and %b)
704 "%a,\ %d\ %b\ %Y\ %T\ %z"
708 .B RFC\~822-compliant date format
709 (with an English locale for %a and %b)
713 "%a,\ %d\ %b\ %y\ %T\ %z"
717 The program below can be used to experiment with
720 Some examples of the result string produced by the glibc implementation of
726 .RB "$" " ./a.out \(aq%m\(aq"
727 Result string is "11"
728 .RB "$" " ./a.out \(aq%5m\(aq"
729 Result string is "00011"
730 .RB "$" " ./a.out \(aq%_5m\(aq"
731 Result string is " 11"
742 main(int argc, char *argv[])
755 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
756 fprintf(stderr, "strftime returned 0");
760 printf("Result string is \e"%s\e"\en", outstr);