-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdate_time.cpp
executable file
·118 lines (91 loc) · 3.72 KB
/
date_time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
How to get the execution time of any program on Linux Shell:
$time ./<your_application>
*/
#include <iostream>
#include <time.h>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <thread>
template <typename Clock, typename Duration>
std::ostream &
operator<<(std::ostream &stream,
const std::chrono::time_point<Clock, Duration> &time_point) {
const time_t time = Clock::to_time_t(time_point);
struct tm tm;
localtime_r(&time, &tm);
return stream << std::put_time(&tm, "%c"); // Print standard date&time
}
void timeLapseFirstExample() {
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::microseconds(1000));
auto elapsed = std::chrono::high_resolution_clock::now() - start;
auto microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
std::cout << "time lapse in microseconds:" << microseconds << std::endl;
}
void cTime() {
time_t epoch = std::time(NULL);
std::cout << "seconds since the epoch began: " << epoch << std::endl;
tm *t = gmtime(&epoch);
std::cout << "current date and time: " << 1900 + t->tm_year << "/"
<< 1 + t->tm_mon << "/" << t->tm_mday << std::endl;
std::cout << "current date and time: " << asctime(gmtime(&epoch))
<< std::endl;
}
int main() {
using namespace std::literals;
const std::chrono::time_point<std::chrono::system_clock> now =
std::chrono::system_clock::now();
const std::time_t t_c = std::chrono::system_clock::to_time_t(now - 24h);
std::cout << "24 hours ago, the time was "
<< std::put_time(std::localtime(&t_c), "%F %T.\n") << std::flush;
const std::chrono::time_point<std::chrono::steady_clock> start =
std::chrono::steady_clock::now();
std::cout << "Different clocks are not comparable: \n"
" System time: "
<< now.time_since_epoch()
<< "\n"
" Steady time: "
<< start.time_since_epoch() << '\n';
// std::chrono::time_point now1 = std::chrono::system_clock::now();
// std::cout << now0.time_since_epoch() << std::endl;
// std::chrono::time_point<std::chrono::system_clock> now1 =
// std::chrono::system_clock::now();
// std::cout << now1 << std::endl;
// // std::chrono::time_point<std::chrono::steady_clock> now2 =
// // std::chrono::system_clock::now();
// std::time_t t_c =
// std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
// std::cout << t_c << std::endl;
// std::chrono::nanoseconds is : typedef duration<signed int, std::nano>
// nanoseconds;
// timePoint = std::chrono::system_clock::now();
// std::chrono::system_clock::to_time_t(timePoint);
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
timePoint = std::chrono::time_point_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now());
std::cout << timePoint << std::endl;
// std::cout << std::chrono::system_clock::to_time_t(timePoint) << std::endl;
std::cout << timePoint.time_since_epoch() << std::endl;
{
const auto now = std::chrono::system_clock::now();
time_t time = std::chrono::system_clock::to_time_t(now);
std::cout << time << "\n";
std::cout << std::chrono::duration_cast<std::chrono::seconds>(
now.time_since_epoch())
.count()
<< "\n";
std::cout << std::chrono::system_clock::from_time_t(time)
.time_since_epoch()
.count()
<< "\n";
std::cout << std::chrono::system_clock::time_point(
std::chrono::seconds(time))
.time_since_epoch()
.count()
<< "\n";
}
return 0;
}