gpt4 book ai didi

c++ - 将 chrono::duration 类型转换为 std::tm 类型

转载 作者:行者123 更新时间:2023-11-30 03:26:31 29 4
gpt4 key购买 nike

我正在编写一个程序,它从用户那里获取输入日期,使用它来初始化一个 tm struct,然后使用 chrono::time_points 执行一些 chrono::duration 操作,比如获取年龄。

这是我的代码:

#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>

using namespace std;

int main(){
//representing a date
tm *birthday = new tm{00, 30, 00, 07, 11, 97};
//convert to time t
time_t bt = mktime(birthday);
//convert time_t to time_point
chrono::system_clock::time_point t = chrono::system_clock::from_time_t(bt);
chrono::system_clock::time_point now = chrono::system_clock::now();
/*.. Testing
time_t nn = chrono::system_clock::to_time_t(now);
time_t tnn = chrono::system_clock::to_time_t(t);
*/

chrono::system_clock::duration lft = now - t;
//convert to timepoint
chrono::system_clock::time_point tlft{lft};
time_t lifetime = chrono::system_clock::to_time_t(tlft);
cout << put_time(localtime(&lifetime), "%F %T") << endl;

return 0;
}

我的输出是这样的:
$> 1990-02-10 09:42:46

因此,根据我的理解,它对刻度执行简单的数学减法并使用本地时间,将其转换为自 EPOCH 以来的日期,这就是它给我 1990 年的原因。我想知道,有什么方法可以将持续时间直接转换为 struct tm,这样我应该得到大约 20 年的时间吗?

最佳答案

以下是如何以您选择的单位提取持续时间:

std::chrono::duration<double> lft = now - t;
using year = std::chrono::duration<int, std::ratio<31557600>>;
auto nby = std::chrono::duration_cast<year>(lft);
std::cout << nby.count() << "\n";

考虑到这一点,我建议实现以下风格:

struct Age
{
using year = std::chrono::duration<int, std::ratio<31'557'600>>;
using month = std::chrono::duration<int, std::ratio< 2'592'000>>;
using day = std::chrono::duration<int, std::ratio< 86'400>>;
using hour = std::chrono::hours;
using minute = std::chrono::minutes;
using second = std::chrono::seconds;

Age(std::chrono::system_clock::time_point birth)
: _age(std::chrono::system_clock::now() - birth)
{}

template<class Duration>
auto extract()
{
const auto result = std::chrono::duration_cast<Duration>(_age);
_age -= result;
return result;
}

friend std::ostream& operator<<(std::ostream& os, Age age)
{
const auto years = age.extract<year>();
const auto monthes = age.extract<month>();
const auto days = age.extract<day>();
const auto hours = age.extract<hour>();
const auto minutes = age.extract<minute>();
const auto seconds = age.extract<second>();

return os << years.count()
<< ":" << std::setw(2) << std::setfill('0') << monthes.count()
<< ":" << std::setw(2) << std::setfill('0') << days.count()
<< " " << std::setw(2) << std::setfill('0') << hours.count()
<< ":" << std::setw(2) << std::setfill('0') << minutes.count()
<< ":" << std::setw(2) << std::setfill('0') << seconds.count()
;
}

private:
std::chrono::duration<double> _age;
};

打印 20:01:10 12:43:40 with your example date (live demo) .

关于c++ - 将 chrono::duration 类型转换为 std::tm 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48278913/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com