-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
62 additions
and
5 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
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
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,38 @@ | ||
#include <ctime> | ||
#include <WString.h> | ||
|
||
#include <devices/virtual.h> | ||
|
||
namespace OswDevices { | ||
#ifdef OSW_EMULATOR | ||
time_t Virtual::getTimezoneOffset(const time_t& timestamp, const String& timezone) { | ||
/* | ||
This code is just copy-pasted from the ESP32, as they both use the time.h library. | ||
*/ | ||
|
||
bool hasOldTimezone = getenv("TZ") != nullptr; | ||
String oldTimezone; // Variable to hold local copy, as the value by getenv() may change after a setenv() | ||
if(hasOldTimezone) | ||
oldTimezone = getenv("TZ"); | ||
|
||
setenv("TZ", timezone.c_str(), 1); // overwrite the TZ environment variable | ||
tzset(); | ||
std::tm localTm = *std::localtime(×tamp); | ||
|
||
setenv("TZ", "UTC0", 1); // overwrite the TZ environment variable | ||
tzset(); | ||
std::tm utcTm = *std::localtime(×tamp); | ||
std::time_t utc = std::mktime(&utcTm); | ||
std::time_t local = std::mktime(&localTm); // this removes the "local"/dst offsets by UTC from our localized timestamp -> we get the UTC timestamp INCLUDING the local offsets! | ||
OSW_LOG_I("local: ", local, " utc: ", utc); | ||
OSW_LOG_I("diff: ", local - utc); | ||
if(hasOldTimezone) { | ||
setenv("TZ", oldTimezone.c_str(), 1); // restore the TZ environment variable | ||
tzset(); | ||
} | ||
if(utc == -1 or local == -1) | ||
throw std::logic_error("Could not represent times in std::time_t?!"); | ||
return local - utc; | ||
} | ||
#endif | ||
} |
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
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