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