-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functions to switch the ENV timezone to and from UTC for use with mkt…
…ime.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "timezone.h" | ||
|
||
#include <string> | ||
|
||
std::string stored_env_tz; | ||
|
||
void store_env_tz() { | ||
//check if TZ is set, store current value | ||
if(stored_env_tz.empty()) { | ||
char* current_tz_env = getenv("TZ"); | ||
if(current_tz_env != 0) { | ||
stored_env_tz = std::string("TZ="); | ||
stored_env_tz += std::string(current_tz_env); | ||
} | ||
} | ||
} | ||
|
||
void set_utc_tz() { | ||
//change TZ to UTC | ||
putenv((char*)"TZ=UTC"); | ||
tzset(); | ||
} | ||
|
||
void unset_utc_tz() { | ||
if(!stored_env_tz.empty()) { | ||
putenv((char*)stored_env_tz.c_str()); | ||
} else { | ||
#ifndef _WIN32 | ||
unsetenv("TZ"); | ||
#else | ||
putenv((char*)"TZ="); | ||
#endif | ||
} | ||
tzset(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef CORE_TIMEZONE_H | ||
#define CORE_TIMEZONE_H | ||
|
||
extern "C" { | ||
void store_env_tz(); | ||
void set_utc_tz(); | ||
void unset_utc_tz(); | ||
}; | ||
|
||
#endif |