1STRFTIME(3) Linux Programmer's Manual STRFTIME(3)
2
3
4
6 strftime - format date and time
7
9 #include <time.h>
10
11 size_t strftime(char *s, size_t max, const char *format,
12 const struct tm *tm);
13
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 week-based year (see NOTES) with century as a deci‐
52 mal number. The 4-digit year corresponding to the ISO week num‐
53 ber (see %V). This has the same format and value as %Y, except
54 that if the ISO week number belongs to the previous or next
55 year, that year is used instead. (TZ)
56
57 %g Like %G, but without century, that is, with a 2-digit year
58 (00-99). (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, that is, 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 week number (see NOTES) of the current year as a
115 decimal number, range 01 to 53, where week 1 is the first week
116 that has at least 4 days in the new year. See also %U and %W.
117 (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 timezone 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 behavior will be as
151 if the unmodified conversion specification were used. (SU) The Single
152 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
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. (This behavior applies
166 since at least libc 4.4.4; very old versions of libc, such as libc
167 4.4.1, would return max 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
173 The environment variables TZ and LC_TIME are used.
174
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 exten‐
181 sions. POSIX.1 only refers to ANSI C; POSIX.2 describes under date(1)
182 several extensions that could apply to strftime() as well. The %F con‐
183 version is in C99 and POSIX.1-2001.
184
185 In SUSv2, the %S specifier 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
190 ISO 8601 Week Dates
191 %G, %g, and %V yield values calculated from the week-based year defined
192 by the ISO 8601 standard. In this system, weeks start on a Monday, and
193 are numbered from 01, for the first week, up to 52 or 53, for the last
194 week. Week 1 is the first week where four or more days fall within the
195 new year (or, synonymously, week 01 is: the first week of the year that
196 contains a Thursday; or, the week that has 4 January in it). When
197 three of fewer days of the first calendar week of the new year fall
198 within that year, then the ISO 8601 week-based system counts those days
199 as part of week 53 of the preceding year. For example, 1 January 2010
200 is a Friday, meaning that just three days of that calendar week fall in
201 2010. Thus, the ISO 8601 week-based system considers these days to be
202 part of week 53 (%V) of the year 2009 (%G) ; week 01 of ISO 8601 year
203 2010 starts on Thursday, 4 January 2010.
204
205 Glibc Notes
206 Glibc provides some extensions for conversion specifications. (These
207 extensions are not specified in POSIX.1-2001, but a few other systems
208 provide similar features.) Between the '%' character and the conver‐
209 sion specifier character, an optional flag and field width may be spec‐
210 ified. (These precede the E or O modifiers, if present.)
211
212 The following flag characters are permitted:
213
214 _ (underscore) Pad a numeric result string with spaces.
215
216 - (dash) Do not pad a numeric result string.
217
218 0 Pad a numeric result string with zeros even if the conversion
219 specifier character uses space-padding by default.
220
221 ^ Convert alphabetic characters in result string to upper case.
222
223 # Swap the case of the result string. (This flag only works with
224 certain conversion specifier characters, and of these, it is
225 only really useful with %Z.)
226
227 An optional decimal width specifier may follow the (possibly absent)
228 flag. If the natural size of the field is smaller than this width,
229 then the result string is padded (on the left) to the specified width.
230
232 Some buggy versions of gcc(1) complain about the use of %c: warning:
233 `%c' yields only last 2 digits of year in some locales. Of course pro‐
234 grammers are encouraged to use %c, it gives the preferred date and time
235 representation. One meets all kinds of strange obfuscations to circum‐
236 vent this gcc(1) problem. A relatively clean one is to add an interme‐
237 diate function
238
239 size_t
240 my_strftime(char *s, size_t max, const char *fmt,
241 const struct tm *tm)
242 {
243 return strftime(s, max, fmt, tm);
244 }
245
246 Nowadays, gcc(1) provides the -Wno-format-y2k option to prevent the
247 warning, so that the above workaround is no longer required.
248
250 The program below can be used to experiment with strftime().
251
252 Some examples of the result string produced by the glibc implementation
253 of strftime() are as follows:
254
255 $ ./a.out '%m'
256 Result string is "11"
257 $ ./a.out '%5m'
258 Result string is "00011"
259 $ ./a.out '%_5m'
260 Result string is " 11"
261
262 Program source
263
264 #include <time.h>
265 #include <stdio.h>
266 #include <stdlib.h>
267
268 int
269 main(int argc, char *argv[])
270 {
271 char outstr[200];
272 time_t t;
273 struct tm *tmp;
274
275 t = time(NULL);
276 tmp = localtime(&t);
277 if (tmp == NULL) {
278 perror("localtime");
279 exit(EXIT_FAILURE);
280 }
281
282 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
283 fprintf(stderr, "strftime returned 0");
284 exit(EXIT_FAILURE);
285 }
286
287 printf("Result string is \"%s\"\n", outstr);
288 exit(EXIT_SUCCESS);
289 } /* main */
290
292 date(1), time(2), ctime(3), setlocale(3), sprintf(3), strptime(3)
293
295 This page is part of release 3.22 of the Linux man-pages project. A
296 description of the project, and information about reporting bugs, can
297 be found at http://www.kernel.org/doc/man-pages/.
298
299
300
301GNU 2009-02-24 STRFTIME(3)