获取当前时间和日期
#include <iostream>
#include <chrono>
int main()
{
auto currentTimePoint = std::chrono::system_clock::now();
auto currentTime = std::chrono::system_clock::to_time_t(currentTimePoint);
auto timeText = ctime(¤tTime);
std::cout << "currentTimePoint: " << currentTimePoint << "\n";
std::cout << "currentTime: " << currentTime << "\n";
std::cout << "timeText: " << timeText << "\n";
}
输出:
比较时间
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
auto startTimePoint = std::chrono::system_clock::now();
//std::this_thread::sleep_for(std::chrono::seconds(5));
using namespace std::chrono_literals;
std::this_thread::sleep_for(5s);
auto endTimePoint = std::chrono::system_clock::now();
auto timeTaken = std::chrono::duration_cast<std::chrono::milliseconds>(endTimePoint - startTimePoint);
std::cout << "time taken: " << timeTaken;
}
输出:
看上去也不是特别准确