strptime - convert a string representation of time to a time tm structure
#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);
The strptime() function is the converse of
strftime(3); it converts the character string pointed
to by s
to values which are stored in the "broken-down time"
structure pointed to by tm
, using the format specified by
format
.
The broken-down time structure tm
is described in
tm(3type).
The format
argument is a character string that consists of
field descriptors and text characters, reminiscent of
scanf(3). Each field descriptor consists of a
% character followed by another character that
specifies the replacement for the field descriptor. All other characters
in the format
string must have a matching character in the
input string, except for whitespace, which matches zero or more
whitespace characters in the input string. There should be whitespace or
other alphanumeric characters between any two field descriptors.
The strptime() function processes the input string from left to right. Each of the three possible input elements (whitespace, literal, or format) are handled one after the other. If the input cannot be matched to the format string, the function stops. The remainder of the format and input strings are not processed.
The supported input field descriptors are listed below. In case a text string (such as the name of a day of the week or a month name) is to be matched, the comparison is case insensitive. In case a number is to be matched, leading zeros are permitted but not required.
The % character.
The name of the day of the week according to the current locale, in abbreviated form or the full name.
The month name according to the current locale, in abbreviated form or the full name.
The date and time representation for the current locale.
The century number (0–99).
The day of month (1–31).
Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format is %Y-%m-%d.)
The hour (0–23).
The hour on a 12-hour clock (1–12).
The day number in the year (1–366).
The month number (1–12).
The minute (0–59).
Arbitrary whitespace.
The locale's equivalent of AM or PM. (Note: there may be none.)
The 12-hour clock time (using the locale's AM or PM). In the POSIX
locale equivalent to %I:%M:%S %p. If
t_fmt_ampm
is empty in the LC_TIME part of the
current locale, then the behavior is undefined.
Equivalent to %H:%M.
The second (0–60; 60 may occur for leap seconds; earlier also 61 was allowed).
Arbitrary whitespace.
Equivalent to %H:%M:%S.
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.
The ordinal number of the day of the week (0–6), with Sunday = 0.
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.
The date, using the locale's date format.
The time, using the locale's time format.
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).
The year, including century (for example, 1991).
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.
The E modifier specifies that the input string may contain alternative locale-dependent versions of the date and time representation:
The locale's alternative date and time representation.
The name of the base year (period) in the locale's alternative representation.
The locale's alternative date representation.
The locale's alternative time representation.
The offset from %EC (year only) in the locale's alternative representation.
The full alternative year representation.
The O modifier specifies that the numerical input may be in an alternative locale-dependent format:
The day of the month using the locale's alternative numeric symbols; leading zeros are permitted but not required.
The hour (24-hour clock) using the locale's alternative numeric symbols.
The hour (12-hour clock) using the locale's alternative numeric symbols.
The month using the locale's alternative numeric symbols.
The minutes using the locale's alternative numeric symbols.
The seconds using the locale's alternative numeric symbols.
The week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols.
The ordinal number of the day of the week (Sunday=0), using the locale's alternative numeric symbols.
The week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.
The year (offset from %C) using the locale's alternative numeric symbols.
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.
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);
}