您的位置:首页 > 其它

mktime的使用

2016-02-28 22:45 459 查看
根据给定年月日计算出,1900-1-1到给定年月日的秒数,就可以使用mktime函数。

#include <iostream>
#include <time.h>

using std::cout;
using std::endl;

int main()
{
/* 获取1900-1-1距当前时间的秒数 */
time_t now_seconds = time(0);
struct tm cur_date;
localtime_s(&cur_date, &now_seconds);
cout << "seconds: " << now_seconds << endl;
cout << "date: " << cur_date.tm_year + 1900 << "-" << cur_date.tm_mon + 1 << "-" << cur_date.tm_mday << endl;

/* 获取1900-1-1距离给定时间的秒数 */
struct tm now_time;
now_time.tm_year = 116;
now_time.tm_yday = 58;
now_time.tm_wday = 0;
now_time.tm_sec = 0;
now_time.tm_mon = 1;
now_time.tm_min = 44;
now_time.tm_mday = 28;
now_time.tm_isdst = 0;
now_time.tm_hour = 22;

time_t sec = mktime(&now_time);
cout << "result seconds: " << sec << endl;
}


执行结果为:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: