使用 std::chrono 在 C++ 中输出日期和时间

Outputting Date and Time in C++ using std::chrono(使用 std::chrono 在 C++ 中输出日期和时间)

本文介绍了使用 std::chrono 在 C++ 中输出日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在升级一些旧代码,并在可能的情况下尝试更新到 c++11.下面的代码是我用来在我的程序中显示时间和日期的方式

I have been upgrading some old code and have been trying to update to c++11 where possible. The following code is how I used to display the time and date in my program

#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(或类似格式)以类似格式输出当前时间和日期,但我不确定如何去做.任何帮助将不胜感激.谢谢

I would like to output the current time and date in a similar format using std::chrono(or similar) but am unsure how to go about doing so. Any help would be greatly appreciated. Thanks

推荐答案

库只处理时间而不处理日期,除了 system_clock能够将其时间点转换为 time_t.因此,使用 表示日期不会有太大改善.希望我们能得到类似 chrono::date 的东西太遥远的未来.

The <chrono> library only deals with time and not dates, except for the system_clock which has the ability to convert its timepoints to time_t. So using <chrono> for dates will not improve things much. Hopefully we get something like chrono::date in the not too distant future.

也就是说,您可以通过以下方式使用 :

That said, you can use <chrono> in the following way:

#include <chrono>  // chrono::system_clock
#include <ctime>   // localtime
#include <sstream> // stringstream
#include <iomanip> // put_time
#include <string>  // string

std::string return_current_time_and_date()
{
    auto now = std::chrono::system_clock::now();
    auto in_time_t = std::chrono::system_clock::to_time_t(now);

    std::stringstream ss;
    ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
    return ss.str();
}

请注意,std::localtime 可能会导致数据竞争.localtime_r 或类似功能可能在您的平台上可用.

Note that std::localtime may cause data races. localtime_r or similar functions may be available on your platforms.

更新:

使用 Howard Hinnant 的 日期库 的新版本,您可以编写:

Using a new version of Howard Hinnant's date library you can write:

#include "date.h"
#include <chrono>
#include <string>
#include <sstream>

std::string return_current_time_and_date() {
  auto now = std::chrono::system_clock::now();
  auto today = date::floor<days>(now);

  std::stringstream ss;
  ss << today << ' ' << date::make_time(now - today) << " UTC";
  return ss.str();
}

这将打印出类似2015-07-24 05:15:34.043473124 UTC"的内容.

This will print out something like "2015-07-24 05:15:34.043473124 UTC".

在一个不相关的注释中,返回 const 对象在 C++11 中变得不可取;不能移动 const 返回值.我还删除了尾随 const,因为尾随 const 仅对成员函数有效,而该函数无需成为成员.

On an unrelated note, returning const objects has become undesirable with C++11; const return values cannot be moved from. I also removed the trailing const because trailing const is only valid for member functions and this function has no need to be a member.

这篇关于使用 std::chrono 在 C++ 中输出日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 std::chrono 在 C++ 中输出日期和时间

基础教程推荐