#include <time.h>
|
char *
strptime (const char *restrict buf, const char *restrict format, struct tm *restrict timeptr); |
The format string consists of zero or more conversion specifications and ordinary characters. All ordinary characters are matched exactly with the buffer, where white space in the format string will match any amount of white space in the buffer. All conversion specifications are identical to those described in strftime.
Two-digit year values, including formats %y and %D, are now interpreted as beginning at 1969 per POSIX requirements. Years 69-00 are interpreted in the 20th century (1969-2000), years 01-68 in the 21st century (2001-2068).
If the format string does not contain enough conversion specifications to completely specify the resulting struct tm , the unspecified members of timeptr are left untouched. For example, if format is "%H:%M:%S", only tm_hour, tm_sec and tm_min will be modified. If time relative to today is desired, initialize the timeptr structure with today’s date before passing it to strptime.
#include <string.h> #include <stdio.h> #include <time.h> #include <locale.h> int main() { struct tm tm; char buf[255]; char *locale; locale = setlocale(LC_TIME,"en_GB.ISO-8859-1"); if( locale != NULL) { strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); printf("sec = %d min = %d hours = %d \nYear = %d Month = %d day = %d\n", tm.tm_sec,tm.tm_min,tm.tm_hour,tm.tm_year,tm.tm_mon,tm.tm_mday); strftime(buf, sizeof(buf), "%d %B %Y %H:%M:%S", &tm); puts(buf); strptime("Mon","%a", &tm); strftime(buf, sizeof(buf), "%a", &tm); puts(buf); } else printf("Failed to set locale"); }
Output
sec = 1 min = 31 hours = 18 Year = 101 Month = 10 day = 12 12 November 2001 18:31:01 Mon
The %p format specifier has no effect unless it is parsed after hour-related specifiers. Specifying %l without %p will produce undefined results. Note that 12AM (ante meridiem) is taken as midnight and 12PM (post meridiem) is taken as noon.
The %U and %W format specifiers accept any value within the range 00 to 53 without validating against other values supplied (like month or day of the year, for example).
The %Z format specifier only accepts time zone abbreviations of the local time zone, or the value "GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations. One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.
The strptime function does not correctly handle multibyte characters in the format argument.
© 2005-2007 Nokia |