OSDN Git Service

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