C++ C++;将时间读入不同的时区,然后提取年/月…纳秒
我使用以下问题的答案,从包含自历元起纳秒的源中提取年/月/日/小时/分钟/秒/纳秒: 但是,我的输入是不同的时区。下面是我到目前为止的代码C++ C++;将时间读入不同的时区,然后提取年/月…纳秒,c++,chrono,time-t,C++,Chrono,Time T,我使用以下问题的答案,从包含自历元起纳秒的源中提取年/月/日/小时/分钟/秒/纳秒: 但是,我的输入是不同的时区。下面是我到目前为止的代码 如何将以下内容转换为从不同时区读取 在执行duration\u casts之前是否需要转换?否则,小时/分钟/秒的数量可能是错误的 我使用的是C++17、Clang、Linux和标准库。将在几个月内转向C++20,我怀疑这将简化答案 using namespace std; using namespace std::chrono; using Clock
duration\u cast
s之前是否需要转换?否则,小时/分钟/秒的数量可能是错误的using namespace std;
using namespace std::chrono;
using Clock = high_resolution_clock;
using TimePoint = time_point<Clock>;
const nanoseconds nanosecondsSinceEpoch(nanosecondsSinceEpochTS);
const Clock::duration since_epoch = nanosecondsSinceEpoch;
const TimePoint time_point_sinc_epoch(since_epoch);
using days = duration<int, ratio_multiply<hours::period, ratio<24> >::type>;
system_clock::time_point now = time_point_sinc_epoch; // Do I need to handle timezone here before duration_cast?
system_clock::duration tp = now.time_since_epoch();
days d = duration_cast<days>(tp);
tp -= d;
hours h = duration_cast<hours>(tp);
tp -= h;
minutes m = duration_cast<minutes>(tp);
tp -= m;
seconds s = duration_cast<seconds>(tp);
tp -= s;
const uint64_t nanosSinceMidnight = tp.count();
time_t tt = system_clock::to_time_t(now);
tm utc_tm = *gmtime(&tt); // Presumably this needs to change
std::cout << utc_tm.tm_year + 1900 << '-';
std::cout << utc_tm.tm_mon + 1 << '-';
std::cout << utc_tm.tm_mday << ' ';
std::cout << utc_tm.tm_hour << ':';
std::cout << utc_tm.tm_min << ':';
std::cout << utc_tm.tm_sec << '\n';
使用名称空间std;
使用名称空间std::chrono;
使用时钟=高分辨率时钟;
使用时间点=时间点;
常数纳秒纳秒(纳秒);
常数时钟::自_epoch起的持续时间=纳秒自epoch;
常数时间点时间点新纪元(自新纪元);
使用天数=持续时间;
系统时钟::时间点现在=时间点新纪元;//我需要在这里处理时区吗?
系统时钟::持续时间tp=now.time_自_epoch()起;
d天=持续时间(tp);
tp-=d;
小时h=持续时间(tp);
tp-=h;
分钟m=持续时间(tp);
tp-=m;
秒s=持续时间(tp);
tp-=s;
const uint64_t nanosincemidnight=tp.count();
时间=系统时钟::到时间(现在);
tm utc_tm=*gmtime(&tt);//大概这需要改变
std::cout由于您的输入和输出位于同一时区,因此时区本身变得不相关。这使得这个问题很容易解决。只需将纳秒数转换为所需的字段。我建议将天数转换为{y,m,d}
数据结构
#include <chrono>
#include <iostream>
#include <tuple>
// Returns year/month/day triple in civil calendar
// Preconditions: z is number of days since 1970-01-01 and is in the range:
// [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
template <class Int>
constexpr
std::tuple<Int, unsigned, unsigned>
civil_from_days(Int z) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
z += 719468;
const Int era = (z >= 0 ? z : z - 146096) / 146097;
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
const Int y = static_cast<Int>(yoe) + era * 400;
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
const unsigned mp = (5*doy + 2)/153; // [0, 11]
const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31]
const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
}
int
main()
{
using namespace std;
using namespace std::chrono;
auto nanosecondsSinceEpochTS = 1592130258959736008;
using days = duration<int, ratio_multiply<hours::period, ratio<24> >>;
nanoseconds ns(nanosecondsSinceEpochTS);
auto D = floor<days>(ns);
ns -= D;
auto H = duration_cast<hours>(ns);
ns -= H;
auto M = duration_cast<minutes>(ns);
ns -= M;
auto S = duration_cast<seconds>(ns);
ns -= S;
auto [y, m, d] = civil_from_days(D.count());
cout << "y = " << y << '\n';
cout << "m = " << m << '\n';
cout << "d = " << d << '\n';
cout << "H = " << H.count() << '\n';
cout << "M = " << M.count() << '\n';
cout << "S = " << S.count() << '\n';
cout << "NS = " << ns.count() << '\n';
}
更新
在下面的评论中进行讨论后,发现纳秒信标
是UTC,而不是我推测的美国/芝加哥。这意味着UTC偏移量是时区和纳秒计数的函数,必须作为第一步添加到计数中。然后按照上面的指示获取每个字段
找到正确的偏移量是一个非常重要的过程,我不会尝试为其显示代码。一种技术是预先计算所有相关输入年份的{utc\u timestamp,utc\u offset}
,然后使用输入utc\u timestamp
查找正确的偏移量
在C++20中,可以简单地:
zoned_time zt{"America/Chicago", sys_time{nanoseconds{nanosecondsSinceEpochTS}}};
cout << zt << '\n';
如果想要积分场:
auto lt = zt.get_local_time(); // look up utc offset and add it to sys_time
year_month_day ymd{floor<days>(lt)}; // run civil_from_days
hh_mm_ss tod{lt - floor<days>(lt)}; // {H, M, S, NS} since local midnight
// copy each underlying integral value
auto y = int{ymd.year()};
auto m = unsigned{ymd.month()};
auto d = unsigned{ymd.day()};
auto H = tod.hours().count();
auto M = tod.minutes().count();
auto S = tod.seconds().count();
auto NS = tod.subseconds().count();
请注意,这只适用于2007年的美国/芝加哥模式。2007年之前,美国/芝加哥有不同的夏令时规则。这里有太多的未知因素,我无法给出答案。但是,如果您包括一个示例输入/输出对,那么我可能能够提供帮助。例如1596185732497162000->2020-07-31 08:55:32.497162000美国东部夏令时。@HowardHinnan当然。我需要将1592130258959736008转换为2020-06-1405:24:18.959736008。现在是芝加哥时间。我们之前提到过这一点,但我需要坚持使用标准库(我可以在几个月内转到cpp20)。明确地说,我需要提取“2020”、“06”、“14”、“05”、“24”、“18”和“959736008”作为整数。C++20civil\u from\u days
拼写为year\u month\u day ymd{floor(tp)}代码>。或者,如果您已经有整数形式的天数D
:year\u month\u day ymd{sys\u days{D}代码>这意味着自1970-01-01 00:00:00 UTC以来,您的输入计数为纳秒,并且您的输出字段位于美国/芝加哥。这一过程要困难得多。步骤1:查找与“America/Chicago”和输入的UTC时间相关联的UTC偏移量。根据一年中的不同时间(),该值目前在-6小时和-5小时之间变化。将UTC偏移量添加到纳秒计数中,以获得“本地纳秒”。然后按照答案中的“本地纳秒”进行操作。如果您的输入在几年的范围内,您可以在偏移量更改时预计算UTC时间表,并将其与每次更改时的偏移量一起存储。然后,您可以使用输入搜索表格以发现正确的偏移量。如果1970-01-01 00:00:00 UTC之后1592130258959736008ns处的所需UTC偏移量为-5小时,则可以将-5小时添加到1592130258959736008ns。棘手的部分是,如果在输入中加上6个月,则需要减去6h。美国/芝加哥UTC偏移量在冬季为-6h,夏季为-5h。它在3月的第二个星期日改变,并在11月的第一个星期日凌晨2点结束。在C++20中,这是一个微不足道的代码量。它将为您选择的任何时区应用UTC偏移量。
2020-06-14 05:24:18.959736008 CDT
auto lt = zt.get_local_time(); // look up utc offset and add it to sys_time
year_month_day ymd{floor<days>(lt)}; // run civil_from_days
hh_mm_ss tod{lt - floor<days>(lt)}; // {H, M, S, NS} since local midnight
// copy each underlying integral value
auto y = int{ymd.year()};
auto m = unsigned{ymd.month()};
auto d = unsigned{ymd.day()};
auto H = tod.hours().count();
auto M = tod.minutes().count();
auto S = tod.seconds().count();
auto NS = tod.subseconds().count();
#include "date/ptz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
auto nanosecondsSinceEpochTS = 1592130258959736008;
zoned_time zt{Posix::time_zone{"CST6CDT,M3.2.0,M11.1.0"},
sys_time<nanoseconds>{nanoseconds{nanosecondsSinceEpochTS}}};
cout << zt << '\n';
}
2020-06-14 05:24:18.959736008 CDT