OSDN Git Service

35f353991cf9bd5121c95412275ee263c0c1282c
[linuxjm/LDP_man-pages.git] / original / man3 / strptime.3
1 .\" Copyright 1993 Mitchum DSouza <m.dsouza@mrc-apu.cam.ac.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 .\" Modified, jmv@lucifer.dorms.spbu.ru, 1999-11-08
26 .\" Modified, aeb, 2000-04-07
27 .\" Updated from glibc docs, C. Scott Ananian, 2001-08-25
28 .\" Modified, aeb, 2001-08-31
29 .\" Modified, wharms 2001-11-12, remark on white space and example
30 .\"
31 .TH STRPTIME 3 2014-01-17 "GNU" "Linux Programmer's Manual"
32 .SH NAME
33 strptime \- convert a string representation of time to a time tm structure
34 .SH SYNOPSIS
35 .BR "#define _XOPEN_SOURCE" "       /* See feature_test_macros(7) */"
36 .br
37 .B #include <time.h>
38 .sp
39 .BI "char *strptime(const char *" s ", const char *" format ,
40 .BI "struct tm *" tm );
41 .SH DESCRIPTION
42 The
43 .BR strptime ()
44 function is the converse of
45 .BR strftime (3);
46 it converts the character string pointed to by
47 .I s
48 to values which are stored in the
49 "broken-down time"
50 structure pointed to by
51 .IR tm ,
52 using the format specified by
53 .IR format .
54
55 The broken-down time structure
56 .I tm
57 is defined in
58 .IR <time.h>
59 as follows:
60 .sp
61 .in +4n
62 .nf
63 struct tm {
64     int tm_sec;    /* Seconds (0-60) */
65     int tm_min;    /* Minutes (0-59) */
66     int tm_hour;   /* Hours (0-23) */
67     int tm_mday;   /* Day of the month (1-31) */
68     int tm_mon;    /* Month (0-11) */
69     int tm_year;   /* Year - 1900 */
70     int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
71     int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
72     int tm_isdst;  /* Daylight saving time */
73 };
74 .fi
75 .in
76
77 For more details on the
78 .I tm
79 structure, see
80 .BR ctime (3).
81
82 The
83 .I format
84 argument
85 is a character string that consists of field descriptors and text characters,
86 reminiscent of
87 .BR scanf (3).
88 Each field descriptor consists of a
89 .B %
90 character followed by another character that specifies the replacement
91 for the field descriptor.
92 All other characters in the
93 .I format
94 string must have a matching character in the input string,
95 except for whitespace, which matches zero or more
96 whitespace characters in the input string.
97 There should be white\%space or other alphanumeric characters
98 between any two field descriptors.
99 .PP
100 The
101 .BR strptime ()
102 function processes the input string from left
103 to right.
104 Each of the three possible input elements (whitespace,
105 literal, or format) are handled one after the other.
106 If the input cannot be matched to the format string, the function stops.
107 The remainder of the format and input strings are not processed.
108 .PP
109 The supported input field descriptors are listed below.
110 In case a text string (such as the name of a day of the week or a month name)
111 is to be matched, the comparison is case insensitive.
112 In case a number is to be matched, leading zeros are
113 permitted but not required.
114 .TP
115 .B %%
116 The
117 .B %
118 character.
119 .TP
120 .BR %a " or " %A
121 The name of the day of the week according to the current locale,
122 in abbreviated form or the full name.
123 .TP
124 .BR %b " or " %B " or " %h
125 The month name according to the current locale,
126 in abbreviated form or the full name.
127 .TP
128 .B %c
129 The date and time representation for the current locale.
130 .TP
131 .B %C
132 The century number (0-99).
133 .TP
134 .BR %d " or " %e
135 The day of month (1-31).
136 .TP
137 .B %D
138 Equivalent to
139 .BR %m/%d/%y .
140 (This is the American style date, very confusing
141 to non-Americans, especially since
142 .B %d/%m/%y
143 is widely used in Europe.
144 The ISO 8601 standard format is
145 .BR %Y-%m-%d .)
146 .TP
147 .B %H
148 The hour (0-23).
149 .TP
150 .B %I
151 The hour on a 12-hour clock (1-12).
152 .TP
153 .B %j
154 The day number in the year (1-366).
155 .TP
156 .B %m
157 The month number (1-12).
158 .TP
159 .B %M
160 The minute (0-59).
161 .TP
162 .B %n
163 Arbitrary whitespace.
164 .TP
165 .B %p
166 The locale's equivalent of AM or PM.
167 (Note: there may be none.)
168 .TP
169 .B %r
170 The 12-hour clock time (using the locale's AM or PM).
171 In the POSIX locale equivalent to
172 .BR "%I:%M:%S %p" .
173 If
174 .I t_fmt_ampm
175 is empty in the
176 .B LC_TIME
177 part of the current locale,
178 then the behavior is undefined.
179 .TP
180 .B %R
181 Equivalent to
182 .BR %H:%M .
183 .TP
184 .B %S
185 The second (0-60; 60 may occur for leap seconds;
186 earlier also 61 was allowed).
187 .TP
188 .B %t
189 Arbitrary whitespace.
190 .TP
191 .B %T
192 Equivalent to
193 .BR %H:%M:%S .
194 .TP
195 .B %U
196 The week number with Sunday the first day of the week (0-53).
197 The first Sunday of January is the first day of week 1.
198 .TP
199 .B %w
200 The ordinal number of the day of the week (0-6), with Sunday = 0.
201 .TP
202 .B %W
203 The week number with Monday the first day of the week (0-53).
204 The first Monday of January is the first day of week 1.
205 .TP
206 .B %x
207 The date, using the locale's date format.
208 .TP
209 .B %X
210 The time, using the locale's time format.
211 .TP
212 .B %y
213 The year within century (0-99).
214 When a century is not otherwise specified, values in the range 69-99 refer
215 to years in the twentieth century (1969-1999); values in the
216 range 00-68 refer to years in the twenty-first century (2000-2068).
217 .TP
218 .B %Y
219 The year, including century (for example, 1991).
220 .LP
221 Some field descriptors can be modified by the E or O modifier characters
222 to indicate that an alternative format or specification should be used.
223 If the
224 alternative format or specification does not exist in the current locale, the
225 unmodified field descriptor is used.
226 .LP
227 The E modifier specifies that the input string may contain
228 alternative locale-dependent versions of the date and time representation:
229 .TP
230 .B %Ec
231 The locale's alternative date and time representation.
232 .TP
233 .B %EC
234 The name of the base year (period) in the locale's alternative representation.
235 .TP
236 .B %Ex
237 The locale's alternative date representation.
238 .TP
239 .B %EX
240 The locale's alternative time representation.
241 .TP
242 .B %Ey
243 The offset from
244 .B %EC
245 (year only) in the locale's alternative representation.
246 .TP
247 .B %EY
248 The full alternative year representation.
249 .LP
250 The O modifier specifies that the numerical input may be in an
251 alternative locale-dependent format:
252 .TP
253 .BR %Od " or " %Oe
254 The day of the month using the locale's alternative numeric symbols;
255 leading zeros are permitted but not required.
256 .TP
257 .B %OH
258 The hour (24-hour clock) using the locale's alternative numeric symbols.
259 .TP
260 .B %OI
261 The hour (12-hour clock) using the locale's alternative numeric symbols.
262 .TP
263 .B %Om
264 The month using the locale's alternative numeric symbols.
265 .TP
266 .B %OM
267 The minutes using the locale's alternative numeric symbols.
268 .TP
269 .B %OS
270 The seconds using the locale's alternative numeric symbols.
271 .TP
272 .B %OU
273 The week number of the year (Sunday as the first day of the week)
274 using the locale's alternative numeric symbols.
275 .TP
276 .B %Ow
277 The ordinal number of the day of the week (Sunday=0),
278  using the locale's alternative numeric symbols.
279 .TP
280 .B %OW
281 The week number of the year (Monday as the first day of the week)
282 using the locale's alternative numeric symbols.
283 .TP
284 .B %Oy
285 The year (offset from
286 .BR %C )
287 using the locale's alternative numeric symbols.
288 .SH RETURN VALUE
289 The return value of the function is a pointer to the first character
290 not processed in this function call.
291 In case the input string
292 contains more characters than required by the format string, the return
293 value points right after the last consumed input character.
294 In case
295 the whole input string is consumed, the return value points to the null
296 byte at the end of the string.
297 If
298 .BR strptime ()
299 fails to match all
300 of the format string and therefore an error occurred, the function
301 returns NULL.
302 .SH CONFORMING TO
303 SUSv2, POSIX.1-2001.
304 .SH NOTES
305 .LP
306 In principle, this function does not initialize
307 .I tm
308 but
309 stores only the values specified.
310 This means that
311 .I tm
312 should be initialized before the call.
313 Details differ a bit between different UNIX systems.
314 The glibc implementation does not touch those fields which are not
315 explicitly specified, except that it recomputes the
316 .I tm_wday
317 and
318 .I tm_yday
319 field if any of the year, month, or day elements changed.
320 .PP
321 This function is available since libc 4.6.8.
322 Linux libc4 and libc5 includes define the prototype unconditionally;
323 glibc2 includes provide a prototype only when
324 .B _XOPEN_SOURCE
325 or
326 .B _GNU_SOURCE
327 are defined.
328 .PP
329 Before libc 5.4.13 whitespace
330 (and the \(aqn\(aq and \(aqt\(aq specifications) was not handled,
331 no \(aqE\(aq and \(aqO\(aq locale modifier characters were accepted,
332 and the \(aqC\(aq specification was a synonym for the \(aqc\(aq specification.
333 .PP
334 The \(aqy\(aq (year in century) specification is taken to specify a year
335 in the 20th century by libc4 and libc5.
336 It is taken to be a year
337 in the range 1950-2049 by glibc 2.0.
338 It is taken to be a year in
339 1969-2068 since glibc 2.1.
340 .\" In libc4 and libc5 the code for %I is broken (fixed in glibc;
341 .\" %OI was fixed in glibc 2.2.4).
342 .SS Glibc notes
343 For reasons of symmetry, glibc tries to support for
344 .BR strptime ()
345 the same format characters as for
346 .BR strftime (3).
347 (In most cases, the corresponding fields are parsed, but no field in
348 .I tm
349 is changed.)
350 This leads to
351 .TP
352 .B %F
353 Equivalent to
354 .BR %Y-%m-%d ,
355 the ISO 8601 date format.
356 .TP
357 .B %g
358 The year corresponding to the ISO week number, but without the century
359 (0-99).
360 .TP
361 .B %G
362 The year corresponding to the ISO week number.
363 (For example, 1991.)
364 .TP
365 .B %u
366 The day of the week as a decimal number (1-7, where Monday = 1).
367 .TP
368 .B %V
369 The ISO 8601:1988 week number as a decimal number (1-53).
370 If the week (starting on Monday) containing 1 January has four or more days
371 in the new year, then it is considered week 1.
372 Otherwise, it is the last week
373 of the previous year, and the next week is week 1.
374 .TP
375 .B %z
376 An RFC-822/ISO 8601 standard timezone specification.
377 .TP
378 .B %Z
379 The timezone name.
380 .LP
381 Similarly, because of GNU extensions to
382 .BR strftime (3),
383 .B %k
384 is accepted as a synonym for
385 .BR %H ,
386 and
387 .B %l
388 should be accepted
389 as a synonym for
390 .BR %I ,
391 and
392 .B %P
393 is accepted as a synonym for
394 .BR %p .
395 Finally
396 .TP
397 .B %s
398 The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
399 Leap seconds are not counted unless leap second support is available.
400 .LP
401 The glibc implementation does not require whitespace between
402 two field descriptors.
403 .SH EXAMPLE
404 The following example demonstrates the use of
405 .BR strptime ()
406 and
407 .BR strftime (3).
408 .sp
409 .nf
410 #define _XOPEN_SOURCE
411 #include <stdio.h>
412 #include <stdlib.h>
413 #include <string.h>
414 #include <time.h>
415
416 int
417 main(void)
418 {
419     struct tm tm;
420     char buf[255];
421
422     memset(&tm, 0, sizeof(struct tm));
423     strptime("2001\-11\-12 18:31:01", "%Y\-%m\-%d %H:%M:%S", &tm);
424     strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
425     puts(buf);
426     exit(EXIT_SUCCESS);
427 }
428 .fi
429 .SH SEE ALSO
430 .BR time (2),
431 .BR getdate (3),
432 .BR scanf (3),
433 .BR setlocale (3),
434 .BR strftime (3)
435 .SH COLOPHON
436 This page is part of release 3.64 of the Linux
437 .I man-pages
438 project.
439 A description of the project,
440 and information about reporting bugs,
441 can be found at
442 \%http://www.kernel.org/doc/man\-pages/.