strptime(3)
โจ strptime โ string parse time โจ
ok so bestie, welcome to strptime(3), a libc classic ๐ this is the one you call when you have a string and you want a struct tm out of it. literally the reverse of strftime(3).
๐ LIBRARY
Standard C library (libc, -lc)
๐ซถ SYNOPSIS
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>
char *strptime(const char *restrict s, const char *restrict format,
struct tm *restrict tm);
๐ธ DESCRIPTION
okay so strptime() is the converse of strftime(3), no cap. it takes the character string pointed to by s and converts it into values, and those values get stored in the "broken-down time" structure pointed to by tm, using the format you specified in format.
the broken-down time structure tm is described over in tm(3type). go say hi ๐
so the format argument is a character string made of field descriptors and text characters, and it's very reminiscent of scanf(3). each field descriptor is a % character followed by another character that says what that field descriptor gets replaced with. every other character in the format string must have a matching character in the input string โ except whitespace, which matches zero or more whitespace characters in the input string. and listen: there should be whitespace or other alphanumeric characters between any two field descriptors. spacing is self-care ๐
strptime() processes the input string from left to right ๐
each of the three possible input elements (whitespace, literal, or format) is handled one after the other. if the input can't be matched to the format string, the function stops. the remainder of the format and the input strings are not processed. it's giving "we're done here".
here are all the supported input field descriptors โจ in case a text string is to be matched (like the name of a day of the week, or a month name), the comparison is case insensitive. in case a number is to be matched, leading zeros are permitted but not required. bestie behavior tbh.
%%โ the%character.%aor%Aโ the name of the day of the week according to the current locale, in abbreviated form or the full name.%bor%Bor%hโ the month name according to the current locale, in abbreviated form or the full name.%cโ the date and time representation for the current locale.%Cโ the century number (0โ99).%dor%eโ the day of month (1โ31).%Dโ equivalent to%m/%d/%y. (this is the American style date, very confusing to non-Americans, especially since%d/%m/%yis widely used in Europe. the ISO 8601 standard format is%Y-%m-%d. ๐)%Hโ the hour (0โ23).%Iโ the hour on a 12-hour clock (1โ12).%jโ the day number in the year (1โ366).%mโ the month number (1โ12).%Mโ the minute (0โ59).%nโ arbitrary whitespace.%pโ the locale's equivalent of AM or PM. (note: there may be none.)%rโ the 12-hour clock time (using the locale's AM or PM). in the POSIX locale equivalent to%I:%M:%S %p. ift_fmt_ampmis empty in theLC_TIMEpart of the current locale, then the behavior is undefined. ๐ญ%Rโ equivalent to%H:%M.%Sโ the second (0โ60; 60 may occur for leap seconds; earlier also 61 was allowed).%tโ arbitrary whitespace.%Tโ equivalent to%H:%M:%S.%Uโ the week number with Sunday the first day of the week (0โ53). the first Sunday of January is the first day of week 1.%wโ the ordinal number of the day of the week (0โ6), with Sunday = 0.%Wโ the week number with Monday the first day of the week (0โ53). the first Monday of January is the first day of week 1.%xโ the date, using the locale's date format.%Xโ the time, using the locale's time format.%yโ the year within century (0โ99). when a century is not otherwise specified, values in the range 69โ99 refer to years in the twentieth century (1969โ1999); values in the range 00โ68 refer to years in the twenty-first century (2000โ2068).%Yโ the year, including century (for example, 1991).
๐ฆ the E and O modifiers
some field descriptors can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used. if the alternative format or specification does not exist in the current locale, the unmodified field descriptor is used. it's giving โจfallbackโจ
the E modifier specifies that the input string may contain alternative locale-dependent versions of the date and time representation:
%Ecโ the locale's alternative 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.
the O modifier specifies that the numerical input may be in an alternative locale-dependent format:
%Odor%Oeโ the day of the month using the locale's alternative numeric symbols; leading zeros are permitted but not required.%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 ordinal number of the day of the week (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.
๐ RETURN VALUE
the return value of the function is a pointer to the first character not processed in this function call. in case the input string contains more characters than required by the format string, the return value points right after the last consumed input character. in case the whole input string is consumed, the return value points to the null byte at the end of the string. ๐ซถ
if strptime() fails to match all of the format string and therefore an error occurred, the function returns NULL. ๐
๐ ATTRIBUTES
for an explanation of the terms used in this section, see attributes(7).
strptime()โ Thread safety:MT-Safe env locale
(original was a lil table, bestie, but it's just that one row ๐ )
๐ STANDARDS
POSIX.1-2008.
๐ฆ HISTORY
POSIX.1-2001, SUSv2.
๐ NOTES
in principle, this function does not initialize tm โ it stores only the values specified. this means that tm should be initialized before the call. details differ a bit between different UNIX systems. the glibc implementation does not touch those fields which are not explicitly specified, except that it recomputes the tm_wday and tm_yday fields if any of the year, month, or day elements changed.
the 'y' (year in century) specification is taken to specify a year in the range 1950โ2049 by glibc 2.0. it is taken to be a year in 1969โ2068 since glibc 2.1.
๐ง glibc notes
for reasons of symmetry, glibc tries to support for strptime() the same format characters as for strftime(3). (in most cases, the corresponding fields are parsed, but no field in tm is changed.) this leads to:
%Fโ equivalent to%Y-%m-%d, the ISO 8601 date format.%gโ the year corresponding to the ISO week number, but without the century (0โ99).%Gโ the year corresponding to the ISO week number. (for example, 1991.)%uโ the day of the week as a decimal number (1โ7, where Monday = 1).%Vโ the ISO 8601:1988 week number as a decimal number (1โ53). if the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. otherwise, it is the last week of the previous year, and the next week is week 1.%zโ an RFC-822/ISO 8601 standard timezone specification.%Zโ the timezone name.
similarly, because of GNU extensions to strftime(3), %k is accepted as a synonym for %H, and %l should be accepted as a synonym for %I, and %P is accepted as a synonym for %p. finally:
%sโ the number of seconds since the Epoch,1970-01-01 00:00:00 +0000(UTC). leap seconds are not counted unless leap second support is available.
the glibc implementation does not require whitespace between two field descriptors. slay.
๐ EXAMPLES
the following example demonstrates the use of strptime() and strftime(3). ๐ฅ
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int
main(void)
{
struct tm tm;
char buf[255];
memset(&tm, 0, sizeof(tm));
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
puts(buf);
exit(EXIT_SUCCESS);
}
๐ SEE ALSO
time(2), getdate(3), scanf(3), setlocale(3), strftime(3)
okay that's the whole thing bestie, you ate that ๐ซถ this came from Linux man-pages 6.20, dated 2026-07-26 โ so go forth, parse those strings, and remember: always memset your struct tm first or she will NOT behave. โจ๐๐
๐ฆ where this page comes from
- 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 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, Mitchum DSouza <m.dsouza@mrc-apu.cam.ac.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms