STRPTIME(3POSIX)
π STRPTIME(3POSIX) π
okay bestie, this is the POSIX Programmer's Manual page for strptime, and it's giving date-and-time-conversion realness. Buckle up. π¦
πΈ PROLOG πΈ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
π 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.
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.
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.
fr fr, this is the part where you memorize the letters. no cap.
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.
%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.
π¦ 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 β β a bunch of those formats that are literally "equivalent to" each other, plus the whole special-whitespace situation, exist for one reason: so you can use the exact same format string for strftime() and strptime() and not lose your mind. matching vibes, matching strings, we love to see it π«Ά
now. important thing, don't scroll past this one π β dates that strftime() builds with the %Y or %C%y conversion specifiers can have values bigger than 9999. and if you then try to read those back with strptime() using %C%y or %Y? yeah, the year values get truncated to four digits. π so instead, applications should use %+w%y or %+xY, with w and x set large enough to hold the full value of any years you're gonna print or scan. big years deserve big fields, periodt β¨
also go read the APPLICATION USAGE section over in strftime() β it's giving important, iykyk.
and one more thing that is genuinely unspecified π β it is unspecified whether multiple calls to strptime() using the same tm structure will update the current contents of the structure or overwrite all contents of the structure. nobody knows, it's a mystery, we don't ask questions π. 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 call, one complete vibe check, done. π
π RATIONALE π
see the RATIONALE section for strftime(). that's it, that's the whole thing, we're redirecting you bestie π
π¦ FUTURE DIRECTIONS π¦
None.
(no crumbs. nothing coming. the future is empty and honestly? iconic. πβ¨)
β SEE ALSO β
fprintf()fscanf()strftime()time()- The Base Definitions volume of POSIX.1β2017,
<time.h>
π COPYRIGHT π
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 .
π«Ά legal bestie moment: the referee document is the original IEEE and The Open Group Standard, not this page. if anything disagrees, they win. no cap.
okay that's a wrap on STRPTIME(3POSIX) π this page is from IEEE/The Open Group, dated 2017, and it's the 3POSIX man page, so go forth and parse those timestamps, you gorgeous genius πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms