1STRFTIME(3)                Linux Programmer's Manual               STRFTIME(3)
2
3
4

NAME

6       strftime - format date and time
7

SYNOPSIS

9       #include <time.h>
10
11       size_t strftime(char *s, size_t max, const char *format,
12                       const struct tm *tm);
13

DESCRIPTION

15       The  strftime()  function  formats the broken-down time tm according to
16       the format specification format and places the result in the  character
17       array s of size max.
18
19       The  format  specification  is a null-terminated string and may contain
20       special character sequences called conversion specifications,  each  of
21       which  is  introduced  by  a '%' character and terminated by some other
22       character known as a conversion specifier character.  All other charac‐
23       ter sequences are ordinary character sequences.
24
25       The  characters  of  ordinary  character  sequences (including the null
26       byte) are copied verbatim from format to s.  However, the characters of
27       conversion specifications are replaced as follows:
28
29       %a     The abbreviated weekday name according to the current locale.
30
31       %A     The full weekday name according to the current locale.
32
33       %b     The abbreviated month name according to the current locale.
34
35       %B     The full month name according to the current locale.
36
37       %c     The  preferred  date  and  time  representation  for the current
38              locale.
39
40       %C     The century number (year/100) as a 2-digit integer. (SU)
41
42       %d     The day of the month as a decimal number (range 01 to 31).
43
44       %D     Equivalent to %m/%d/%y.  (Yecch—for Americans  only.   Americans
45              should  note  that in other countries %d/%m/%y is rather common.
46              This means that in international context this format is  ambigu‐
47              ous and should not be used.) (SU)
48
49       %e     Like %d, the day of the month as a decimal number, but a leading
50              zero is replaced by a space. (SU)
51
52       %E     Modifier: use alternative format, see below. (SU)
53
54       %F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
55
56       %G     The ISO 8601 week-based year (see NOTES) with century as a deci‐
57              mal number.  The 4-digit year corresponding to the ISO week num‐
58              ber (see %V).  This has the same format and value as %Y,  except
59              that  if  the  ISO  week  number belongs to the previous or next
60              year, that year is used instead. (TZ)
61
62       %g     Like %G, but without century,  that  is,  with  a  2-digit  year
63              (00-99). (TZ)
64
65       %h     Equivalent to %b.  (SU)
66
67       %H     The  hour as a decimal number using a 24-hour clock (range 00 to
68              23).
69
70       %I     The hour as a decimal number using a 12-hour clock (range 01  to
71              12).
72
73       %j     The day of the year as a decimal number (range 001 to 366).
74
75       %k     The  hour  (24-hour  clock) as a decimal number (range 0 to 23);
76              single digits are preceded by a blank.  (See also %H.)  (TZ)
77
78       %l     The hour (12-hour clock) as a decimal number (range  1  to  12);
79              single digits are preceded by a blank.  (See also %I.)  (TZ)
80
81       %m     The month as a decimal number (range 01 to 12).
82
83       %M     The minute as a decimal number (range 00 to 59).
84
85       %n     A newline character. (SU)
86
87       %O     Modifier: use alternative format, see below. (SU)
88
89       %p     Either  "AM"  or  "PM" according to the given time value, or the
90              corresponding strings for the current locale.  Noon  is  treated
91              as "PM" and midnight as "AM".
92
93       %P     Like %p but in lowercase: "am" or "pm" or a corresponding string
94              for the current locale. (GNU)
95
96       %r     The time in a.m. or p.m. notation.  In the POSIX locale this  is
97              equivalent to %I:%M:%S %p.  (SU)
98
99       %R     The  time  in  24-hour  notation  (%H:%M).   (SU)  For a version
100              including the seconds, see %T below.
101
102       %s     The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000
103              (UTC). (TZ)
104
105       %S     The  second as a decimal number (range 00 to 60).  (The range is
106              up to 60 to allow for occasional leap seconds.)
107
108       %t     A tab character. (SU)
109
110       %T     The time in 24-hour notation (%H:%M:%S).  (SU)
111
112       %u     The day of the week as a decimal, range 1 to 7, Monday being  1.
113              See also %w.  (SU)
114
115       %U     The  week  number of the current year as a decimal number, range
116              00 to 53, starting with the first Sunday as  the  first  day  of
117              week 01.  See also %V and %W.
118
119       %V     The  ISO 8601  week  number (see NOTES) of the current year as a
120              decimal number, range 01 to 53, where week 1 is the  first  week
121              that  has  at least 4 days in the new year.  See also %U and %W.
122              (SU)
123
124       %w     The day of the week as a decimal, range 0 to 6, Sunday being  0.
125              See also %u.
126
127       %W     The  week  number of the current year as a decimal number, range
128              00 to 53, starting with the first Monday as  the  first  day  of
129              week 01.
130
131       %x     The preferred date representation for the current locale without
132              the time.
133
134       %X     The preferred time representation for the current locale without
135              the date.
136
137       %y     The year as a decimal number without a century (range 00 to 99).
138
139       %Y     The year as a decimal number including the century.
140
141       %z     The  +hhmm  or  -hhmm  numeric  timezone  (that is, the hour and
142              minute offset from UTC). (SU)
143
144       %Z     The timezone name or abbreviation.
145
146       %+     The date and time in date(1)  format.  (TZ)  (Not  supported  in
147              glibc2.)
148
149       %%     A literal '%' character.
150
151       Some conversion specifications can be modified by preceding the conver‐
152       sion specifier character by the E or O modifier  to  indicate  that  an
153       alternative format should be used.  If the alternative format or speci‐
154       fication does not exist for the current locale, the behavior will be as
155       if  the  unmodified conversion specification were used. (SU) The Single
156       UNIX Specification mentions %Ec, %EC, %Ex, %EX,  %Ey,  %EY,  %Od,  %Oe,
157       %OH, %OI, %Om, %OM, %OS, %Ou, %OU, %OV, %Ow, %OW, %Oy, where the effect
158       of the O modifier is to use alternative  numeric  symbols  (say,  roman
159       numerals),  and  that  of  the  E modifier is to use a locale-dependent
160       alternative representation.
161
162       The broken-down time structure tm is defined  in  <time.h>.   See  also
163       ctime(3).
164

