1STRFTIME(P) POSIX Programmer's Manual STRFTIME(P)
2
3
4
6 strftime - convert date and time to a string
7
9 #include <time.h>
10
11 size_t strftime(char *restrict s, size_t maxsize,
12 const char *restrict format, const struct tm *restrict timeptr);
13
14
16 The strftime() function shall place bytes into the array pointed to by
17 s as controlled by the string pointed to by format. The format is a
18 character string, beginning and ending in its initial shift state, if
19 any. The format string consists of zero or more conversion specifica‐
20 tions and ordinary characters. A conversion specification consists of
21 a '%' character, possibly followed by an E or O modifier, and a termi‐
22 nating conversion specifier character that determines the conversion
23 specification's behavior. All ordinary characters (including the termi‐
24 nating null byte) are copied unchanged into the array. If copying takes
25 place between objects that overlap, the behavior is undefined. No more
26 than maxsize bytes are placed into the array. Each conversion specifier
27 is replaced by appropriate characters as described in the following
28 list. The appropriate characters are determined using the LC_TIME cate‐
29 gory of the current locale and by the values of zero or more members of
30 the broken-down time structure pointed to by timeptr, as specified in
31 brackets in the description. If any of the specified values are outside
32 the normal range, the characters stored are unspecified.
33
34 Local timezone information is used as though strftime() called tzset().
35
36 The following conversion specifications are supported:
37
38 %a Replaced by the locale's abbreviated weekday name. [ tm_wday]
39
40 %A Replaced by the locale's full weekday name. [ tm_wday]
41
42 %b Replaced by the locale's abbreviated month name. [ tm_mon]
43
44 %B Replaced by the locale's full month name. [ tm_mon]
45
46 %c Replaced by the locale's appropriate date and time representa‐
47 tion. (See the Base Definitions volume of IEEE Std 1003.1-2001,
48 <time.h>.)
49
50 %C Replaced by the year divided by 100 and truncated to an integer,
51 as a decimal number [00,99]. [ tm_year]
52
53 %d Replaced by the day of the month as a decimal number [01,31]. [
54 tm_mday]
55
56 %D Equivalent to %m / %d / %y . [ tm_mon, tm_mday, tm_year]
57
58 %e Replaced by the day of the month as a decimal number [1,31]; a
59 single digit is preceded by a space. [ tm_mday]
60
61 %F Equivalent to %Y - %m - %d (the ISO 8601:2000 standard date for‐
62 mat). [ tm_year, tm_mon, tm_mday]
63
64 %g Replaced by the last 2 digits of the week-based year (see below)
65 as a decimal number [00,99]. [ tm_year, tm_wday, tm_yday]
66
67 %G Replaced by the week-based year (see below) as a decimal number
68 (for example, 1977). [ tm_year, tm_wday, tm_yday]
69
70 %h Equivalent to %b . [ tm_mon]
71
72 %H Replaced by the hour (24-hour clock) as a decimal number
73 [00,23]. [ tm_hour]
74
75 %I Replaced by the hour (12-hour clock) as a decimal number
76 [01,12]. [ tm_hour]
77
78 %j Replaced by the day of the year as a decimal number [001,366]. [
79 tm_yday]
80
81 %m Replaced by the month as a decimal number [01,12]. [ tm_mon]
82
83 %M Replaced by the minute as a decimal number [00,59]. [ tm_min]
84
85 %n Replaced by a <newline>.
86
87 %p Replaced by the locale's equivalent of either a.m. or p.m. [
88 tm_hour]
89
90 %r Replaced by the time in a.m. and p.m. notation; in the POSIX
91 locale this shall be equivalent to %I : %M : %S %p . [ tm_hour,
92 tm_min, tm_sec]
93
94 %R Replaced by the time in 24-hour notation ( %H : %M ). [
95 tm_hour, tm_min]
96
97 %S Replaced by the second as a decimal number [00,60]. [ tm_sec]
98
99 %t Replaced by a <tab>.
100
101 %T Replaced by the time ( %H : %M : %S ). [ tm_hour, tm_min,
102 tm_sec]
103
104 %u Replaced by the weekday as a decimal number [1,7], with 1 repre‐
105 senting Monday. [ tm_wday]
106
107 %U Replaced by the week number of the year as a decimal number
108 [00,53]. The first Sunday of January is the first day of week
109 1; days in the new year before this are in week 0. [ tm_year,
110 tm_wday, tm_yday]
111
112 %V Replaced by the week number of the year (Monday as the first day
113 of the week) as a decimal number [01,53]. If the week containing
114 1 January has four or more days in the new year, then it is con‐
115 sidered week 1. Otherwise, it is the last week of the previous
116 year, and the next week is week 1. Both January 4th and the
117 first Thursday of January are always in week 1. [ tm_year,
118 tm_wday, tm_yday]
119
120 %w Replaced by the weekday as a decimal number [0,6], with 0 repre‐
121 senting Sunday. [ tm_wday]
122
123 %W Replaced by the week number of the year as a decimal number
124 [00,53]. The first Monday of January is the first day of week
125 1; days in the new year before this are in week 0. [ tm_year,
126 tm_wday, tm_yday]
127
128 %x Replaced by the locale's appropriate date representation. (See
129 the Base Definitions volume of IEEE Std 1003.1-2001, <time.h>.)
130
131 %X Replaced by the locale's appropriate time representation. (See
132 the Base Definitions volume of IEEE Std 1003.1-2001, <time.h>.)
133
134 %y Replaced by the last two digits of the year as a decimal number
135 [00,99]. [ tm_year]
136
137 %Y Replaced by the year as a decimal number (for example, 1997). [
138 tm_year]
139
140 %z Replaced by the offset from UTC in the ISO 8601:2000 standard
141 format ( +hhmm or -hhmm ), or by no characters if no timezone is
142 determinable. For example, "-0430" means 4 hours 30 minutes
143 behind UTC (west of Greenwich). If tm_isdst is zero, the stan‐
144 dard time offset is used. If tm_isdst is greater than zero, the
145 daylight savings time offset is used. If tm_isdst is negative,
146 no characters are returned. [ tm_isdst]
147
148 %Z Replaced by the timezone name or abbreviation, or by no bytes if
149 no timezone information exists. [ tm_isdst]
150
151 %% Replaced by % .
152
153
154 If a conversion specification does not correspond to any of the above,
155 the behavior is undefined.
156
157 If a struct tm broken-down time structure is created by localtime() or
158 localtime_r(), or modified by mktime(), and the value of TZ is subse‐
159 quently modified, the results of the %Z and %z strftime() conversion
160 specifiers are undefined, when strftime() is called with such a broken-
161 down time structure.
162
163 If a struct tm broken-down time structure is created or modified by
164 gmtime() or gmtime_r(), it is unspecified whether the result of the %Z
165 and %z conversion specifiers shall refer to UTC or the current local
166 timezone, when strftime() is called with such a broken-down time struc‐
167 ture.
168
169 Modified Conversion Specifiers
170 Some conversion specifiers can be modified by the E or O modifier char‐
171 acters to indicate that an alternative format or specification should
172 be used rather than the one normally used by the unmodified conversion
173 specifier. If the alternative format or specification does not exist
174 for the current locale (see ERA in the Base Definitions volume of
175 IEEE Std 1003.1-2001, Section 7.3.5, LC_TIME), the behavior shall be as
176 if the unmodified conversion specification were used.
177
178 %Ec Replaced by the locale's alternative appropriate date and time
179 representation.
180
181 %EC Replaced by the name of the base year (period) in the locale's
182 alternative representation.
183
184 %Ex Replaced by the locale's alternative date representation.
185
186 %EX Replaced by the locale's alternative time representation.
187
188 %Ey Replaced by the offset from %EC (year only) in the locale's
189 alternative representation.
190
191 %EY Replaced by the full alternative year representation.
192
193 %Od Replaced by the day of the month, using the locale's alternative
194 numeric symbols, filled as needed with leading zeros if there is
195 any alternative symbol for zero; otherwise, with leading spaces.
196
197 %Oe Replaced by the day of the month, using the locale's alternative
198 numeric symbols, filled as needed with leading spaces.
199
200 %OH Replaced by the hour (24-hour clock) using the locale's alterna‐
201 tive numeric symbols.
202
203 %OI Replaced by the hour (12-hour clock) using the locale's alterna‐
204 tive numeric symbols.
205
206 %Om Replaced by the month using the locale's alternative numeric
207 symbols.
208
209 %OM Replaced by the minutes using the locale's alternative numeric
210 symbols.
211
212 %OS Replaced by the seconds using the locale's alternative numeric
213 symbols.
214
215 %Ou Replaced by the weekday as a number in the locale's alternative
216 representation (Monday=1).
217
218 %OU Replaced by the week number of the year (Sunday as the first day
219 of the week, rules corresponding to %U ) using the locale's
220 alternative numeric symbols.
221
222 %OV Replaced by the week number of the year (Monday as the first day
223 of the week, rules corresponding to %V ) using the locale's
224 alternative numeric symbols.
225
226 %Ow Replaced by the number of the weekday (Sunday=0) using the
227 locale's alternative numeric symbols.
228
229 %OW Replaced by the week number of the year (Monday as the first day
230 of the week) using the locale's alternative numeric symbols.
231
232 %Oy Replaced by the year (offset from %C ) using the locale's alter‐
233 native numeric symbols.
234
235
236 %g , %G , and %V give values according to the ISO 8601:2000 standard
237 week-based year. In this system, weeks begin on a Monday and week 1 of
238 the year is the week that includes January 4th, which is also the week
239 that includes the first Thursday of the year, and is also the first
240 week that contains at least four days in the year. If the first Monday
241 of January is the 2nd, 3rd, or 4th, the preceding days are part of the
242 last week of the preceding year; thus, for Saturday 2nd January 1999,
243 %G is replaced by 1998 and %V is replaced by 53. If December 29th,
244 30th, or 31st is a Monday, it and any following days are part of week 1
245 of the following year. Thus, for Tuesday 30th December 1997, %G is
246 replaced by 1998 and %V is replaced by 01.
247
248 If a conversion specifier is not one of the above, the behavior is
249 undefined.
250
252 If the total number of resulting bytes including the terminating null
253 byte is not more than maxsize, strftime() shall return the number of
254 bytes placed into the array pointed to by s, not including the termi‐
255 nating null byte. Otherwise, 0 shall be returned and the contents of
256 the array are unspecified.
257
259 No errors are defined.
260
261 The following sections are informative.
262
264 Getting a Localized Date String
265 The following example first sets the locale to the user's default. The
266 locale information will be used in the nl_langinfo() and strftime()
267 functions. The nl_langinfo() function returns the localized date string
268 which specifies how the date is laid out. The strftime() function takes
269 this information and, using the tm structure for values, places the
270 date and time information into datestring.
271
272
273 #include <time.h>
274 #include <locale.h>
275 #include <langinfo.h>
276 ...
277 struct tm *tm;
278 char datestring[256];
279 ...
280 setlocale (LC_ALL, "");
281 ...
282 strftime (datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);
283 ...
284
286 The range of values for %S is [00,60] rather than [00,59] to allow for
287 the occasional leap second.
288
289 Some of the conversion specifications are duplicates of others. They
290 are included for compatibility with nl_cxtime() and nl_ascxtime(),
291 which were published in Issue 2.
292
293 Applications should use %Y (4-digit years) in preference to %y (2-digit
294 years).
295
296 In the C locale, the E and O modifiers are ignored and the replacement
297 strings for the following specifiers are:
298
299 %a The first three characters of %A .
300
301 %A One of Sunday, Monday, ..., Saturday.
302
303 %b The first three characters of %B .
304
305 %B One of January, February, ..., December.
306
307 %c Equivalent to %a %b %e %T %Y .
308
309 %p One of AM or PM.
310
311 %r Equivalent to %I : %M : %S %p .
312
313 %x Equivalent to %m / %d / %y .
314
315 %X Equivalent to %T .
316
317 %Z Implementation-defined.
318
319
321 None.
322
324 None.
325
327 asctime() , clock() , ctime() , difftime() , getdate() , gmtime() ,
328 localtime() , mktime() , strptime() , time() , tzset() , utime() , Base
329 Definitions volume of IEEE Std 1003.1-2001, Section 7.3.5, LC_TIME,
330 <time.h>
331
333 Portions of this text are reprinted and reproduced in electronic form
334 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
335 -- Portable Operating System Interface (POSIX), The Open Group Base
336 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
337 Electrical and Electronics Engineers, Inc and The Open Group. In the
338 event of any discrepancy between this version and the original IEEE and
339 The Open Group Standard, the original IEEE and The Open Group Standard
340 is the referee document. The original Standard can be obtained online
341 at http://www.opengroup.org/unix/online.html .
342
343
344
345IEEE/The Open Group 2003 STRFTIME(P)