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

RETURN VALUE

162       The  strftime() function returns the number of characters placed in the
163       array s, not including the terminating null byte, provided the  string,
164       including  the  terminating  null byte, fits.  Otherwise, it returns 0,
165       and the contents of the array is undefined.  (Thus at least since  libc
166       4.4.4;  very old versions of libc, such as libc 4.4.1, would return max
167       if the array was too small.)
168
169       Note that the return value 0 does not necessarily  indicate  an  error;
170       for example, in many locales %p yields an empty string.
171

ENVIRONMENT

173       The environment variables TZ and LC_TIME are used.
174

CONFORMING TO

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

GLIBC NOTES

190       Glibc provides some extensions for conversion  specifications.   (These
191       extensions  are  not specified in POSIX.1-2001, but a few other systems
192       provide similar features.)  Between the % character and the  conversion
193       specifier character, an optional flag and field width may be specified.
194       (These precede the E or O modifiers, if present.)
195
196       The following flag characters are permitted:
197
198       _      (underscore) Pad a numeric result string with spaces.
199
200       -      (dash) Do not pad a numeric result string.
201
202       0      Pad a numeric result string with zeros even  if  the  conversion
203              specifier character uses space-padding by default.
204
205       ^      Convert alphabetic characters in result string to upper case.
206
207       #      Swap  the case of the result string.  (This flag only works with
208              certain conversion specifier characters, and  of  these,  it  is
209              only really useful with %Z).
210
211       An  optional  decimal  width specifier may follow the (possibly absent)
212       flag.  If the natural size of the field is  smaller  than  this  width,
213       then the result string is padded (on the left) to the specified width.
214

BUGS

216       Some  buggy versions of gcc complain about the use of %c: warning: `%c'
217       yields only last 2 digits of year in some locales.  Of course  program‐
218       mers  are  encouraged  to  use %c, it gives the preferred date and time
219       representation. One meets all kinds of strange obfuscations to  circum‐
220       vent this gcc problem. A relatively clean one is to add an intermediate
221       function
222              size_t my_strftime(char *s, size_t max, const char  *fmt,  const
223              struct tm *tm) {
224                   return strftime(s, max, fmt, tm);
225              }
226

EXAMPLE

228       The program below can be used to experiment with strftime().
229
230       #include <time.h>
231       #include <stdio.h>
232       #include <stdlib.h>
233
234       int
235       main(int argc, char *argv[])
236       {
237           char outstr[200];
238           time_t t;
239           struct tm *tmp;
240
241           t = time(NULL);
242           tmp = localtime(&t);
243           if (tmp == NULL) {
244               perror("localtime");
245               exit(EXIT_FAILURE);
246           }
247
248           if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
249               fprintf(stderr, "strftime returned 0");
250               exit(EXIT_FAILURE);
251           }
252
253           printf("Result string is \"%s\"\n", outstr);
254           exit(EXIT_SUCCESS);
255       } /* main */
256
257       Some examples of the result string produced by the glibc implementation
258       of strftime() are as follows:
259
260       $ ./a.out "%m"
261       Result string is "11"
262       $ ./a.out "%5m"
263       Result string is "00011"
264       $ ./a.out "%_5m"
265       Result string is "   11"
266

SEE ALSO

268       date(1), time(2), ctime(3), setlocale(3), sprintf(3), strptime(3)
269
270
271
272GNU                               2005-11-23                       STRFTIME(3)
Impressum