【问题标题】:Get UTC epoch time till start of current year in C++在 C++ 中获取 UTC 纪元时间直到今年年初
【发布时间】:2020-06-12 16:44:32
【问题描述】:

我正在尝试在今年年初获取 EPOCH 时间(没有地区差异的通用时间)。以下是我写的:

    time_t getEpochTimeAtSOY(){
        /* Get the current year */
        time_t currTime = time(NULL);
        struct tm *aTime = localtime(&currTime);
        int currentYear = aTime->tm_year;   // This is (actualCurrentYear - 1900), so 2020 - 1900 = 120

        /* Now find out EPOCH time for start of currentYear */
        struct tm t;
        time_t timeSinceEpoch = 0;
        memset (&t, 0, sizeof(t));  // Initalize to all 0's
        t.tm_year = currentYear;
        t.tm_mday = 1;

        timeSinceEpoch = timegm(&t); // EPOCH time of 1st Jan <currentYear> 00:00

        return (timeSinceEpoch);
    }

这会在 x86 或 x86_64 上返回正确的结果(即 1577836800),但目标板(交叉编译器)不支持 timegm() 函数。如果我使用 mktime() 代替 timegm(),则会导致区域时差。有人可以建议一个替代的便携式解决方案吗?也许使用 std::chrono 或其他东西?

【问题讨论】:

    标签: c++ time epoch chrono


    【解决方案1】:

    我刚刚意识到getEpochTimeFromSOY() 旨在仅返回与第一年关联的time_t,而不是在当前时间和第一年之间进行减法运算。就是这样:

    std::time_t
    getEpochTimeFromSOY()
    {
        using namespace date;
        using namespace std::chrono;
        auto currTime = system_clock::now();
        auto y = year_month_day{floor<days>(currTime)}.year();
        return floor<seconds>(sys_days{y/1/1} - sys_days{})/1s;
    }
    

    这在 C++20 中非常简单,并且有一个库可以使用 C++20 语法来实现,该语法可以追溯到 C++11(使用 chrono)。

    这是 C++20 语法:

    #include <ctime>
    #include <chrono>
    
    std::time_t
    getEpochTimeFromSOY()
    {
        using namespace std::chrono;
        auto currTime = system_clock::now();
        auto y = year_month_day{floor<days>(currTime)}.year();
        return floor<seconds>(currTime - sys_days{y/1/1})/1s;
    }
    

    Here's the header-only, free, open-source library that you can use prior to C++20,下面是使用它的语法:

    #include "date/date.h"
    
    #include <ctime>
    #include <chrono>
    
    std::time_t
    getEpochTimeFromSOY()
    {
        using namespace date;
        using namespace std::chrono;
        auto currTime = system_clock::now();
        auto y = year_month_day{floor<days>(currTime)}.year();
        return floor<seconds>(currTime - sys_days{y/1/1})/1s;
    }
    

    即包括date.h 并添加using namespace date

    代码说明:

    • system_clock::time_point 的形式获取当前时间。
    • 以 UTC 格式获取当前时间的年份。
    • 从当前时间中减去一年中的第一个时刻 (UTC),将其截断到秒精度,然后除以 1 秒来提取计数。

    语法1s 是在 C++14 中引入的。在 C++11 中,您可以将其更改为 seconds{1}。或者您可以将/1s 替换为.count() 以将秒数提取为整数类型。

    如果您不想使用免费的开源库来执行此操作,您可以获取 underlying algorithms from here 并自己编写代码。

    【讨论】:

      【解决方案2】:

      [注意:这更像是一个“设计元答案”。霍华德的回答是正确的——我赞成。 ]

      如果我正在编写这段代码,我会改为编写两个函数getCurrentYear()epochTimeAtStartOfYear(year y)。每个都做一件事情,可以组合起来做你想做的事,但它们也可以在其他情况下使用。

      (警告 - 未经测试的代码)

      std::chrono::year
      getCurrentYear()
      {
          using namespace std::chrono;
          return year_month_day{floor<days>(system_clock::now())}.year();
      }
      
      std::time_t
      getEpochTimeFromSOY(std::chrono::year y)
      {
          using namespace std::chrono;
          return floor<seconds>(sys_days{y/1/1} - sys_days{})/1s;
      }
      
      std::time_t getEpochTimeAtSOY()
      {
          return getEpochTimeFromSOY(getCurrentYear());
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-25
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        相关资源
        最近更新 更多