RETURN VALUE

166       Provided  that  the result string, including the terminating null byte,
167       does not exceed max bytes,  strftime()  returns  the  number  of  bytes
168       (excluding  the  terminating  null byte) placed in the array s.  If the
169       length of the result string (including the terminating null byte) would
170       exceed  max  bytes,  then strftime() returns 0, and the contents of the
171       array are undefined.  (This behavior applies since at least libc 4.4.4;
172       very  old versions of libc, such as libc 4.4.1, would return max if the
173       array was too small.)
174
175       Note that the return value 0 does not necessarily  indicate  an  error.
176       For  example, in many locales %p yields an empty string.  An empty for‐
177       mat string will likewise yield an empty string.
178

ENVIRONMENT

180       The environment variables TZ and LC_TIME are used.
181

CONFORMING TO

183       SVr4, C89, C99.  There are strict inclusions between the set of conver‐
184       sions given in ANSI C (unmarked), those given in the Single UNIX Speci‐
185       fication (marked SU), those given in Olson's timezone  package  (marked
186       TZ),  and those given in glibc (marked GNU), except that %+ is not sup‐
187       ported in glibc2.  On the other hand glibc2  has  several  more  exten‐
188       sions.   POSIX.1 only refers to ANSI C; POSIX.2 describes under date(1)
189       several extensions that could apply to strftime() as well.  The %F con‐
190       version is in C99 and POSIX.1-2001.
191
192       In  SUSv2,  the  %S specifier allowed a range of 00 to 61, to allow for
193       the theoretical possibility of a minute that  included  a  double  leap
194       second (there never has been such a minute).
195

NOTES

