strptime() — Date and time conversion

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <time.h>

char *strptime(const char * __restrict__ buf, const char * __restrict__ fmt, 
               struct tm * __restrict__ tm);

General description

The strptime() function converts the character string pointed to by buf to values that are stored in the tm structure pointed to by tm, using the format specified by fmt. Only the fields in the tm structure for which there is a corresponding format item in fmt are updated by the strptime() function. Therefore, for a valid and conforming tm structure, provide strptime() with a format and data that completely specify the date and time being converted.

To avoid infringing on the user's name space, this nonstandard function has two names. One name is prefixed with two underscore characters, and one name is not. The name without the prefix underscore characters is exposed only when you use LANGLVL(EXTENDED).

To use this function, you must either invoke the function using its external entry point name (that is, the name that begins with two underscore characters), or compile with LANGLVL(EXTENDED). When you use LANGLVL(EXTENDED) any relevant information in the header is also exposed.

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 the isalnum() to isxdigit() function), an ordinary character other than a percent sign (%) or white space character, or a conversion specification. Each conversion specification is composed of a percent sign followed by a conversion character that specifies the replacement required. There must be a white space character or other nonalphanumeric character between any two conversion specifications.

Table 1. Conversion Specifiers Used by strptime()
Specifier Meaning
%a Day of week, using locale's abbreviated or full weekday name.
%A Day of week, using locale's abbreviated or full weekday name.
%b Month, using locale's abbreviated or full month name.
%B Month, using locale's abbreviated or full month name.
%c Date and time, using locale's date and time.
%C Century number (year divided by 100 and truncated to an integer).
%d Day of the month (1 - 31; leading zeros permitted but not required; day will not be checked for correctness for the month specified).
%D Date as %m/%d/%y.
%e Day of the month (1 - 31; leading zeros permitted but not required; day will not be checked for correctness for the month specified).
%h Month, using locale's abbreviated or full month name.
%H Hour (0 - 23; leading zeros permitted but not required).
%I Hour (0 - 12; leading zeros permitted but not required).
%j Day number of the year (001 - 366).
%m Month number (1 - 12; leading zeros are permitted but not required).
%M Minute (0 - 59; leading zeros are permitted but not required).
%n Newline character.
%p Locale's equivalent of AM or PM.
%r 12-hour clock time using the AM/PM notation if t_fmt_ampm is not an empty string in the LC_TIME portion of the current locale; in the POSIX locale, this is equivalent to %I : %M : %S %p.
%R Time in 24 hour notation (11/19/01M).
%S Seconds (0 - 60; leading zeros are permitted but not required).
%t Tab character.
%T Time as %H:%M:%S.
%U Week number of the year (0 - 53; where Sunday is the first day of the week; leading zeros are permitted but not required).
%w Weekday (0 - 6; where Sunday is 0; leading zeros are permitted but not required).
%W Week number of the year (0 - 53; where Monday is the first day of the week; leading zeros are permitted but not required).
%x Date, using locale's date format.
%X Time, using locale's time format.
%y Year within century. When a century is not otherwise specified, values in the range 69 - 99 refer to years in the twentieth century (1969 to 1999 inclusive); values in the range 00 - 68 refer to years in the twenty-first century (2000 to 2068 inclusive). Leading zeros are permitted but not required.
%Y Year, including century. When the value of the _EDC_STRPTM_STD environment variable is set to 1, at most 4 digits will be consumed. When the value of _EDC_STRPTM_STD is set to other values or unset, more than 4 digits might be consumed, and if the generated value is greater than 9999, this function fails.
%Z Time zone name.
%% Replace with %.
Modified directives: Some directives can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified directive. If the alternative format or specification does not exist in the current locale, the behavior will be as if the unmodified directive were used.
Table 2. Modified Directives Used by strptime()
Specifier Meaning
%Ec Replace with the locale's alternative date and time representation.
%EC Replace with the name of the base year (period) in the locale's representation.
%Ex Replace with the locale's alternative date representation.
%EX Replace with the locale's alternative time representation.
%Ey Replace with the offset from %EC (year only) in the locale's representation.
%EY Replace with the full alternative year representation.
%Od Replace with the day of month, using the locale's alternative numeric symbols, filled as needed with leading zeros if there is any alternative symbol for zero, otherwise with leading spaces.
%Oe Replace with the day of the month, using the locale's alternative numeric symbols, filled as needed with leading spaces.
%OH Replace with the hour (24-hour clock) using the locale's alternative numeric symbols.
%OI Replace with the hour (12-hour clock) using the locale's alternative numeric symbols.
%Om Replace with the month using the locale's alternative numeric symbols.
%OM Replace with the minutes using the locale's alternative numeric symbols.
%OS Replace with the seconds using the locale's alternative numeric symbols.
%OU Replace with the week number of the year (Sunday as the first rules corresponding to %U) using the locale's alternative numeric symbols.
%Ow Replace with the weekday (Sunday=0) using the locale's alternative numeric symbols.
%OW Replace with the week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.
%Oy Replace with the year (offset from %C) in the locale's alternative representation and using the locale's alternative numeric symbols.

A directive 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 directive 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 directives composed or %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, excepting 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.

Returned value

If successful, strptime() returns a pointer to the character following the last character parsed.

If unsuccessful, strptime() returns a NULL pointer.

Example

CELEBS48
⁄* CELEBS48 *⁄                                   
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <time.h>                                                               
#include <localdef.h>                                                           
                                                                                
int main(void)                                                                  
{                                                                               
   struct tm xmas;                                                              
                                                                                
   if (strptime("12⁄25⁄93 13:30:00", "%D %T", &xmas) == NULL) {                 
      printf("strptime() failed.\n");                                           
      exit(1);                                                                  
   }                                                                            
                                                                                
   printf("tm_sec  =%3d\n", xmas.tm_sec  );                                     
   printf("tm_min  =%3d\n", xmas.tm_min  );                                     
   printf("tm_hour =%3d\n", xmas.tm_hour );                                     
   printf("tm_mday =%3d\n", xmas.tm_mday );                                     
   printf("tm_mon  =%3d\n", xmas.tm_mon  );                                     
   printf("tm_year =%3d\n", xmas.tm_year );                                     
   printf("tm_wday =%3d\n", xmas.tm_wday );                                     
   printf("tm_yday =%3d\n", xmas.tm_yday );                                     
}                                                                               
Output
tm_sec  =  0
tm_min  = 30
tm_hour = 13
tm_mday = 25
tm_mon  = 11
tm_year = 93
tm_wday =  0
tm_yday =358

Related information