我正在从包含Epoch以来纳秒的源中提取年/月/日/小时/分钟/秒/纳秒,使用以下问题的答案:
用C++从std::chrono::time_point中提取年/月/日等
但是,我的输入是不同的时区。 下面是我到目前为止的代码。
duration_cast
s之前是否需要进行转换? 否则小时/分钟/秒数可能会出错?我使用的是C++17,Clang,Linux和首选的标准库。 将在几个月内转向C++20,我怀疑这将简化答案。
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';
由于您的输入和输出在同一个时区,时区本身就变得不相关了。 这使得这个问题变得非常容易。 我们只需将纳秒的计数转换为所需的字段。 我推荐一个简短的公共域帮助器函数,将天数转换为{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';
}
输出:
y = 2020
m = 6
d = 14
H = 10
M = 24
S = 18
NS = 959736008