197   ISO 8601 week dates
198       %G, %g, and %V yield values calculated from the week-based year defined
199       by the ISO 8601 standard.  In this system, weeks start on a Monday, and
200       are  numbered from 01, for the first week, up to 52 or 53, for the last
201       week.  Week 1 is the first week where four or more days fall within the
202       new year (or, synonymously, week 01 is: the first week of the year that
203       contains a Thursday; or, the week that has  4  January  in  it).   When
204       three  of  fewer  days  of the first calendar week of the new year fall
205       within that year, then the ISO 8601 week-based system counts those days
206       as  part of week 53 of the preceding year.  For example, 1 January 2010
207       is a Friday, meaning that just three days of that calendar week fall in
208       2010.   Thus, the ISO 8601 week-based system considers these days to be
209       part of week 53 (%V) of the year 2009 (%G); week 01  of  ISO 8601  year
210       2010 starts on Monday, 4 January 2010.
211
212   Glibc notes
213       Glibc  provides  some extensions for conversion specifications.  (These
214       extensions are not specified in POSIX.1-2001, but a few  other  systems
215       provide  similar  features.)  Between the '%' character and the conver‐
216       sion specifier character, an optional flag and field width may be spec‐
217       ified.  (These precede the E or O modifiers, if present.)
218
219       The following flag characters are permitted:
220
221       _      (underscore) Pad a numeric result string with spaces.
222
223       -      (dash) Do not pad a numeric result string.
224
225       0      Pad  a  numeric  result string with zeros even if the conversion
226              specifier character uses space-padding by default.
227
228       ^      Convert alphabetic characters in result string to upper case.
229
230       #      Swap the case of the result string.  (This flag works only  with
231              certain  conversion  specifier  characters,  and of these, it is
232              only really useful with %Z.)
233
234       An optional decimal width specifier may follow  the  (possibly  absent)
235       flag.   If  the  natural  size of the field is smaller than this width,
236       then the result string is padded (on the left) to the specified width.
237

BUGS

239       If the output string would exceed max bytes, errno is  not  set.   This
240       makes it impossible to distinguish this error case from cases where the
241       format  string  legitimately  produces  a  zero-length  output  string.
242       POSIX.1-2001 does not specify any errno settings for strftime().
243
244       Some  buggy  versions  of gcc(1) complain about the use of %c: warning:
245       `%c' yields only last 2 digits of year in some locales.  Of course pro‐
246       grammers are encouraged to use %c, it gives the preferred date and time
247       representation.  One meets all kinds of strange obfuscations to circum‐
248       vent this gcc(1) problem.  A relatively clean one is to add an interme‐
249       diate function
250
251           size_t
252           my_strftime(char *s, size_t max, const char *fmt,
253                       const struct tm *tm)
254           {
255               return strftime(s, max, fmt, tm);
256           }
257
258       Nowadays, gcc(1) provides the -Wno-format-y2k  option  to  prevent  the
259       warning, so that the above workaround is no longer required.
260

EXAMPLE

262       RFC 2822-compliant date format (with an English locale for %a and %b)
263
264         "%a, %d %b %Y %T %z"
265
266       RFC 822-compliant date format (with an English locale for %a and %b)
267
268         "%a, %d %b %y %T %z"
269
270   Example program
271       The program below can be used to experiment with strftime().
272
273       Some examples of the result string produced by the glibc implementation
274       of strftime() are as follows:
275
276           $ ./a.out '%m'
277           Result string is "11"
278           $ ./a.out '%5m'
279           Result string is "00011"
280           $ ./a.out '%_5m'
281           Result string is "   11"
282
283       Here's the program source:
284
285       #include <time.h>
286       #include <stdio.h>
287       #include <stdlib.h>
288
289       int
290       main(int argc, char *argv[])
291       {
292           char outstr[200];
293           time_t t;
294           struct tm *tmp;
295
296           t = time(NULL);
297           tmp = localtime(&t);
298           if (tmp == NULL) {
299               perror("localtime");
300               exit(EXIT_FAILURE);
301           }
302
303           if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
304               fprintf(stderr, "strftime returned 0");
305               exit(EXIT_FAILURE);
306           }
307
308           printf("Result string is \"%s\"\n", outstr);
309           exit(EXIT_SUCCESS);
310       }
311

SEE ALSO

313       date(1), time(2), ctime(3), setlocale(3), sprintf(3), strptime(3)
314

COLOPHON

316       This page is part of release 3.53 of the Linux  man-pages  project.   A
317       description  of  the project, and information about reporting bugs, can
318       be found at http://www.kernel.org/doc/man-pages/.
319
320
321
322GNU                               2013-06-28                       STRFTIME(3)
Impressum