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 includ‐
100              ing 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 or 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       The  strftime() function returns the number of characters placed in the
167       array s, not including the terminating null byte, provided the  string,
168       including  the  terminating  null byte, fits.  Otherwise, it returns 0,
169       and the contents of the array is  undefined.   (This  behavior  applies
170       since  at  least  libc  4.4.4;  very old versions of libc, such as libc
171       4.4.1, would return max if the array was too small.)
172
173       Note that the return value 0 does not necessarily  indicate  an  error;
174       for example, in many locales %p yields an empty string.
175

ENVIRONMENT

177       The environment variables TZ and LC_TIME are used.
178

CONFORMING TO

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

NOTES

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

BUGS

236       Some  buggy  versions  of gcc(1) complain about the use of %c: warning:
237       `%c' yields only last 2 digits of year in some locales.  Of course pro‐
238       grammers are encouraged to use %c, it gives the preferred date and time
239       representation.  One meets all kinds of strange obfuscations to circum‐
240       vent this gcc(1) problem.  A relatively clean one is to add an interme‐
241       diate function
242
243           size_t
244           my_strftime(char *s, size_t max, const char *fmt,
245                       const struct tm *tm)
246           {
247               return strftime(s, max, fmt, tm);
248           }
249
250       Nowadays, gcc(1) provides the -Wno-format-y2k  option  to  prevent  the
251       warning, so that the above workaround is no longer required.
252

EXAMPLES

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

SEE ALSO

305       date(1), time(2), ctime(3), setlocale(3), sprintf(3), strptime(3)
306

COLOPHON

308       This page is part of release 3.25 of the Linux  man-pages  project.   A
309       description  of  the project, and information about reporting bugs, can
310       be found at http://www.kernel.org/doc/man-pages/.
311
312
313
314GNU                               2010-01-17                       STRFTIME(3)
Impressum