1STRPTIME(3) Linux Programmer's Manual STRPTIME(3)
2
3
4
6 strptime - convert a string representation of time to a time tm struc‐
7 ture
8
10 #define _XOPEN_SOURCE /* See feature_test_macros(7) */
11 #include <time.h>
12
13 char *strptime(const char *s, const char *format, struct tm *tm);
14
16 The strptime() function is the converse of strftime(3); it converts the
17 character string pointed to by s to values which are stored in the
18 "broken-down time" structure pointed to by tm, using the format speci‐
19 fied by format.
20
21 The broken-down time structure tm is defined in <time.h> as follows:
22
23 struct tm {
24 int tm_sec; /* Seconds (0-60) */
25 int tm_min; /* Minutes (0-59) */
26 int tm_hour; /* Hours (0-23) */
27 int tm_mday; /* Day of the month (1-31) */
28 int tm_mon; /* Month (0-11) */
29 int tm_year; /* Year - 1900 */
30 int tm_wday; /* Day of the week (0-6, Sunday = 0) */
31 int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
32 int tm_isdst; /* Daylight saving time */
33 };
34
35 For more details on the tm structure, see ctime(3).
36
37 The format argument is a character string that consists of field
38 descriptors and text characters, reminiscent of scanf(3). Each field
39 descriptor consists of a % character followed by another character that
40 specifies the replacement for the field descriptor. All other charac‐
41 ters in the format string must have a matching character in the input
42 string, except for whitespace, which matches zero or more whitespace
43 characters in the input string. There should be whitespace or other
44 alphanumeric characters between any two field descriptors.
45
46 The strptime() function processes the input string from left to right.
47 Each of the three possible input elements (whitespace, literal, or for‐
48 mat) are handled one after the other. If the input cannot be matched
49 to the format string, the function stops. The remainder of the format
50 and input strings are not processed.
51
52 The supported input field descriptors are listed below. In case a text
53 string (such as the name of a day of the week or a month name) is to be
54 matched, the comparison is case insensitive. In case a number is to be
55 matched, leading zeros are permitted but not required.
56
57 %% The % character.
58
59 %a or %A
60 The name of the day of the week according to the current locale,
61 in abbreviated form or the full name.
62
63 %b or %B or %h
64 The month name according to the current locale, in abbreviated
65 form or the full name.
66
67 %c The date and time representation for the current locale.
68
69 %C The century number (0–99).
70
71 %d or %e
72 The day of month (1–31).
73
74 %D Equivalent to %m/%d/%y. (This is the American style date, very
75 confusing to non-Americans, especially since %d/%m/%y is widely
76 used in Europe. The ISO 8601 standard format is %Y-%m-%d.)
77
78 %H The hour (0–23).
79
80 %I The hour on a 12-hour clock (1–12).
81
82 %j The day number in the year (1–366).
83
84 %m The month number (1–12).
85
86 %M The minute (0–59).
87
88 %n Arbitrary whitespace.
89
90 %p The locale's equivalent of AM or PM. (Note: there may be none.)
91
92 %r The 12-hour clock time (using the locale's AM or PM). In the
93 POSIX locale equivalent to %I:%M:%S %p. If t_fmt_ampm is empty
94 in the LC_TIME part of the current locale, then the behavior is
95 undefined.
96
97 %R Equivalent to %H:%M.
98
99 %S The second (0–60; 60 may occur for leap seconds; earlier also 61
100 was allowed).
101
102 %t Arbitrary whitespace.
103
104 %T Equivalent to %H:%M:%S.
105
106 %U The week number with Sunday the first day of the week (0–53).
107 The first Sunday of January is the first day of week 1.
108
109 %w The ordinal number of the day of the week (0–6), with Sunday =
110 0.
111
112 %W The week number with Monday the first day of the week (0–53).
113 The first Monday of January is the first day of week 1.
114
115 %x The date, using the locale's date format.
116
117 %X The time, using the locale's time format.
118
119 %y The year within century (0–99). When a century is not otherwise
120 specified, values in the range 69–99 refer to years in the twen‐
121 tieth century (1969–1999); values in the range 00–68 refer to
122 years in the twenty-first century (2000–2068).
123
124 %Y The year, including century (for example, 1991).
125
126 Some field descriptors can be modified by the E or O modifier charac‐
127 ters to indicate that an alternative format or specification should be
128 used. If the alternative format or specification does not exist in the
129 current locale, the unmodified field descriptor is used.
130
131 The E modifier specifies that the input string may contain alternative
132 locale-dependent versions of the date and time representation:
133
134 %Ec The locale's alternative date and time representation.
135
136 %EC The name of the base year (period) in the locale's alternative
137 representation.
138
139 %Ex The locale's alternative date representation.
140
141 %EX The locale's alternative time representation.
142
143 %Ey The offset from %EC (year only) in the locale's alternative rep‐
144 resentation.
145
146 %EY The full alternative year representation.
147
148 The O modifier specifies that the numerical input may be in an alterna‐
149 tive locale-dependent format:
150
151 %Od or %Oe
152 The day of the month using the locale's alternative numeric sym‐
153 bols; leading zeros are permitted but not required.
154
155 %OH The hour (24-hour clock) using the locale's alternative numeric
156 symbols.
157
158 %OI The hour (12-hour clock) using the locale's alternative numeric
159 symbols.
160
161 %Om The month using the locale's alternative numeric symbols.
162
163 %OM The minutes using the locale's alternative numeric symbols.
164
165 %OS The seconds using the locale's alternative numeric symbols.
166
167 %OU The week number of the year (Sunday as the first day of the
168 week) using the locale's alternative numeric symbols.
169
170 %Ow The ordinal number of the day of the week (Sunday=0),
171 using the locale's alternative numeric symbols.
172
173 %OW The week number of the year (Monday as the first day of the
174 week) using the locale's alternative numeric symbols.
175
176 %Oy The year (offset from %C) using the locale's alternative numeric
177 symbols.
178
180 The return value of the function is a pointer to the first character
181 not processed in this function call. In case the input string contains
182 more characters than required by the format string, the return value
183 points right after the last consumed input character. In case the
184 whole input string is consumed, the return value points to the null
185 byte at the end of the string. If strptime() fails to match all of the
186 format string and therefore an error occurred, the function returns
187 NULL.
188
190 For an explanation of the terms used in this section, see
191 attributes(7).
192
193 ┌───────────┬───────────────┬────────────────────┐
194 │Interface │ Attribute │ Value │
195 ├───────────┼───────────────┼────────────────────┤
196 │strptime() │ Thread safety │ MT-Safe env locale │
197 └───────────┴───────────────┴────────────────────┘
199 POSIX.1-2001, POSIX.1-2008, SUSv2.
200
202 In principle, this function does not initialize tm but stores only the
203 values specified. This means that tm should be initialized before the
204 call. Details differ a bit between different UNIX systems. The glibc
205 implementation does not touch those fields which are not explicitly
206 specified, except that it recomputes the tm_wday and tm_yday field if
207 any of the year, month, or day elements changed.
208
209 The 'y' (year in century) specification is taken to specify a year in
210 the range 1950–2049 by glibc 2.0. It is taken to be a year in
211 1969–2068 since glibc 2.1.
212
213 Glibc notes
214 For reasons of symmetry, glibc tries to support for strptime() the same
215 format characters as for strftime(3). (In most cases, the correspond‐
216 ing fields are parsed, but no field in tm is changed.) This leads to
217
218 %F Equivalent to %Y-%m-%d, the ISO 8601 date format.
219
220 %g The year corresponding to the ISO week number, but without the
221 century (0–99).
222
223 %G The year corresponding to the ISO week number. (For example,
224 1991.)
225
226 %u The day of the week as a decimal number (1–7, where Monday = 1).
227
228 %V The ISO 8601:1988 week number as a decimal number (1–53). If
229 the week (starting on Monday) containing 1 January has four or
230 more days in the new year, then it is considered week 1. Other‐
231 wise, it is the last week of the previous year, and the next
232 week is week 1.
233
234 %z An RFC-822/ISO 8601 standard timezone specification.
235
236 %Z The timezone name.
237
238 Similarly, because of GNU extensions to strftime(3), %k is accepted as
239 a synonym for %H, and %l should be accepted as a synonym for %I, and %P
240 is accepted as a synonym for %p. Finally
241
242 %s The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000
243 (UTC). Leap seconds are not counted unless leap second support
244 is available.
245
246 The glibc implementation does not require whitespace between two field
247 descriptors.
248
250 The following example demonstrates the use of strptime() and strf‐
251 time(3).
252
253 #define _XOPEN_SOURCE
254 #include <stdio.h>
255 #include <stdlib.h>
256 #include <string.h>
257 #include <time.h>
258
259 int
260 main(void)
261 {
262 struct tm tm;
263 char buf[255];
264
265 memset(&tm, 0, sizeof(struct tm));
266 strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
267 strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
268 puts(buf);
269 exit(EXIT_SUCCESS);
270 }
271
273 time(2), getdate(3), scanf(3), setlocale(3), strftime(3)
274
276 This page is part of release 5.02 of the Linux man-pages project. A
277 description of the project, information about reporting bugs, and the
278 latest version of this page, can be found at
279 https://www.kernel.org/doc/man-pages/.
280
281
282
283GNU 2017-09-15 STRPTIME(3)