C ++如何将年 - 月 - 日格式的日期转换为unix纪元格式? [英] C++ How can I convert a date in year-month-day format to a unix epoch format?

查看:271
本文介绍了C ++如何将年 - 月 - 日格式的日期转换为unix纪元格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个给定的日期转换为包含自1970年1月1日以来的毫秒数的整数。(unix epoch)

I need to convert a given date to an int containing the number of milliseconds since Jan 1 1970. (unix epoch)

我试过下面的代码: p>

I tried the following code:

tm lDate;

lDate.tm_sec = 0;  
lDate.tm_min = 0;  
lDate.tm_hour = 0;  
lDate.tm_mday = 1;  
lDate.tm_mon = 10;  
lDate.tm_year = 2010 - 1900;  

time_t lTimeEpoch = mktime(&lDate);

cout << "Epoch: " << lTimeEpoch << endl;

结果是Epoch:1288584000,对应于2010年11月01日星期一04:00:00 GMT

The result is Epoch: 1288584000 which corresponds to Mon, 01 Nov 2010 04:00:00 GMT

编辑:我期待2010年10月1日,显然tm_mon是从1月1月起的月数,
所以正确的行将是lDate.tm_mon = 10 -1 ;

I was expecting Oct 01 2010, apparently tm_mon is the number of months SINCE January, so the correct line would be lDate.tm_mon = 10 -1;

推荐答案

如手册页面中指定的,tm_mon是:
从1月起, 0至11。

As specified in the man page, tm_mon is: The number of months since January, in the range 0 to 11.

这篇关于C ++如何将年 - 月 - 日格式的日期转换为unix纪元格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