strptime (3) - Linux Manuals
strptime: convert a string representation of time to a time tm structure
NAME
strptime - convert a string representation of time to a time tm structure
SYNOPSIS
#define _XOPEN_SOURCE /* See feature_test_macros(7) */#include <time.h>
char *strptime(const char *s, const char *format, struct tm *tm);
DESCRIPTION
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 defined in <time.h> as follows:
struct tm {
For more details on the
tm
structure, see
ctime(3).
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.
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 O modifier specifies that the numerical input may be in an
alternative locale-dependent format:
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.
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
The glibc implementation does not require whitespace between
two field descriptors.
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int
main(void)
{
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).
Interface Attribute Value
strptime()
Thread safety MT-Safe env locale CONFORMING TO
POSIX.1-2001, POSIX.1-2008, SUSv2.
NOTES
In principle, this function does not initialize
tm
but
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
field if any of the year, month, or day elements changed.
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
EXAMPLES
The following example demonstrates the use of
strptime()
and
strftime(3).
COLOPHON
This page is part of release 5.10 of the Linux
man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
https://www.kernel.org/doc/man-pages/.