STRPTIME(3P)
๐ธ PROLOG ๐ธ
Okay bestie, this is strptime โ the STRPTIME(3P) page โ and first the fine print: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for the Linux-specific details), or the interface may not be implemented on Linux at all. ๐
๐ NAME ๐
strptime โ date and time conversion
โจ SYNOPSIS โจ
#include <time.h>
char *strptime(const char *restrict buf, const char *restrict format,
struct tm *restrict tm);
๐ฆ DESCRIPTION ๐ฆ
The strptime() function shall convert the character string pointed to by buf to values which are stored in the tm structure pointed to by tm, using the format specified by format. Like, it's literally taking a string and parsing it into a broken-down time struct. ๐
The format is composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters (as specified by isspace()); an ordinary character (neither '%' nor a white-space character); or a conversion specification. So think of directives as the little instruction units in your format string. โจ
Each conversion specification is introduced by the '%' character after which the following appear in sequence:
- An optional flag, the zero character (
'0') or the<plus-sign>character ('+'), which is ignored. - An optional field width. If a field width is specified, it shall be interpreted as a string of decimal digits that will determine the maximum number of bytes converted for the conversion rather than the number of bytes specified below in the description of the conversion specifiers.
- An optional
EorOmodifier. - A terminating conversion specifier character that indicates the type of conversion to be applied.
The conversions are determined using the LC_TIME category of the current locale. The application shall ensure that there is white-space or other non-alphanumeric characters between any two conversion specifications unless all of the adjacent conversion specifications convert a known, fixed number of characters. In the following list, the maximum number of characters scanned (excluding the one matching the next directive) is as follows:
- If a maximum field width is specified, then that number
- Otherwise, the pattern
"{x}"indicates that the maximum isx - Otherwise, the pattern
"[x,y]"indicates that the value shall fall within the range given (both bounds being inclusive), and the maximum number of characters scanned shall be the maximum required to represent any value in the range without leading zeros and without a leading<plus-sign>
The following conversion specifiers are supported.
Heads up: The results are unspecified if a modifier is specified with a flag or with a minimum field width, or if a field width is specified for any conversion specifier other thanCorY. So maybe don't do that. ๐
aโ The day of the week, using the locale's weekday names; either the abbreviated or full name may be specified.Aโ Equivalent to%a.bโ The month, using the locale's month names; either the abbreviated or full name may be specified.Bโ Equivalent to%b.cโ Replaced by the locale's appropriate date and time representation.Cโ All but the last two digits of the year{2}; leading zeros shall be permitted but shall not be required. A leading'+'or'-'character shall be permitted before any leading zeros but shall not be required.dโ The day of the month[01,31]; leading zeros shall be permitted but shall not be required.Dโ The date as%m/%d/%y.eโ Equivalent to%d.hโ Equivalent to%b.Hโ The hour (24-hour clock)[00,23]; leading zeros shall be permitted but shall not be required.Iโ The hour (12-hour clock)[01,12]; leading zeros shall be permitted but shall not be required.jโ The day number of the year[001,366]; leading zeros shall be permitted but shall not be required.mโ The month number[01,12]; leading zeros shall be permitted but shall not be required.Mโ The minute[00,59]; leading zeros shall be permitted but shall not be required.nโ Any white space.pโ The locale's equivalent of a.m. or p.m.rโ 12-hour clock time using the AM/PM notation ift_fmt_ampmis not an empty string in theLC_TIMEportion of the current locale; in the POSIX locale, this shall be equivalent to%I:%M:%S %p.Rโ The time as%H:%M.Sโ The seconds[00,60]; leading zeros shall be permitted but shall not be required.tโ Any white space.Tโ The time as%H:%M:%S.Uโ The week number of the year (Sunday as the first day of the week) as a decimal number[00,53]; leading zeros shall be permitted but shall not be required.wโ The weekday as a decimal number[0,6], with0representing Sunday.Wโ The week number of the year (Monday as the first day of the week) as a decimal number[00,53]; leading zeros shall be permitted but shall not be required.xโ The date, using the locale's date format.Xโ The time, using the locale's time format.yโ The last two digits of the year. Whenformatcontains neither aCconversion specifier nor aYconversion specifier, values in the range[69,99]shall refer to years 1969 to 1999 inclusive and values in the range[00,68]shall refer to years 2000 to 2068 inclusive; leading zeros shall be permitted but shall not be required. A leading'+'or'-'character shall be permitted before any leading zeros but shall not be required.
Note: It is expected that in a future version of this standard the default century inferred from a 2-digit year will change. (This would apply to all commands accepting a 2-digit year as input.)
Yโ The full year{4}; leading zeros shall be permitted but shall not be required. A leading'+'or'-'character shall be permitted before any leading zeros but shall not be required.%โ Replaced by%.
Modified Conversion Specifiers
Some conversion specifiers can be modified by the E and O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified conversion specifier. If the alternative format or specification does not exist in the current locale, the behavior shall be as if the unmodified conversion specification were used. So it's like a lil glow-up for your specifier, but only if your locale supports it. ๐
%Ecโ The locale's alternative appropriate date and time representation.%ECโ The name of the base year (period) in the locale's alternative representation.%Exโ The locale's alternative date representation.%EXโ The locale's alternative time representation.%Eyโ The offset from%EC(year only) in the locale's alternative representation.%EYโ The full alternative year representation.%Odโ The day of the month using the locale's alternative numeric symbols; leading zeros shall be permitted but shall not be required.%Oeโ Equivalent to%Od.%OHโ The hour (24-hour clock) using the locale's alternative numeric symbols.%OIโ The hour (12-hour clock) using the locale's alternative numeric symbols.%Omโ The month using the locale's alternative numeric symbols.%OMโ The minutes using the locale's alternative numeric symbols.%OSโ The seconds using the locale's alternative numeric symbols.%OUโ The week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols.%Owโ The number of the weekday (Sunday=0) using the locale's alternative numeric symbols.%OWโ The week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.%Oyโ The year (offset from%C) using the locale's alternative numeric symbols.
A conversion specification composed of white-space characters is executed by scanning input up to the first character that is not white-space (which remains unscanned), or until no more characters can be scanned.
A conversion specification that is an ordinary character is executed by scanning the next character from the buffer. If the character scanned from the buffer differs from the one comprising the directive, the directive fails, and the differing and subsequent characters remain unscanned.
A series of conversion specifications composed of %n, %t, white-space characters, or any combination is executed by scanning up to the first character that is not white space (which remains unscanned), or until no more characters can be scanned.
Any other conversion specification is executed by scanning characters until a character matching the next directive is scanned, or until no more characters can be scanned. These characters, except the one matching the next directive, are then compared to the locale values associated with the conversion specifier. If a match is found, values for the appropriate tm structure members are set to values corresponding to the locale information. Case is ignored when matching items in buf such as month or weekday names. If no match is found, strptime() fails and no more characters are scanned.
๐ RETURN VALUE ๐
Upon successful completion, strptime() shall return a pointer to the character following the last character parsed. Otherwise, a null pointer shall be returned. So basically: pointer = yay, null = nope. ๐ซถ
๐ ERRORS ๐
No errors are defined.
The following sections are informative.
๐ EXAMPLES ๐
Convert a Date-Plus-Time String to Broken-Down Time and Then into Seconds
The following example demonstrates the use of strptime() to convert a string into broken-down time. The broken-down time is then converted into seconds since the Epoch using mktime().
#include <time.h>
...
struct tm tm;
time_t t;
if (strptime("6 Dec 2001 12:33:45", "%d %b %Y %H:%M:%S", &tm) == NULL)
/* Handle error */;
printf("year: %d; month: %d; day: %d;\n",
tm.tm_year, tm.tm_mon, tm.tm_mday);
printf("hour: %d; minute: %d; second: %d\n",
tm.tm_hour, tm.tm_min, tm.tm_sec);
printf("week day: %d; year day: %d\n", tm.tm_wday, tm.tm_yday);
tm.tm_isdst = -1; /* Not set by strptime(); tells mktime()
to determine whether daylight saving time
is in effect */
t = mktime(&tm);
if (t == -1)
/* Handle error */;
printf("seconds since the Epoch: %ld\n", (long) t);"
โจ APPLICATION USAGE โจ
okay so bestie, here's the tea on why the format strings are Like That ๐ซถ
There's a whole bunch of "``equivalent to''" formats, plus some special little โจmagicโจ where whitespace characters get processed in a very specific way โ and all of that exists so you can literally use the same format string for both strftime() and strptime(). cute, right? one string, two functions, no drama ๐
๐จ lowkey important: dates built bystrftime()with the%Yor%C%yconversion specifiers can have values bigger than 9999. so if you then feed those intostrptime()using%C%yor%Y, the year values get truncated to four digits. so basically, apps should use%+w%yor%+xYwithwandxset large enough to hold the full value of any years that will be printed or scanned. no cap, this will bite you ๐
also go peek at the APPLICATION USAGE section in strftime(), it's giving sibling energy ๐ธ
and here's the chaotic part: it is unspecified whether multiple calls to strptime() using the same tm structure will update the current contents of that structure or just fully overwrite everything in it. like, nobody knows, it's a mystery ๐. so conforming applications should make a single call to strptime() with a format and all the data needed to completely specify the date and time being converted. one shot, one call, main character energy ๐
๐ง RATIONALE ๐ง
bestie, this one just points you over to the RATIONALE section for strftime(). that's it. that's the whole section. go read your sibling's homework, iykyk ๐โจ
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
literally nothing. empty. the void. no future plans, no glow-up scheduled. iconic silence ๐คซ
๐ SEE ALSO ๐
fprintf()fscanf()strftime()time()
also, from the Base Definitions volume of POSIX.1โ2017: <time.h> ๐
๐ COPYRIGHT ๐
okay so legally i have to tell you this and it's not my words, it's the required text, so here we go bestie, no skipping allowed ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! ๐โจ this one comes from the IEEE/The Open Group, dated 2017, and it's page STRPTIME(3P) in the manual. mwah, go parse some dates responsibly ๐๐
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms