Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strptime - convert string to date and time

&pagelevel(4)&pagelevel

Syntax

#include <time.h>

char *strptime(const char *buf, const char *format, struct tm *tm); 

Description

In accordance with format, strptime() converts the string pointed to by *buf into individual values that are stored in the structure pointed to by *tm.

The format string consists of none, one or more conversion statements. Each conversion statement consists of one of the following elements:
one or more white-space characters (as defined in isspace()),
a regular character (neither % nor white-space characters)
or a conversion specification.

Each conversion specification consists of a % character followed by a conversion character that specifies the desired conversion. With conversion specifications that expect a numeric value, the string to be converted may contain not more digits than specified in the format description. I.e. additional leading zeroes are not allowed. If between two conversion specifications there is neither a white-space character nor an non-alphanumeric character, the numbers of digits even must be the same as in the format description.

The following conversion characters are supported:

%%

Replaced by %

%a

Weekday, whereby the name from the locale is used. Either the full name or the abbreviated name can be specified

%A

Same meaning as %a

%b

Month, whereby the name from the locale is used. Either the full name or the abbreviated name can be specified

%B

Same meaning as %b

%c

Date and time representation according to the definition in the locale

%C

Century (the year divided by 100, truncated to an integer) (00-99)

%d

Day of the month (01-31)

%D

Date as %m/%d/%y

%e

Same meaning as %d

%h

Same meaning as %b

%H

Hours (00-23), 24-hour representation

%I

Hours (01-12), 12-hour representation

%j

Day of the year (001-366)

%m

Number of the month (01-12)

%M

Minutes (00-59)

%n

Replaced by a white-space character

%p

Locale’s equivalent of AM or PM

%r

Time in the form %I:%M:%S%p

%R

Time in the form %H:%M

%S

Seconds (00-61), allows leap seconds

%t

Replaced by a white-space character

%T

Time in the form %H:%M:%S

%U

Week number of the year (00-53). The first week begins with the first Sunday of the year. All days before the first Sunday of the year belong to week 0.

%w

Weekday as a number (0-6), Sunday = 0

%W

Week number of the year (00-53), with Monday as the first day of week 1. All days before the first Monday of the year belong to week 0.

%x

Date representation of the locale

%X

Time representation of the locale

%y

Year within the century (00-99)

%Y

Year in the form ccyy (e.g. 1986)

Modified conversion specifications

Some conversion specifications can be modified by the characters E or O to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified conversion specification. If the alternative format or specification does not exist for the current locale, the behavior will be as if the unmodified conversion specification were used.

%Ec

The locale's alternative date and time representation.

%EC

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

Offset from %EC (year only) in the locale's alternative representation.

%EY

Alternative representation for the year.

%Od

Day of the month, using the locale's alternative numeric symbols, padded as needed with leading zeros if an alternative symbol for zero exists; otherwise, with leading spaces.

%Oe

Same meaning as %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 is the first day of the week; rules correspond to %U) using the locale's alternative numeric symbols.

%OV

The week number of the year (Sunday is the first day of the week, rules correspond to %V) using the locale's alternative numeric symbols.

%OW

The week number of the year (Monday is the first day of the week), using the locale's alternative numeric symbols.

%Oy

The year (offset from %C) in the locale's alternative representation, and using the locale's alternative symbols.

A conversion specification consisting of white-space characters is executed by the input being read up to the first character that is not a white-space character (this character remains unread), or until there are no more characters left.

A conversion specification comprising a regular character is executed by the next character from the buffer being read. If the character read from the buffer does not match the character of the conversion specification, the latter fails and the deviating character plus all characters that follow it remain unread.

A sequence of conversion specifications consisting of %n, %t, white-space characters and combinations of all these is executed by being read up to the first character that is not a white-space character (this character remains unread), or until there are no more
characters left.

All other conversion specifications are executed by characters being read in until a character which matches the next conversion specification is read (it remains in the buffer) or until there are no more characters left. The read characters are then compared with the values in the locale that correspond to the conversion specification. If the matching value is found in the locale, the corresponding structure elements of the tm structure are set to the values that correspond to this information.
The search is not case-sensitive if it is a comparison of elements such as weekdays and month names.
If no matching value is found in the locale, strptime() fails and no more characters are read.

Return val.

Pointer to the character after the last character read


if successful.                                                    

Null pointer

otherwise.

Notes

The special handling of white-space characters and many “same formats” is designed to simplify the use of identical format strings with strftime() and strptime().

The structure to which tm points is not initialized with zeros when strptime() is executed.
The values set by the user remain intact as long as they are not modified by conversion statements or implicit calculations. The structure element tm_isdst is never changed. Date adjustment may be carried out implicitly, i.e. if the date entry is incomplete, the missing structure elements are added and a plausibility check is made between the structure elements.

However, this is only made if a week number was specified via %U, %W, %OU or %OW. In this case, the year entry (tm_year) and weekday (tm_wday) are used to calculate and reassign the day in the year (tm_yday), the day of the month (tm_mday) and the month of the year (tm_mon). The weekday is assigned the value 0 if it was not explicitly specified with %w, %a, %A or %Ow.

See also

scanf(), strftime(), time(), time.h.