Skip to content

Commit be11bb0

Browse files
time_t_localtime.cpp
1 parent 4af5d6d commit be11bb0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

time_t_localtime.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <ctime>
3+
4+
//https://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
5+
6+
void get_time(int& year, int& month, int& day, int& hour, int& min, int& sec) {
7+
time_t now = time(NULL);
8+
struct tm* ltm = localtime(&now);
9+
10+
year = ltm->tm_year + 1900; // Year is # years since 1900
11+
month = ltm->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
12+
day = ltm->tm_mday;
13+
hour = ltm->tm_hour;
14+
min = ltm->tm_min;
15+
sec = ltm->tm_sec;
16+
}
17+
18+
int main() {
19+
int year, month, day, hour, min, sec;
20+
get_time(year, month, day, hour, min, sec);
21+
std::cout << "year: " << year << std::endl;
22+
std::cout << "month: " << month << std::endl;
23+
std::cout << "day: " << day << std::endl;
24+
std::cout << "hour: " << hour << std::endl;
25+
std::cout << "min: " << min << std::endl;
26+
std::cout << "sec: " << sec << std::endl;
27+
return 0;
28+
}
29+
30+
/**
31+
year: 2021
32+
month: 8
33+
day: 30
34+
hour: 1
35+
min: 50
36+
sec: 10
37+
**/

0 commit comments

Comments
 (0)