![]() |
![]() |
![]() |
![]() |
![]() |
This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Convert a string into a time
#include <time.h> char * strptime( const char *buf, const char *format, struct tm *timeptr );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
![]() |
This function is in libc.a, but not in libc.so (in order to save space). |
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.
The format is composed of zero or more directives, each of which is composed of one of the following:
![]() |
Ensure that there are whitespace or other non-alphanumeric characters between any two conversion specifications. |
The strptime() function supports the following conversion specifications:
![]() |
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.) |
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.
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.
A pointer to the character after the last character parsed in the string, or NULL.
#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, sizeof(struct tm), 0); 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
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
scanf(), strftime(), tm, time(), tzset()
![]() |
![]() |
![]() |
![]() |