时间标签(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
本文介绍如何使用strptime和strftime函数进行时间格式转换,包括从字符串到tm结构体,再到时间戳的转换过程。同时,展示了mktime和localtime函数在时间管理中的应用。
1万+

被折叠的 条评论
为什么被折叠?



