【发布时间】:2021-12-08 19:20:46
【问题描述】:
我一直在升级一些旧代码,并在可能的情况下尝试更新到 c++11。以下代码是我用来在程序中显示时间和日期的方式
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
const std::string return_current_time_and_date() const
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
我想使用 std::chrono(或类似的)以类似的格式输出当前时间和日期,但我不确定如何去做。任何帮助将不胜感激。谢谢
【问题讨论】:
-
如果已经有这样的问题,请查找,如果没有,请查询。
-
C++11 不会让你变得更好。 chrono 更多的是关于时间(某件事花了多长时间)而不是时间类型的东西。不过,您可以查看 Boost Date Time。它具有更强大的日期和时间功能。
-
使用计时和输出获取日期和时间?请参阅first example。
-
@DyP 这看起来很有希望,但我需要从函数中返回一个字符串,并且不知道如何使用 std::put_time 进行此操作
标签: c++ date c++11 time chrono