Lauen_1

Stay foolish

2-7. Working with Time

在c14中已经,已经添加了chrono库,用于获取关于时间的库函数

1. Getting the Current Date and Time

 1 #include <ctime>
 2 #include <chrono>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 using namespace chrono;
 7 
 8 int main()
 9 {
10     auto currentTimePoint = system_clock::now();
11     auto currentTime = system_clock::to_time_t( currentTimePoint );
12     auto timeText = ctime( &currentTime );
13     cout << timeText << endl;
14     return 0;
15 }
View Code

2.Comparing Times

 1 #include <ctime>
 2 #include <chrono>
 3 #include <iostream>
 4 #include <thread>
 5 
 6 using namespace std;
 7 using namespace chrono;
 8 using namespace literals;
 9 
10 int main()
11 {
12     auto startTimePoint = system_clock::now();
13     this_thread::sleep_for(5s);
14     auto endTimePoint = system_clock::now();
15     auto timeTaken = duration_cast<milliseconds>(endTimePoint - startTimePoint);
16     cout << "Time Taken: " << timeTaken.count() << endl;
17     return 0;
18 }
View Code

posted on 2016-11-08 10:58  Lauen_1  阅读(121)  评论(0)    收藏  举报

导航