std :: chrono :: system_clock :: now()考虑操作系统配置的时区 [英] std::chrono::system_clock::now() considering the OS configured time zone

查看:5347
本文介绍了std :: chrono :: system_clock :: now()考虑操作系统配置的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编程一个在BusyBox嵌入式linux上运行的C ++代码。我的代码及其库有几个调用 std :: chrono :: system_clock :: now()获取当前时间。

I´m programming a C++ code that is running on a BusyBox embedded linux. My code and its libraries has several calls to std::chrono::system_clock::now() to get the current time.

从现在开始,我的文件夹被配置为dafault时区(UTC),一切正常,进程正常运行,结果正常。

Since now my box was configured as dafault time zone (UTC) and everything works fine, processes running and results ok.

现在我不得不将我的linux设置为不同的时区。然后我在框中配置 / etc / profile

Now I had to set my linux to stay in a different timezone. Then I did it by configuring in the box /etc/profile:

export TZ=UTC+3

当我发出 date 命令和控制台我得到正确的时间,但我调用 std :: chrono :: system_clock :: now()我仍然得到UTC时间,而不是 date 命令(正确的时间)中显示的时间。

When I issue date command and the console I get the correct time, but my calls to std::chrono::system_clock::now() I´m still getting the UTC time, not the time that is shown in the date command (the correct time).

我不想改变我所有的现在()调用 - 有成百上千...这是导致我的进程使用不同的时间比正确的时间,设置在控制台。

I don´t want to change all my now() calls - there are hundreds on them... And that is causing my processes to work with different time than the correct time, set on the console.

有没有办法解决这个问题,而不改变我的代码?

IS there any way to solve that without changing my code ? Anything I´m missing here ?

感谢您的帮助。

推荐答案

虽然标准没有指定,但 std :: chrono :: system_clock :: now()的每个实现都跟踪 Unix Time ,这是非常接近UTC的。

Though unspecified by the standard, every implementation of std::chrono::system_clock::now() is tracking Unix Time, which is a very close approximation to UTC.

如果你想翻译 std :: chrono :: system_clock :: now()到本地时间,你可以将 system_clock :: time_point code> time_t 通过 system_clock :: to_time_t ,然后以您的方式通过C API(例如 localtime ),或者您可以尝试建立在< chrono> 之上的现代时区库:

If you want to translate std::chrono::system_clock::now() to local time, you can either translate system_clock::time_point to time_t via system_clock::to_time_t, and then work your way through the C API (e.g. localtime), or you might try this modern timezone library which is built on top of <chrono>:

https://howardhinnant.github.io/date/tz.html

您可以使用此方法获取当前本地时间,如下所示:

You would use this to get the current local time like this:

#include "tz.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    auto t = make_zoned(current_zone(), system_clock::now());
    std::cout << t << '\n';
}

make_zoned 工厂函数,以 system_clock 支持的任何精度(例如纳秒)返回类型 zoned_time 。它是 time_zone system_clock :: time_point 的配对。

make_zoned is a factory function which returns the type zoned_time with whatever precision your system_clock supports (e.g. nanoseconds). It is a pairing of a time_zone and a system_clock::time_point.

你可以得到一个 std :: chrono :: time_point local_time< Duration> this:

You can get a local_time<Duration> which is a std::chrono::time_point like this:

    auto t = make_zoned(current_zone(), system_clock::now());
    auto lt = t.get_local_time();
    std::cout << lt.time_since_epoch().count() << '\n';

虽然库有一个远程API来下载 IANA时区数据库,该API可以通过使用 -DHAS_REMOTE_API = 0 进行编译而禁用。有关详情,请参阅安装说明。禁用远程API后,您必须手动从 IANA时区数据库下载数据库(这只是a tar.gz )。

Though the library has a remote API to download the IANA timezone database automatically, that API can be disabled by compiling with -DHAS_REMOTE_API=0. This is all detailed in the installation instructions. With the remote API disabled, you would have to manually download the database from IANA timezone database (it's just a tar.gz).

如果您需要当前UTC偏移,可以这样获得: / p>

If you need the current UTC offset, that can be obtained like this:

    auto t = make_zoned(current_zone(), system_clock::now());
    auto offset = t.get_info().offset;
    std::cout << offset << '\n';

在上面的代码段中,我利用了chrono_io.h 在同一个github存储库中找到,以打印 offset offset 有类型 std :: chrono :: seconds 。这只是我的输出:

In the snippet above, I'm taking advantage of "chrono_io.h" found at the same github repository to print offset. offset has type std::chrono::seconds. This just output for me:

-14400s



< - p>( - 0400)

(-0400)

最后,如果要发现当前时区的IANA名称, / p>

Finally, if you want to discover the IANA name of your current timezone, that is simply:

std::cout << current_zone()->name() << '\n';

这对我来说只是输出:

America/New_York

name() std :: string 。如果需要,可以记录该字符串,然后稍后使用该名称使用 time_zone ,即使这不是计算机的当前时区:

The type returned from name() is std::string. If desired, one could record that string, and then later use the time_zone with that name, even if that is not the computer's current time zone:

auto tz_name = current_zone()->name();
// ...
auto t = make_zoned(tz_name, system_clock::now()); // current local time in tz_name

这篇关于std :: chrono :: system_clock :: now()考虑操作系统配置的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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