久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 std::chrono 在 C++ 中輸出日期和時間

Outputting Date and Time in C++ using std::chrono(使用 std::chrono 在 C++ 中輸出日期和時間)
本文介紹了使用 std::chrono 在 C++ 中輸出日期和時間的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我一直在升級一些舊代碼,并在可能的情況下嘗試更新到 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(或類似格式)以類似格式輸出當(dāng)前時間和日期,但我不確定如何去做.任何幫助將不勝感激.謝謝

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能夠?qū)⑵鋾r間點轉(zhuǎn)換為 time_t.因此,使用 表示日期不會有太大改善.希望我們能得到類似 chrono::date 的東西太遙遠(yuǎn)的未來.

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 可能會導(dǎo)致數(shù)據(jù)競爭.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"的內(nèi)容.

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

在一個不相關(guān)的注釋中,返回 const 對象在 C++11 中變得不可取;不能移動 const 返回值.我還刪除了尾隨 const,因為尾隨 const 僅對成員函數(shù)有效,而該函數(shù)無需成為成員.

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.

這篇關(guān)于使用 std::chrono 在 C++ 中輸出日期和時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 色综合一区二区三区 | 一片毛片 | 在线区| 黄色网址免费看 | 久久久久久久久淑女av国产精品 | 国产精品二区三区 | 国产精品视频 | 中文字幕亚洲视频 | 欧美日韩国产不卡 | 91超碰在线 | 欧美久久一区二区 | 午夜精品一区 | 97超碰人人| 久久久91 | 亚洲性免费 | 国产一区二区电影 | 国产亚洲成av人在线观看导航 | 欧美一级视频在线观看 | 自拍视频网 | 亚洲协和影视 | 午夜影院网站 | 午夜免费观看体验区 | 精品国产精品 | 国产专区视频 | 久久一级大片 | 日韩at| 一区二区av | 精品香蕉一区二区三区 | 成人不卡 | 一本大道久久a久久精二百 国产成人免费在线 | 天天影视综合 | 亚洲综合小视频 | 欧美一区免费在线观看 | 狠狠色综合久久丁香婷婷 | 日韩一区二区在线视频 | 亚洲精品一区在线 | 国产一区二区在线免费视频 | 99久久久久 | 黄色中文字幕 | 午夜爽爽爽男女免费观看 | 成人午夜影院 |