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