无法将带有秒数的字符串转换为从epoch转换为date_time

Unable to convert string with seconds from epoch to date_time

本文关键字:转换 epoch date time 字符串      更新时间:2023-10-16

我有一个字符串,其中包含来自时期的秒,如何将此字符串转换为格式
因此 -

年度HH:MM:SS

这里的月份应显示月号和日期。同样需要以24小时格式的时间。
例如:

2019-06-26 11:14:25

我曾尝试使用strptime,但还没有成功,有人可以帮助我做错了什么。
这是我到目前为止尝试的

int main()
{
    string timee = "12341234534";
    const char *ptr = timee.c_str();
    struct tm tim;
    strptime(ptr, "%S", &tim);
    time_t abcd = mktime(&tim);
    cout<<abcd;
    return 0;
}

在另一个stackoverflow链接上找到此代码段如何将包含时间的字符串变量转换为time_t类型C ?

您的问题有些混乱,但我想我想出了您想要什么...

如果时间是time_t兼容格式,则只需将其直接读取到time_t变量中,然后通过std::localtime转换它,以便您可以使用std::put_time以通缉格式输出它。

之类的东西
std::time_t time;
std::cin >> time;
std::cout << std::put_time(std::localtime(&time), "%F %T") << 'n';