-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.cpp
More file actions
37 lines (31 loc) · 1018 Bytes
/
Copy pathclock.cpp
File metadata and controls
37 lines (31 loc) · 1018 Bytes
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
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
int main() {
// Get the current system time
auto now = std::chrono::system_clock::now();
// Convert the system time to a time_t object
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
std::tm local_time;
// Use localtime_s on MSVC (Windows), otherwise fallback to standard localtime
#if defined(_MSC_VER)
if (localtime_s(&local_time, &now_time_t) != 0) {
std::cerr << "Error retrieving local time." << std::endl;
return 1;
}
#else
std::tm* tm_ptr = std::localtime(&now_time_t);
if (tm_ptr == nullptr) {
std::cerr << "Error retrieving local time." << std::endl;
return 1;
}
local_time = *tm_ptr;
#endif
// Print the formatted date and time
// Format: YYYY-MM-DD HH:MM:SS
std::cout << "Current date and time: "
<< std::put_time(&local_time, "%Y-%m-%d %H:%M:%S")
<< std::endl;
return 0;
}