STRPTIME(3) Linux Programmer's Manual STRPTIME(3)
NAME
strptime - convert a string representation of time to a time tm structure
SYNOPSIS
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>
❤❤❤❤❤❤❤❤
char *strptime(const char *s, const char *format, struct tm *tm);
DESCRIPTION
The strptime() function is the converse function to strftime(3) and converts the character string pointed to by s to values which are stored in the tm structure pointed to by tm, using the format specified by format. Here
format is a character string that consists of field descriptors and text characters, reminiscent of scanf(3). Each field descriptor consists of a % character followed by another character that specifies the replacement for
the field descriptor. All other characters in the format string must have a matching character in the input string, except for whitespace, which matches zero or more whitespace characters in the input string. There should
be whitespace or other alphanumeric characters between any two field descriptors.
The strptime() function processes the input string from left to right. Each of the three possible input elements (whitespace, literal, or format) are handled one after the other. If the input cannot be matched to the for‐
mat string the function stops. The remainder of the format and input strings are not processed.
The supported input field descriptors are listed below. In case a text string (such as a weekday or month name) is to be matched, the comparison is case insensitive. In case a number is to be matched, leading zeros are
permitted but not required.
%% The % character.
%a or %A
The weekday name according to the current locale, in abbreviated form or the full name.
%b or %B or %h
The month name according to the current locale, in abbreviated form or the full name.
%c The date and time representation for the current locale.
%C The century number (0-99).
%d or %e
The day of month (1-31).
%D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format is %Y-%m-%d.)
%H The hour (0-23).
%I The hour on a 12-hour clock (1-12).
%j The day number in the year (1-366).
%m The month number (1-12).
使用前要加强宏定义和头文件
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif // ! _XOPEN_SOURCE
#include <iostream>
#include <time.h>
using namespace std;
time_t timeTest(const char* timeData,const char* format)
{
time_t tt;
strptime(timeData, &tt);
return std::mktime(&tt);
}
int main()
{
auto t=timeTest("20200429", "%y%m%D");
cout << "time str 20200429 is " << t << endl;
return 0;
}