-
Notifications
You must be signed in to change notification settings - Fork 27
/
timezone.cpp
51 lines (39 loc) · 864 Bytes
/
timezone.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
#include "timezone.h"
#include <string>
#include <stdlib.h>
std::string local_tz;
bool tz_initialized = false;
void init_tz() {
if(tz_initialized) return;
//check if TZ is set, store current value
char* current_tz_env = getenv("TZ");
if(current_tz_env != 0) {
local_tz = std::string("TZ=");
local_tz += std::string(current_tz_env);
}
tz_initialized = true;
}
void set_utc_tz() {
//change TZ to UTC
putenv((char*)"TZ=UTC");
tzset();
}
void unset_utc_tz() {
if(!local_tz.empty()) {
putenv((char*)local_tz.c_str());
} else {
#ifndef _WIN32
unsetenv("TZ");
#else
putenv((char*)"TZ=");
#endif
}
tzset();
}
time_t mktime_utc(struct tm* timeinfo) {
init_tz();
set_utc_tz();
time_t time_utc = mktime(timeinfo);
unset_utc_tz();
return time_utc;
}