c++ - 如何将时间转换为纪元时间?

标签 c++ time

假设我有一个特定的时刻,我知道小时、分钟、日、秒、月、年等;如何转换这个纪元时间(自 1970 年以来的秒数)?

我不能使用 Boost,所以请不要推荐 Boost 解决方案。

最佳答案

使用 mktime(3)功能。例如:

struct tm t = {0};  // Initalize to all 0's
t.tm_year = 112;  // This is year-1900, so 112 = 2012
t.tm_mon = 8;
t.tm_mday = 15;
t.tm_hour = 21;
t.tm_min = 54;
t.tm_sec = 13;
time_t timeSinceEpoch = mktime(&t);
// Result: 1347764053

关于c++ - 如何将时间转换为纪元时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979655/

相关文章:

java - 集成测试其行为取决于当前时间的 API

html - CakePHP:四分之一精度的时间输入

c++ - 为什么 views::reverse 不适用于 iota_view<int64_t, int64_t>

c - 在 C 中进行毫秒倒计时的最简单方法?

java.text.ParseException : Unparseable date: "20:01:00.000Z"

Java Swing - 我需要将输入数字转换为 HH :MM format 中的分钟和秒

c++ - 在应用程序崩溃期间获取堆栈跟踪 - C/C++

c++ - 我可以为模板成员函数提供多个默认值吗?

c++ - Xcode:编译而不运行

cryptography - CryptoAPI 封装的 key