-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
time_fun.cpp
44 lines (36 loc) · 973 Bytes
/
time_fun.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
#include <iostream>
#include <time.h>
#include "time_fun.h"
time_t getTime_t(int y, int m, int d, int h, int n, int s) {
tm ttm = {0};
ttm.tm_year = y - 1900;
ttm.tm_mon = m - 1;
ttm.tm_mday = d;
ttm.tm_hour = h;
ttm.tm_min = n;
ttm.tm_sec = s;
ttm.tm_isdst = -1; // FIXME is -1 OK? Doesn't seem to work if we use something else
return mktime(&ttm);
}
std::string printDateTime(time_t t) {
struct tm timeinfo;
char buff[20];
strftime(buff, 20, "%m/%d/%Y %H:%M:%S", localtime_r(&t, &timeinfo));
std::string str(buff);
return str;
}
std::string printDateTimeCsv(time_t t) {
struct tm timeinfo;
char buff[20];
strftime(buff, 20, "%Y-%m-%d_%H:%M:%S", localtime_r(&t, &timeinfo));
std::string str(buff);
return str;
}
std::string printDateTimeFileName() {
time_t now = time(NULL);
struct tm timeinfo;
char buff[20];
strftime(buff, 20, "%Y%m%d_%H%M%S", localtime_r(&now, &timeinfo));
std::string str(buff);
return str;
}