strptime()

Convert a string into a time

Synopsis:

#include <time.h>

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

Arguments:

buf
A pointer to a buffer that contains the string to convert.
format
The format that you want to use for the time; see "Formats," below.
timeptr
A pointer to a tm structure where the function can store the time.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Note: This function is in libc.a, but not in libc.so (in order to save space).

Description:

The strptime() function converts the character string that buf points to into a time and stores the time in the tm structure pointed to by timeptr, using the specified format.

Formats

The format is composed of zero or more directives, each of which is composed of one of the following:

Note: Ensure that there are whitespace or other non-alphanumeric characters between any two conversion specifications.

The strptime() function supports the following conversion specifications:

%a or %A
The day of the week, using the locale's weekday names; you can use either the abbreviated or full name.
%b, %B, or %h
The month, using the locale's month names; you can use either the abbreviated or full name.
%c
Replaced by the locale's appropriate date and time representation.
%C
The century number [00,99]. Leading zeros are permitted but not required.
%d or %e
The day of the month [01,31]. Leading zeros are permitted but not required.
%D
The date as %m/%d/%y.
%H
The hour (24-hour clock) [00,23]; leading zeros are permitted but not required.
%I
The hour (12-hour clock) [01,12]; leading zeros are permitted but not required.
%j
The day number of the year [001,366]; leading zeros are permitted but not required.
%m
The month number [01,12]; leading zeros are permitted but not required.
%M
The minute [00,59]; leading zeros are permitted but not required.
%n or %t
Any whitespace.
%p
The locale's equivalent of a.m. or p.m.
%r
12-hour clock time using the AM/PM notation if t_fmt_ampm isn't 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
The time as %H:%M.
%S
The seconds [00,60]; leading zeros are permitted but not required.
%T
The time as %H:%M:%S.
%U
The week number of the year (Sunday as the first day of the week) as a decimal number [00,53]; leading zeros are permitted but not required.
%w
The weekday as a decimal number [0,6], with 0 representing Sunday; leading zeros are permitted but not required.
%W
The week number of the year (Monday as the first day of the week) as a decimal number [00,53]; leading zeros are permitted but not required.
%x
The date, using the locale's date format.
%X
The time, using the locale's time format.
%y
The year within the century. When a century isn't otherwise specified, values in the range [69,99] refer to years 1969 to 1999 inclusive, and values in the range [00,68] refer to years 2000 to 2068 inclusive; leading zeros are permitted but not required.
Note: The default century inferred from a 2-digit year will likely change in a future version of IEEE Std 1003.1-2001. (This would apply to all commands accepting a 2-digit year as input.)
%Y
The year, including the century (for example, 1988).
%%
Replaced by %.

Modified conversion specifiers

You can modify some conversion specifiers by adding the E and O ("Oh") modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified conversion specifier. If the alternative format or specification doesn't exist in the current locale, the function behaves as if you used the unmodified conversion specification.

%Ec
The locale's alternative appropriate 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.
%Od or %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 (using Sunday as the first day of the week) using the locale's alternative numeric symbols.
%Ow
The number of the weekday (Sunday=0) using the locale's alternative numeric symbols.
%OW
The week number of the year (using 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.

A conversion specification composed of whitespace characters is executed by scanning input up to the first character that isn't whitespace (which remains unscanned), or until no more characters can be scanned.

A conversion specification 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 conversion specifications composed of %n, %t, whitespace characters, or any combination is executed by scanning up to the first character that isn't whitespace (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, except 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 doesn't scan any more characters.

Returns:

A pointer to the character after the last character parsed in the string, or NULL.

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main( void )
  {
    struct tm my_tm;
    char in_buffer[ 80 ] ="July 31, 1993 11:00:00",
         out_buffer[ 80 ];
    time_t t;

    /* Convert the string to a struct tm. */
    memset (&my_tm, 0, sizeof(struct tm));
    strptime( in_buffer, "%b %d, %Y %T", &my_tm );

    /* Convert the struct tm to a time_t (to fill in the
     * missing fields). */
    t = mktime (&my_tm);

    /* Convert the time back to a string. */
    strftime( out_buffer, 80, "That's %D (a %A), at %T",
              localtime (&t) );
    printf( "%s\n", out_buffer );

    return EXIT_SUCCESS;
  }

This produces the output:

That's 07/31/93 (a Saturday), at 11:00:00

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes