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

查看:150
本文介绍了使用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

推荐答案

< chrono> 库仅处理时间和不是日期,除了 system_clock ,可以将其时间点转换为 time_t 。所以使用< chrono> 的日期不会改善很多。希望我们得到像 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.

那就是说,你可以使用< chrono> 以下方式:

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 ++中输出日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