c++ - 在 C++ 中将带有时区的日期时间字符串转换为 UNIX 时间戳的快速方法

标签 c++ date datetime mktime c++20

我想在 C++ 中将包含日期时间字符串的巨大文件转换为自 UNIX 纪元(1970 年 1 月 1 日)以来的秒数。我需要非常快的计算,因为我需要处理大量的日期时间。

到目前为止,我已经尝试了两种选择。第一个是使用 mktime,定义于 time.h .我尝试的第二个选项是 Howard Hinnant 的 date library带时区扩展。

这是我用来比较 mktime 和 Howard Hinnant 的 tz 之间性能的代码:

for( int i=0; i<RUNS; i++){
    genrandomdate(&time_str);

    time_t t = mktime(&time_str);

}

auto tz = current_zone()
for( int i=0; i<RUNS; i++){

    genrandomdate(&time_str);
    auto ymd = year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday;
    auto tcurr = make_zoned(tz, local_days{ymd} + 
            seconds{time_str.tm_hour*3600 + time_str.tm_min*60 + time_str.tm_sec}, choose::earliest);
    auto tbase = make_zoned("UTC", local_days{January/1/1970});
    auto dp = tcurr.get_sys_time() - tbase.get_sys_time() + 0s;

}

比较结果:
time for mktime : 0.000142s
time for tz : 0.018748s

与mktime相比,tz的性能并不好。我想要比 mktime 更快的东西,因为 mktime 在重复用于大量迭代时也很慢。 Java Calendar 提供了一种非常快速的方法来做到这一点,但是当时区也在起作用时,我不知道任何 C++ 替代方案。

注意:在没有时区的情况下使用 Howard Hinnant 的日期非常快(甚至超过 Java)。但这还不足以满足我的要求。

最佳答案

您可以采取一些措施来优化 Howard Hinnant 的使用 date library :

auto tbase = make_zoned("UTC", local_days{January/1/1970});

时区(甚至“UTC”)的查找涉及对数据库进行二进制搜索以查找具有该名称的时区。执行一次查找并重用结果会更快:
// outside of loop:
auto utc_tz = locate_zone("UTC");

// inside of loop:
auto tbase = make_zoned(utc_tz, local_days{January/1/1970});

此外,我注意到 tbase是循环无关的,所以整个事情都可以移到循环之外:
// outside of loop:
auto tbase = make_zoned("UTC", local_days{January/1/1970});

这里有一个进一步的小优化。改变:
auto dp = tcurr.get_sys_time() - tbase.get_sys_time() + 0s;

到:
auto dp = tcurr.get_sys_time().time_since_epoch();

这摆脱了对 tbase 的需要共。 tcurr.get_sys_time().time_since_epoch()是自 1970-01-01 00:00:00 UTC 以来的持续时间,以秒为单位。秒的精度仅用于此示例,因为输入具有秒精度。

风格 nit:尽量避免在代码中放入转换因子。这意味着改变:
auto tcurr = make_zoned(tz, local_days{ymd} + 
        seconds{time_str.tm_hour*3600 + time_str.tm_min*60 + time_str.tm_sec}, choose::earliest);

到:
auto tcurr = make_zoned(tz, local_days{ymd} + hours{time_str.tm_hour} + 
                        minutes{time_str.tm_min} + seconds{time_str.tm_sec},
                        choose::earliest);

Is there a way to avoid this binary search if this time zone is also fixed. I mean can we get the time zone offset and DST offset and manually adjust the time point.



如果您不在 Windows 上,请尝试使用 -DUSE_OS_TZDB=1 进行编译.这使用了具有更高性能的数据库的编译形式。

