Skip to content

Commit

Permalink
Functions to switch the ENV timezone to and from UTC for use with mkt…
Browse files Browse the repository at this point in the history
…ime.
  • Loading branch information
acaudwell committed Nov 27, 2013
1 parent f6354ea commit 4520ab0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
35 changes: 35 additions & 0 deletions timezone.cpp
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();
}
10 changes: 10 additions & 0 deletions timezone.h
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

0 comments on commit 4520ab0

Please sign in to comment.