]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strftime.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[thirdparty/man-pages.git] / man3 / strftime.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(verbatim)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" 386BSD man pages
29 .\" GNU texinfo documentation on glibc date/time functions.
30 .\" Modified Sat Jul 24 18:03:44 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Applied fix by Wolfgang Franke, aeb, 961011
32 .\" Corrected return value, aeb, 970307
33 .\" Added Single UNIX Spec conversions and %z, aeb/esr, 990329.
34 .\" 2005-11-22 mtk, added Glibc Notes covering optional 'flag' and
35 .\" 'width' components of conversion specifications.
36 .\"
37 .TH STRFTIME 3 2012-05-10 "GNU" "Linux Programmer's Manual"
38 .SH NAME
39 strftime \- format date and time
40 .SH SYNOPSIS
41 .nf
42 .B #include <time.h>
43 .sp
44 .BI "size_t strftime(char *" s ", size_t " max ", const char *" format ,
45 .BI " const struct tm *" tm );
46 .fi
47 .SH DESCRIPTION
48 The
49 .BR strftime ()
50 function formats the broken-down time \fItm\fP
51 according to the format specification \fIformat\fP and places the
52 result in the character array \fIs\fP of size \fImax\fP.
53 .\" FIXME POSIX says: Local timezone information is used as though
54 .\" strftime() called tzset(). But this doesn't appear to be the case
55 .PP
56 The format specification is a null-terminated string and may contain
57 special character sequences called
58 .IR "conversion specifications",
59 each of which is introduced by a \(aq%\(aq character and terminated by
60 some other character known as a
61 .IR "conversion specifier character".
62 All other character sequences are
63 .IR "ordinary character sequences".
64 .PP
65 The characters of ordinary character sequences (including the null byte)
66 are copied verbatim from \fIformat\fP to \fIs\fP.
67 However, the characters
68 of conversion specifications are replaced as follows:
69 .TP
70 .B %a
71 The abbreviated weekday name according to the current locale.
72 .TP
73 .B %A
74 The full weekday name according to the current locale.
75 .TP
76 .B %b
77 The abbreviated month name according to the current locale.
78 .TP
79 .B %B
80 The full month name according to the current locale.
81 .TP
82 .B %c
83 The preferred date and time representation for the current locale.
84 .TP
85 .B %C
86 The century number (year/100) as a 2-digit integer. (SU)
87 .TP
88 .B %d
89 The day of the month as a decimal number (range 01 to 31).
90 .TP
91 .B %D
92 Equivalent to
93 .BR %m/%d/%y .
94 (Yecch\(emfor Americans only.
95 Americans should note that in other countries
96 .B %d/%m/%y
97 is rather common.
98 This means that in international context this format is
99 ambiguous and should not be used.) (SU)
100 .TP
101 .B %e
102 Like
103 .BR %d ,
104 the day of the month as a decimal number, but a leading
105 zero is replaced by a space. (SU)
106 .TP
107 .B %E
108 Modifier: use alternative format, see below. (SU)
109 .TP
110 .B %F
111 Equivalent to
112 .B %Y-%m-%d
113 (the ISO\ 8601 date format). (C99)
114 .TP
115 .B %G
116 The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
117 The 4-digit year corresponding to the ISO week number (see
118 .BR %V ).
119 This has the same format and value as
120 .BR %Y ,
121 except that if the ISO week number belongs to the previous or next year,
122 that year is used instead. (TZ)
123 .TP
124 .B %g
125 Like
126 .BR %G ,
127 but without century, that is, with a 2-digit year (00-99). (TZ)
128 .TP
129 .B %h
130 Equivalent to
131 .BR %b .
132 (SU)
133 .TP
134 .B %H
135 The hour as a decimal number using a 24-hour clock (range 00 to 23).
136 .TP
137 .B %I
138 The hour as a decimal number using a 12-hour clock (range 01 to 12).
139 .TP
140 .B %j
141 The day of the year as a decimal number (range 001 to 366).
142 .TP
143 .B %k
144 The hour (24-hour clock) as a decimal number (range 0 to 23);
145 single digits are preceded by a blank.
146 (See also
147 .BR %H .)
148 (TZ)
149 .TP
150 .B %l
151 The hour (12-hour clock) as a decimal number (range 1 to 12);
152 single digits are preceded by a blank.
153 (See also
154 .BR %I .)
155 (TZ)
156 .TP
157 .B %m
158 The month as a decimal number (range 01 to 12).
159 .TP
160 .B %M
161 The minute as a decimal number (range 00 to 59).
162 .TP
163 .B %n
164 A newline character. (SU)
165 .TP
166 .B %O
167 Modifier: use alternative format, see below. (SU)
168 .TP
169 .B %p
170 Either "AM" or "PM" according to the given time value, or the
171 corresponding strings for the current locale.
172 Noon is treated as "PM" and midnight as "AM".
173 .TP
174 .B %P
175 Like
176 .B %p
177 but in lowercase: "am" or "pm" or a corresponding
178 string for the current locale. (GNU)
179 .TP
180 .B %r
181 The time in a.m. or p.m. notation.
182 In the POSIX locale this is equivalent to
183 .BR "%I:%M:%S %p" .
184 (SU)
185 .TP
186 .B %R
187 The time in 24-hour notation (\fB%H:%M\fP). (SU)
188 For a version including the seconds, see
189 .B %T
190 below.
191 .TP
192 .B %s
193 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
194 .TP
195 .B %S
196 The second as a decimal number (range 00 to 60).
197 (The range is up to 60 to allow for occasional leap seconds.)
198 .TP
199 .B %t
200 A tab character. (SU)
201 .TP
202 .B %T
203 The time in 24-hour notation (\fB%H:%M:%S\fP). (SU)
204 .TP
205 .B %u
206 The day of the week as a decimal, range 1 to 7, Monday being 1.
207 See also
208 .BR %w .
209 (SU)
210 .TP
211 .B %U
212 The week number of the current year as a decimal number,
213 range 00 to 53, starting with the first Sunday as the first day
214 of week 01.
215 See also
216 .B %V
217 and
218 .BR %W .
219 .TP
220 .B %V
221 The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
222 range 01 to 53, where week 1 is the first week that has at least
223 4 days in the new year.
224 See also
225 .B %U
226 and
227 .BR %W .
228 (SU)
229 .TP
230 .B %w
231 The day of the week as a decimal, range 0 to 6, Sunday being 0.
232 See also
233 .BR %u .
234 .TP
235 .B %W
236 The week number of the current year as a decimal number,
237 range 00 to 53, starting with the first Monday as the first day of week 01.
238 .TP
239 .B %x
240 The preferred date representation for the current locale without the time.
241 .TP
242 .B %X
243 The preferred time representation for the current locale without the date.
244 .TP
245 .B %y
246 The year as a decimal number without a century (range 00 to 99).
247 .TP
248 .B %Y
249 The year as a decimal number including the century.
250 .TP
251 .B %z
252 The
253 .I +hhmm
254 or
255 .I -hhmm
256 numeric timezone (that is, the hour and minute offset from UTC). (SU)
257 .TP
258 .B %Z
259 The timezone or name or abbreviation.
260 .TP
261 .B %+
262 .\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
263 .\" their man pages)
264 The date and time in
265 .BR date (1)
266 format. (TZ)
267 (Not supported in glibc2.)
268 .TP
269 .B %%
270 A literal \(aq%\(aq character.
271 .PP
272 Some conversion specifications can be modified by preceding the
273 conversion specifier character by the
274 .B E
275 or
276 .B O
277 .I modifier
278 to indicate that an alternative format should be used.
279 If the alternative format or specification does not exist for
280 the current locale, the behavior will be as if the unmodified
281 conversion specification were used. (SU)
282 The Single UNIX Specification mentions
283 .BR %Ec ,
284 .BR %EC ,
285 .BR %Ex ,
286 .BR %EX ,
287 .BR %Ey ,
288 .BR %EY ,
289 .BR %Od ,
290 .BR %Oe ,
291 .BR %OH ,
292 .BR %OI ,
293 .BR %Om ,
294 .BR %OM ,
295 .BR %OS ,
296 .BR %Ou ,
297 .BR %OU ,
298 .BR %OV ,
299 .BR %Ow ,
300 .BR %OW ,
301 .BR %Oy ,
302 where the effect of the
303 .B O
304 modifier is to use
305 alternative numeric symbols (say, roman numerals), and that of the
306 E modifier is to use a locale-dependent alternative representation.
307 .PP
308 The broken-down time structure \fItm\fP is defined in \fI<time.h>\fP.
309 See also
310 .BR ctime (3).
311 .SH RETURN VALUE
312 The
313 .BR strftime ()
314 function returns the number of bytes placed
315 in the array \fIs\fP, not including the terminating null byte,
316 provided the string, including the terminating null byte, fits.
317 Otherwise, it returns 0, and the contents of the array is undefined.
318 (This behavior applies since at least libc 4.4.4;
319 very old versions of libc, such as libc 4.4.1,
320 would return \fImax\fP if the array was too small.)
321 .LP
322 Note that the return value 0 does not necessarily indicate an error;
323 for example, in many locales
324 .B %p
325 yields an empty string.
326 .SH ENVIRONMENT
327 The environment variables
328 .B TZ
329 and
330 .B LC_TIME
331 are used.
332 .SH CONFORMING TO
333 SVr4, C89, C99.
334 There are strict inclusions between the set of conversions
335 given in ANSI C (unmarked), those given in the Single UNIX Specification
336 (marked SU), those given in Olson's timezone package (marked TZ),
337 and those given in glibc (marked GNU), except that
338 .B %+
339 is not supported in glibc2.
340 On the other hand glibc2 has several more extensions.
341 POSIX.1 only refers to ANSI C; POSIX.2 describes under
342 .BR date (1)
343 several extensions that could apply to
344 .BR strftime ()
345 as well.
346 The
347 .B %F
348 conversion is in C99 and POSIX.1-2001.
349
350 In SUSv2, the
351 .B %S
352 specifier allowed a range of 00 to 61,
353 to allow for the theoretical possibility of a minute that
354 included a double leap second
355 (there never has been such a minute).
356 .SH NOTES
357 .SS ISO 8601 week dates
358 .BR %G ,
359 .BR %g ,
360 and
361 .BR %V
362 yield values calculated from the week-based year defined by the
363 ISO\ 8601 standard.
364 In this system, weeks start on a Monday, and are numbered from 01,
365 for the first week, up to 52 or 53, for the last week.
366 Week 1 is the first week where four or more days fall within the
367 new year (or, synonymously, week 01 is:
368 the first week of the year that contains a Thursday;
369 or, the week that has 4 January in it).
370 When three of fewer days of the first calendar week of the new year fall
371 within that year,
372 then the ISO 8601 week-based system counts those days as part of week 53
373 of the preceding year.
374 For example, 1 January 2010 is a Friday,
375 meaning that just three days of that calendar week fall in 2010.
376 Thus, the ISO\ 8601 week-based system considers these days to be part of
377 week 53 (\fB%V\fP) of the year 2009 (\fB%G\fP) ;
378 week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
379 .SS Glibc notes
380 Glibc provides some extensions for conversion specifications.
381 (These extensions are not specified in POSIX.1-2001, but a few other
382 systems provide similar features.)
383 .\" HP-UX and Tru64 also have features like this.
384 Between the \(aq%\(aq character and the conversion specifier character,
385 an optional
386 .I flag
387 and field
388 .I width
389 may be specified.
390 (These precede the
391 .B E
392 or
393 .B O
394 modifiers, if present.)
395
396 The following flag characters are permitted:
397 .TP
398 .B _
399 (underscore)
400 Pad a numeric result string with spaces.
401 .TP
402 .B \-
403 (dash)
404 Do not pad a numeric result string.
405 .TP
406 .B 0
407 Pad a numeric result string with zeros even if the conversion
408 specifier character uses space-padding by default.
409 .TP
410 .B ^
411 Convert alphabetic characters in result string to upper case.
412 .TP
413 .B #
414 Swap the case of the result string.
415 (This flag only works with certain conversion specifier characters,
416 and of these, it is only really useful with
417 .BR %Z .)
418 .PP
419 An optional decimal width specifier may follow the (possibly absent) flag.
420 If the natural size of the field is smaller than this width,
421 then the result string is padded (on the left) to the specified width.
422 .SH BUGS
423 Some buggy versions of
424 .BR gcc (1)
425 complain about the use of
426 .BR %c :
427 .IR "warning: `%c' yields only last 2 digits of year in some locales" .
428 Of course programmers are encouraged to use
429 .BR %c ,
430 it gives the preferred date and time representation.
431 One meets all kinds of strange obfuscations
432 to circumvent this
433 .BR gcc (1)
434 problem.
435 A relatively clean one is to add an
436 intermediate function
437 .in +4n
438 .nf
439
440 size_t
441 my_strftime(char *s, size_t max, const char *fmt,
442 const struct tm *tm)
443 {
444 return strftime(s, max, fmt, tm);
445 }
446 .fi
447 .in
448
449 Nowadays,
450 .BR gcc (1)
451 provides the \fI\-Wno\-format\-y2k\fP option to prevent the warning,
452 so that the above workaround is no longer required.
453 .SH EXAMPLE
454 .BR "RFC\ 2822-compliant date format"
455 (with an English locale for %a and %b)
456 .PP
457 .in +2n
458 "%a,\ %d\ %b\ %Y\ %T\ %z"
459 .PP
460 .BR "RFC\ 822-compliant date format"
461 (with an English locale for %a and %b)
462 .PP
463 .in +2n
464 "%a,\ %d\ %b\ %y\ %T\ %z"
465 .SS Example program
466 The program below can be used to experiment with
467 .BR strftime ().
468 .PP
469 Some examples of the result string produced by the glibc implementation of
470 .BR strftime ()
471 are as follows:
472 .in +4n
473 .nf
474
475 .RB "$" " ./a.out \(aq%m\(aq"
476 Result string is "11"
477 .RB "$" " ./a.out \(aq%5m\(aq"
478 Result string is "00011"
479 .RB "$" " ./a.out \(aq%_5m\(aq"
480 Result string is " 11"
481 .fi
482 .in
483 .PP
484 Here's the program source:
485 .nf
486
487 #include <time.h>
488 #include <stdio.h>
489 #include <stdlib.h>
490
491 int
492 main(int argc, char *argv[])
493 {
494 char outstr[200];
495 time_t t;
496 struct tm *tmp;
497
498 t = time(NULL);
499 tmp = localtime(&t);
500 if (tmp == NULL) {
501 perror("localtime");
502 exit(EXIT_FAILURE);
503 }
504
505 if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
506 fprintf(stderr, "strftime returned 0");
507 exit(EXIT_FAILURE);
508 }
509
510 printf("Result string is \\"%s\\"\\n", outstr);
511 exit(EXIT_SUCCESS);
512 }
513 .fi
514 .SH SEE ALSO
515 .BR date (1),
516 .BR time (2),
517 .BR ctime (3),
518 .BR setlocale (3),
519 .BR sprintf (3),
520 .BR strptime (3)