有一种方法可以获取偏移量并手动应用它( https://howardhinnant.github.io/date/tz.html#local_info ),但是除非您知道您的偏移量不会随 time_point 的值而改变。 ,你最终会重新发明 make_zoned 引擎盖下的逻辑。 .

但是,如果您确信您的 UTC 偏移量是恒定的,那么您可以这样做:
auto tz = current_zone();
// Use a sample time_point to get the utc_offset:
auto info = tz->get_info(
    local_days{year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday}
      + hours{time_str.tm_hour} + minutes{time_str.tm_min}
      + seconds{time_str.tm_sec});
seconds utc_offset = info.first.offset;
for( int i=0; i<RUNS; i++){

    genrandomdate(&time_str);
    // Apply the offset manually:
    auto ymd = year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday;
    auto tp = sys_days{ymd} + hours{time_str.tm_hour} +
              minutes{time_str.tm_min} + seconds{time_str.tm_sec} - utc_offset;
    auto dp = tp.time_since_epoch();
}

更新——我自己的计时测试

我正在使用 Xcode 10.2.1 运行 macOS 10.14.4。我创建了一个相对安静的机器:时间机器备份没有运行。邮件未运行。 iTunes 未运行。

我有以下应用程序,它使用几种不同的技术来实现欲望转换,具体取决于预处理器设置:
#include "date/tz.h"
#include <cassert>
#include <iostream>
#include <vector>

constexpr int RUNS = 1'000'000;
using namespace date;
using namespace std;
using namespace std::chrono;

vector<tm>
gendata()
{
    vector<tm> v;
    v.reserve(RUNS);
    auto tz = current_zone();
    auto tp = floor<seconds>(system_clock::now());
    for (auto i = 0; i < RUNS; ++i, tp += 1s)
    {
        zoned_seconds zt{tz, tp};
        auto lt = zt.get_local_time();
        auto d = floor<days>(lt);
        year_month_day ymd{d};
        auto s = lt - d;
        auto h = floor<hours>(s);
        s -= h;
        auto m = floor<minutes>(s);
        s -= m;
        tm x{};
        x.tm_year = int{ymd.year()} - 1900;
        x.tm_mon = unsigned{ymd.month()} - 1;
        x.tm_mday = unsigned{ymd.day()};
        x.tm_hour = h.count();
        x.tm_min = m.count();
        x.tm_sec = s.count();
        x.tm_isdst = -1;
        v.push_back(x);
    }
    return v;
}


int
main()
{

    auto v = gendata();
    vector<time_t> vr;
    vr.reserve(v.size());
    auto tz = current_zone();  // Using date
    sys_seconds begin;         // Using date, optimized
    sys_seconds end;           // Using date, optimized
    seconds offset{};          // Using date, optimized

    auto t0 = steady_clock::now();
    for(auto const& time_str : v)
    {
#if 0  // Using mktime
        auto t = mktime(const_cast<tm*>(&time_str));
        vr.push_back(t);
#elif 1  // Using date, easy
        auto ymd = year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday;
        auto tp = local_days{ymd} + hours{time_str.tm_hour} +
                  minutes{time_str.tm_min} + seconds{time_str.tm_sec};
        zoned_seconds zt{tz, tp};
        vr.push_back(zt.get_sys_time().time_since_epoch().count());
#elif 0  // Using date, optimized
        auto ymd = year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday;
        auto tp = local_days{ymd} + hours{time_str.tm_hour} +
                  minutes{time_str.tm_min} + seconds{time_str.tm_sec};
        sys_seconds zt{(tp - offset).time_since_epoch()};
        if (!(begin <= zt && zt < end))
        {
            auto info = tz->get_info(tp);
            offset = info.first.offset;
            begin = info.first.begin;
            end = info.first.end;
            zt = sys_seconds{(tp - offset).time_since_epoch()};
        }
        vr.push_back(zt.time_since_epoch().count());
#endif
    }
    auto t1 = steady_clock::now();

    cout << (t1-t0)/v.size() << " per conversion\n";
    auto i = vr.begin();
    for(auto const& time_str : v)
    {
        auto t = mktime(const_cast<tm*>(&time_str));
        assert(t == *i);
        ++i;
    }
}

每个解决方案都被计时,然后根据基线解决方案检查正确性。每个解决方案转换 1,000,000 个时间戳,所有时间戳在时间上都相对接近,并输出每次转换的平均时间。

我提出了四种解决方案,以及它们在我的环境中的时间安排:

1. 使用 mktime .

输出:
3849ns per conversion

2. 使用 tz.h以最简单的方式使用 USE_OS_TZDB=0
输出:
3976ns per conversion

这比 mktime 稍慢解决方案。

3. 使用 tz.h以最简单的方式使用 USE_OS_TZDB=1
输出:
55ns per conversion

这比上述两种解决方案要快得多。但是,此解决方案在 Windows 上不可用(此时),并且在 macOS 上不支持库的闰秒部分(在此测试中未使用)。这两个限制都是由操作系统发布时区数据库的方式引起的。

4. 使用 tz.h以优化的方式,利用时间分组时间戳的先验知识。如果假设为假,则性能会受到影响,但正确性不会受到影响。

输出:
15ns per conversion

这个结果大致独立于 USE_OS_TZDB环境。但性能取决于输入数据不会经常更改 UTC 偏移量的事实。对于不明确或不存在的本地时间点,此解决方案也很粗心。这样的本地时间点没有唯一的 UTC 映射。如果遇到这样的本地时间点,解决方案 2 和 3 将抛出异常。

USE_OS_TZDB 的运行时错误

OP 在 Ubuntu 上运行时得到了这个堆栈转储。此崩溃发生在第一次访问时区数据库时。崩溃是由操作系统为 pthread 库提供的空 stub 函数引起的。修复方法是显式链接到 pthreads 库(在命令行中包含 -lpthread)。
==20645== Process terminating with default action of signal 6 (SIGABRT)
==20645==    at 0x5413428: raise (raise.c:54)
==20645==    by 0x5415029: abort (abort.c:89)
==20645==    by 0x4EC68F6: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==20645==    by 0x4ECCA45: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==20645==    by 0x4ECCA80: std::terminate() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==20645==    by 0x4ECCCB3: __cxa_throw (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==20645==    by 0x4EC89B8: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==20645==    by 0x406AF9: void std::call_once<date::time_zone::init() const::{lambda()#1}>(std::once_flag&, date::time_zone::init() const::{lambda()#1}&&) (mutex:698)
==20645==    by 0x40486C: date::time_zone::init() const (tz.cpp:2114)
==20645==    by 0x404C70: date::time_zone::get_info_impl(std::chrono::time_point<date::local_t, std::chrono::duration<long, std::ratio<1l, 1l> > >) const (tz.cpp:2149)
==20645==    by 0x418E5C: date::local_info date::time_zone::get_info<std::chrono::duration<long, std::ratio<1l, 1l> > >(std::chrono::time_point<date::local_t, std::chrono::duration<long, std::ratio<1l, 1l> > >) const (tz.h:904)
==20645==    by 0x418CB2: std::chrono::time_point<std::chrono::_V2::system_clock, std::common_type<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1l> > >::type> date::time_zone::to_sys_impl<std::chrono::duration<long, std::ratio<1l, 1l> > >(std::chrono::time_point<date::local_t, std::chrono::duration<long, std::ratio<1l, 1l> > >, date::choose, std::integral_constant<bool, false>) const (tz.h:947)
==20645== 

关于c++ - 在 C++ 中将带有时区的日期时间字符串转换为 UNIX 时间戳的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56191222/

相关文章:

Javascript,如何找到mysql时间戳样式中两个日期时间之间的差异

c++ - 如何处理类及其属性的抽象和特化?

python - 在 Python 中使用时区缩写名称解析日期/时间字符串?

jQuery Datepicker UI 仅在单击关闭按钮或完成按钮时关闭

python - 创建一个列,该列是从另一列中提取的特定日期之后的一组列中日期数的总和

django - 如何在 Graphite 烯中格式化日期和日期时间

c++ - boost::system::error_category 处未解析的外部符号

c++ - 为什么我的基于 boost::asio 的应用程序不接受新连接

c++ - 创建一个向主窗体返回值的模态窗口

java - java中基本数据格式转换