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 2015-03-02 "GNU" "Linux Programmer's Manual"
39 strftime \- format date and time
44 .BI "size_t strftime(char *" s ", size_t " max ", const char *" format ,
45 .BI " const struct tm *" tm );
50 function formats the broken-down time
52 according to the format specification
55 result in the character array
59 .\" FIXME . POSIX says: Local timezone information is used as though
60 .\" strftime() called tzset(). But this doesn't appear to be the case
62 The format specification is a null-terminated string and may contain
63 special character sequences called
64 .IR "conversion specifications",
65 each of which is introduced by a \(aq%\(aq character and terminated by
66 some other character known as a
67 .IR "conversion specifier character".
68 All other character sequences are
69 .IR "ordinary character sequences".
71 The characters of ordinary character sequences (including the null byte)
72 are copied verbatim from
76 However, the characters
77 of conversion specifications are replaced as follows:
80 The abbreviated name of the day of the week according to the current locale.
83 The full name of the day of the week according to the current locale.
86 The abbreviated month name according to the current locale.
89 The full month name according to the current locale.
92 The preferred date and time representation for the current locale.
95 The century number (year/100) as a 2-digit integer. (SU)
98 The day of the month as a decimal number (range 01 to 31).
103 (Yecch\(emfor Americans only.
104 Americans should note that in other countries
107 This means that in international context this format is
108 ambiguous and should not be used.) (SU)
113 the day of the month as a decimal number, but a leading
114 zero is replaced by a space. (SU)
117 Modifier: use alternative format, see below. (SU)
122 (the ISO\ 8601 date format). (C99)
125 The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
126 The 4-digit year corresponding to the ISO week number (see
128 This has the same format and value as
130 except that if the ISO week number belongs to the previous or next year,
131 that year is used instead. (TZ)
136 but without century, that is, with a 2-digit year (00-99). (TZ)
144 The hour as a decimal number using a 24-hour clock (range 00 to 23).
147 The hour as a decimal number using a 12-hour clock (range 01 to 12).
150 The day of the year as a decimal number (range 001 to 366).
153 The hour (24-hour clock) as a decimal number (range 0 to 23);
154 single digits are preceded by a blank.
160 The hour (12-hour clock) as a decimal number (range 1 to 12);
161 single digits are preceded by a blank.
167 The month as a decimal number (range 01 to 12).
170 The minute as a decimal number (range 00 to 59).
173 A newline character. (SU)
176 Modifier: use alternative format, see below. (SU)
179 Either "AM" or "PM" according to the given time value, or the
180 corresponding strings for the current locale.
181 Noon is treated as "PM" and midnight as "AM".
186 but in lowercase: "am" or "pm" or a corresponding
187 string for the current locale. (GNU)
190 The time in a.m. or p.m. notation.
191 In the POSIX locale this is equivalent to
196 The time in 24-hour notation
199 For a version including the seconds, see
204 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
207 The second as a decimal number (range 00 to 60).
208 (The range is up to 60 to allow for occasional leap seconds.)
211 A tab character. (SU)
214 The time in 24-hour notation
219 The day of the week as a decimal, range 1 to 7, Monday being 1.
225 The week number of the current year as a decimal number,
226 range 00 to 53, starting with the first Sunday as the first day
234 The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
235 range 01 to 53, where week 1 is the first week that has at least
236 4 days in the new year.
244 The day of the week as a decimal, range 0 to 6, Sunday being 0.
249 The week number of the current year as a decimal number,
250 range 00 to 53, starting with the first Monday as the first day of week 01.
253 The preferred date representation for the current locale without the time.
256 The preferred time representation for the current locale without the date.
259 The year as a decimal number without a century (range 00 to 99).
262 The year as a decimal number including the century.
269 numeric timezone (that is, the hour and minute offset from UTC). (SU)
272 The timezone name or abbreviation.
275 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
280 (Not supported in glibc2.)
283 A literal \(aq%\(aq character.
285 Some conversion specifications can be modified by preceding the
286 conversion specifier character by the
291 to indicate that an alternative format should be used.
292 If the alternative format or specification does not exist for
293 the current locale, the behavior will be as if the unmodified
294 conversion specification were used. (SU)
295 The Single UNIX Specification mentions
315 where the effect of the
318 alternative numeric symbols (say, roman numerals), and that of the
319 E modifier is to use a locale-dependent alternative representation.
321 The broken-down time structure
328 Provided that the result string,
329 including the terminating null byte, does not exceed
333 returns the number of bytes (excluding the terminating null byte)
336 If the length of the result string (including the terminating null byte)
341 returns 0, and the contents of the array are undefined.
342 .\" (This behavior applies since at least libc 4.4.4;
343 .\" very old versions of libc, such as libc 4.4.1,
346 .\" if the array was too small.)
348 Note that the return value 0 does not necessarily indicate an error.
349 For example, in many locales
351 yields an empty string.
354 string will likewise yield an empty string.
356 The environment variables
362 For an explanation of the terms used in this section, see
368 Interface Attribute Value
371 T} Thread safety MT-Safe env locale
375 .\" FIXME strftime() is in POSIX.1-2001 and POSIX.1-2008, but the details
376 .\" in the standards changed across kernel versions. Investigate and
378 There are strict inclusions between the set of conversions
379 given in ANSI C (unmarked), those given in the Single UNIX Specification
380 (marked SU), those given in Olson's timezone package (marked TZ),
381 and those given in glibc (marked GNU), except that
383 is not supported in glibc2.
384 On the other hand glibc2 has several more extensions.
385 POSIX.1 only refers to ANSI C; POSIX.2 describes under
387 several extensions that could apply to
392 conversion is in C99 and POSIX.1-2001.
396 specifier allowed a range of 00 to 61,
397 to allow for the theoretical possibility of a minute that
398 included a double leap second
399 (there never has been such a minute).
401 .SS ISO 8601 week dates
406 yield values calculated from the week-based year defined by the
408 In this system, weeks start on a Monday, and are numbered from 01,
409 for the first week, up to 52 or 53, for the last week.
410 Week 1 is the first week where four or more days fall within the
411 new year (or, synonymously, week 01 is:
412 the first week of the year that contains a Thursday;
413 or, the week that has 4 January in it).
414 When three of fewer days of the first calendar week of the new year fall
416 then the ISO 8601 week-based system counts those days as part of week 53
417 of the preceding year.
418 For example, 1 January 2010 is a Friday,
419 meaning that just three days of that calendar week fall in 2010.
420 Thus, the ISO\ 8601 week-based system considers these days to be part of
425 week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
427 Glibc provides some extensions for conversion specifications.
428 (These extensions are not specified in POSIX.1-2001, but a few other
429 systems provide similar features.)
430 .\" HP-UX and Tru64 also have features like this.
431 Between the \(aq%\(aq character and the conversion specifier character,
441 modifiers, if present.)
443 The following flag characters are permitted:
447 Pad a numeric result string with spaces.
451 Do not pad a numeric result string.
454 Pad a numeric result string with zeros even if the conversion
455 specifier character uses space-padding by default.
458 Convert alphabetic characters in result string to uppercase.
461 Swap the case of the result string.
462 (This flag works only with certain conversion specifier characters,
463 and of these, it is only really useful with
466 An optional decimal width specifier may follow the (possibly absent) flag.
467 If the natural size of the field is smaller than this width,
468 then the result string is padded (on the left) to the specified width.
470 If the output string would exceed
477 This makes it impossible to distinguish this error case from cases where the
479 string legitimately produces a zero-length output string.
488 Some buggy versions of
490 complain about the use of
492 .IR "warning: `%c' yields only last 2 digits of year in some locales" .
493 Of course programmers are encouraged to use
495 it gives the preferred date and time representation.
496 One meets all kinds of strange obfuscations
500 A relatively clean one is to add an
501 intermediate function
506 my_strftime(char *s, size_t max, const char *fmt,
509 return strftime(s, max, fmt, tm);
517 .IR \-Wno\-format\-y2k
518 option to prevent the warning,
519 so that the above workaround is no longer required.
521 .BR "RFC\ 2822-compliant date format"
522 (with an English locale for %a and %b)
525 "%a,\ %d\ %b\ %Y\ %T\ %z"
527 .BR "RFC\ 822-compliant date format"
528 (with an English locale for %a and %b)
531 "%a,\ %d\ %b\ %y\ %T\ %z"
533 The program below can be used to experiment with
536 Some examples of the result string produced by the glibc implementation of
542 .RB "$" " ./a.out \(aq%m\(aq"
543 Result string is "11"
544 .RB "$" " ./a.out \(aq%5m\(aq"
545 Result string is "00011"
546 .RB "$" " ./a.out \(aq%_5m\(aq"
547 Result string is " 11"
557 main(int argc, char *argv[])
570 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
571 fprintf(stderr, "strftime returned 0");
575 printf("Result string is \\"%s\\"\\n", outstr);