-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time_2.cpp
59 lines (49 loc) · 1.82 KB
/
Time_2.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
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;
int main()
{
{
// http://www.cplusplus.com/reference/ctime/time/
time_t curTime;
curTime = time(0);
time(&curTime);
cout << "Epoch Time In Seconds 1: " << curTime << endl;
// Method 1 to convert Time to String
string strTime = ctime(&curTime);
cout << "String Time in readable format: " << strTime << endl;
curTime += 1800;
unsigned long int l1Time = (unsigned long int)curTime;
unsigned long int l2Time = static_cast<unsigned long int>(curTime);
cout << l1Time << endl;
cout << l2Time << endl;
// OR
// Method 2 to convert Time to String
// http://www.cplusplus.com/reference/ctime/tm/
struct tm* locTime = localtime(&curTime);
cout << "Year: " << locTime->tm_year + 1900 << ", ";
cout << "Mon: " << locTime->tm_mon + 1 << ", ";
cout << "Day : " << locTime->tm_mday << ", ";
cout << "Hour: " << locTime->tm_hour << ", ";
cout << "Min : " << locTime->tm_min << ", ";
cout << "Sec : " << locTime->tm_sec << endl;
// OR
// Method 3 to convert Time to String
// localtime Convert time_t to tm as local time (function )
cout << asctime(locTime) << endl;
for (int i = 0; i < 100000000; i++)
{
int j = 5;
}
}
{
// get the current time
const auto now = std::chrono::system_clock::now();
// transform the time into a duration since the epoch
const auto epoch = now.time_since_epoch();
// cast the duration into seconds
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
cout << "Epoch Time In Seconds 2: " << seconds.count() << endl;
}
}