Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c++ - 如何在C ++中获取当前时间和日期?(How to get current time and date in C++?)

有没有一种跨平台的方式来获取C ++中的当前日期和时间?

  ask by Max Frai translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++ 11 you can use std::chrono::system_clock::now()

(在C ++ 11中,您可以使用std::chrono::system_clock::now())

Example (copied from en.cppreference.com ):

(示例(从en.cppreference.com复制):)

#include <iostream>
#include <chrono>
#include <ctime>    

int main()
{
    auto start = std::chrono::system_clock::now();
    // Some computation here
    auto end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s
";
}

This should print something like this:

(这应该是这样的:)

finished computation at Mon Oct  2 00:59:08 2017
elapsed time: 1.88232s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...