C++11 获取当前日期和时间作为字符串
C++11 get current date and time as string
c++11 中以字符串形式获取日期和时间的最新方法是什么?
我知道std::put_time
,但参考资料说我只会在流中使用它。
有std::chrono::system_clock
提供to_time_t
返回时间作为time_t
而缺少日期,不是吗?
我可以使用像 bames53 这样的字符串流:使用 std::chrono 在C++中输出日期和时间,但这似乎是一种解决方法。
使用 Howard Hinnant 的免费开源标头日期时间库,您可以在一行代码中获取当前 UTC 时间作为std::string
:
std::string s = date::format("%F %T", std::chrono::system_clock::now());
我刚刚运行了这个,字符串包含:
2017-03-30 17:05:13.400455
没错,它甚至可以为您提供完全的精度。 如果您不喜欢这种格式,可以使用所有strftime
格式标志。 如果您想要本地时间,还有一个时区库可用,尽管它不仅仅是标题。
std::string s = date::format("%F %T %Z", date::make_zoned(date::current_zone(),
std::chrono::system_clock::now()));
输出:
2017-03-30 13:05:13.400455 EDT
针对 C++20 的更新
现在可以使用 std::lib 标头<chrono>
和<format>
来完成此操作:
#include <chrono>
#include <format>
#include <string>
int
main()
{
std::string s1 = std::format("{:%F %T}", std::chrono::system_clock::now());
std::string s2 = std::format("{:%F %T %Z}",
std::chrono::zoned_time{std::chrono::current_zone(),
std::chrono::system_clock::now()});
}
演示。
更简单:
string CurrentDate()
{
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char buf[100] = {0};
std::strftime(buf, sizeof(buf), "%Y-%m-%d", std::localtime(&now));
return buf;
}
也根据时间调整格式。
请注意,我怀疑这不适用于多线程代码,因为std::localtime()
返回指向内部结构的指针。
首先,std::time_t
确实捕获了日期和时间,因为它通常表示从 1970 年 1 月 1 日开始的秒数。
处理 C++11 中的日期没有很好的支持。如果您不想这样做,您仍然必须依赖 boost,主要是手动的。以下是手动操作的方法。
您可以以线程安全的方式将其与任何std::chrono::*clock
一起使用,例如 std::system_clock
,如下所示:
std::string get_date_string(std::chrono::time_point t) {
auto as_time_t = std::chrono::system_clock::to_time_t(t);
struct tm tm;
if (::gmtime_r(&as_time_t, &tm))
if (std::strftime(some_buffer, sizeof(some_buffer), "%F", &tm))
return std::string{some_buffer};
throw std::runtime_error("Failed to get current date as string");
}
在其他地方,您可以发布:
get_date_string(std::system_clock::now());
这个解决方案的相对好处是,在API级别,您仍在使用现代,可移植的C++概念,例如std::chrono::time_point
,当然还有std::string
。
这样的时间戳;
#include <iostream>
#include <chrono>
#include <ctime>
int main() {
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(now);
char timedisplay[100];
struct tm buf;
errno_t err = localtime_s(&buf, &start_time);
if (std::strftime(timedisplay, sizeof(timedisplay), "%H:%M:%S", &buf)) {
std::cout << timedisplay << 'n';
}
}
以类似的方式约会。
您可以使用下面给出的代码片段,因为它将满足您的目的。这里使用 time.h 头文件作为所需的 localtime() 函数,然后使用带有所需参数的 strftime() 函数将给出输出并将其作为字符串返回。
#include <iostream>
#include <string>
#include <time.h>
std::string current_date();
std::string current_time();
int main(){
std::cout<<"Current date => "<<current_date()<<"n";
std::cout<<"Current time => "<<current_time()<<"n";
}
std::string current_date(){
time_t now = time(NULL);
struct tm tstruct;
char buf[40];
tstruct = *localtime(&now);
//format: day DD-MM-YYYY
strftime(buf, sizeof(buf), "%A %d/%m/%Y", &tstruct);
return buf;
}
std::string current_time(){
time_t now = time(NULL);
struct tm tstruct;
char buf[40];
tstruct = *localtime(&now);
//format: HH:mm:ss
strftime(buf, sizeof(buf), "%X", &tstruct);
return buf;
}
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 通过字符串来计算长度需要更长的时间,而不是移动字符串几次?
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- 如何将时间字符串 (M:SS) 转换为浮点数
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 时间持续时间到时间字符串
- 如何将我的每个矢量字符串转换为纪元时间日期
- 如何找到修改后的字符串与原始字符串相等的时间
- 在C++中修改字符串的BigO时间复杂度是多少
- 格式化的时间字符串在解析为time_point时会产生小时差异
- 使用 ctime 保存新时间点会覆盖旧字符串?
- 将日期字符串转换为 unix 时间戳
- 有序地图字符串搜索与整数搜索之间的时间复杂度
- 字符串连接时间复杂度 c++
- 提升,从字符串问题中获得时间
- C 将字符串时间戳转换为STD :: Chrono :: System_Clock :: Time_point
- 将字符串时间转换为UNIX时间戳
- 最接近地图的字符串/时间戳