C++ 将 time_t 转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9483974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:53:20  来源:igfitidea点击:

Converting time_t to int

c++epoch

提问by Yogi

I want to convert a given time into epoch(time_t) and vice versa. can anyone tell what is the routine or algorithm for this?

我想将给定的时间转换为纪元(time_t),反之亦然。谁能说出这的例程或算法是什么?

Thanks

谢谢

Update

更新

 epoch_strt.tm_sec = 0;
 epoch_strt.tm_min = 0;
 epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
 epoch_strt.tm_isdst = -1;

double nsecs = difftime(curtime, basetime);//current time from system, I converrting it to struct tm

but this always return 32399 for some reason.

但这总是出于某种原因返回 32399 。

回答by Luchian Grigore

You should cast it to a long intinstead of int.

您应该将其转换为 along int而不是int

long int t = static_cast<long int> time(NULL);

An intmight not be enough to hold the time, for example, on my platform, time_tis a typedefof __int64.

一个int可能不足以保存时间,例如,我的平台上,time_ttypedef__int64

回答by bames53

Whatever you're doing with time_t, you'll probably be best off using the <chrono>library. Then you can convert to and from time_t, if necessary. Hopefully you can just do what you need in <chrono>

无论您使用 做什么time_t,最好使用该<chrono>库。然后,您可以time_t根据需要在 和 之间进行转换。希望你可以做你需要的<chrono>

#include <chrono>

int main() {
    auto a = std::chrono::system_clock::now()
    time_t b = std::chrono::system_clock::to_time_t(a);
    auto c = std::chrono::system_clock::from_time_t(b);
}

Update:

更新:

The C++ standard library doesn't yet have an API for dates as good as <chrono>is for time. You can use the Boost date library or you can fall back on the C date library:

C++ 标准库还没有像<chrono>时间一样好的日期 API 。您可以使用 Boost 日期库,也可以使用 C 日期库:

#include <ctime>
#include <chrono>
#include <iostream>

int main() {
    std::tm epoch_start = {};
    epoch_start.tm_sec = 0;
    epoch_start.tm_min = 0;
    epoch_start.tm_hour = 0;
    epoch_start.tm_mday = 1;
    epoch_start.tm_mon = 0;
    epoch_start.tm_year = 70;
    epoch_start.tm_wday = 4;
    epoch_start.tm_yday = 0;
    epoch_start.tm_isdst = -1;

    std::time_t base = std::mktime(&epoch_start);

    auto diff = std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(base);
    std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(diff);

    std::cout << s.count() << '\n';
}

回答by Rob?

To convert a struct tmto a time_t, use mktime. To convert a time_tto a struct tm, use either gmtimeor localtime.

要将 a 转换struct tm为 a time_t,请使用mktime. 要将 a 转换time_t为 a struct tm,请使用gmtimelocaltime

Sample:

样品

#include <iostream>
#include <ctime>

int main () {
    std::time_t now = std::time(0);

    std::tm *now_tm = gmtime(&now);

    std::tm tomorrow_tm(*now_tm);

    tomorrow_tm.tm_mday += 1;
    tomorrow_tm.tm_hour = 0;
    tomorrow_tm.tm_min = 0;
    tomorrow_tm.tm_sec = 0;

    std::time_t tomorrow = std::mktime(&tomorrow_tm);

    double delta = std::difftime(tomorrow, now);

    std::cout << "Tomorrow is " << delta << " seconds from now.\n";
}



UPDATE 2更新 2

#include <iostream>
#include <ctime>
#include <cassert>

// Display the difference between now and 1970-01-01 00:00:00
// On my computer, this printed the value 1330421747
int main () {

    std::tm epoch_strt;
    epoch_strt.tm_sec = 0;
    epoch_strt.tm_min = 0;
    epoch_strt.tm_hour = 0;
    epoch_strt.tm_mday = 1;
    epoch_strt.tm_mon = 0;
    epoch_strt.tm_year = 70;
    epoch_strt.tm_isdst = -1;

    std::time_t basetime = std::mktime(&epoch_strt);

    std::time_t curtime = std::time(0);

    long long nsecs = std::difftime(curtime, basetime);

    std::cout << "Seconds since epoch: " << nsecs << "\n";
    assert(nsecs > 42ll * 365 * 24 * 60 * 60);
}