时间标签(timestamp)的格式为RFC3339格式。
strptime 和strptime函数
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
#include <string>
using namespace std;
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
int length= strftime (buffer,80,"%Y-%m-%dT%H:%M:%SZ\n",timeinfo);
printf ("timestamp: %.*s",length,buffer);
return 0;
}
strptime 函数将字符串转换成 tm 结构
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
int
main(void)
{
struct tm *timeinfo;
char buffer[80];
memset( timeinfo, 0, sizeof(struct tm));
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", timeinfo);
int length= strftime (buffer,80,"%Y-%m-%dT%H:%M:%SZ\n",timeinfo);
printf ("timestamp: %.*s",length,buffer);
exit(EXIT_SUCCESS);
}
localtime 和mktime 函数
mktime 将tm 结构转换成 time_t
localtime 将time_t 转换成 tm结构时间
gmtime 函数 Convert time_t to tm as UTC time