Skip to content

Commit

Permalink
v0.14.1 (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmicro committed Mar 26, 2023
2 parents 7a5c4c1 + 5df7a08 commit 787df50
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/devices/virtual.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <OswLogger.h>
#include <devices/interfaces/OswTemperatureProvider.h>
#include <devices/interfaces/OswAccelerationProvider.h>
#include <devices/interfaces/OswHumidityProvider.h>
Expand Down Expand Up @@ -93,6 +94,9 @@ class Virtual : public OswTemperatureProvider, public OswAccelerationProvider, p
virtual unsigned char getTimeProviderPriority() override {
return this->priority;
};
#ifdef OSW_EMULATOR
virtual time_t getTimezoneOffset(const time_t& timestamp, const String& timezone) override;
#endif
private:
const unsigned char priority;
};
Expand Down
21 changes: 18 additions & 3 deletions src/devices/esp32.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <time.h> // The native ESP32 clock is wrapped by the standard time header
#include <ctime> // The native ESP32 clock is wrapped by the standard time header
#include <cstdlib>
#include <sys/cdefs.h>
#include <sys/time.h>
Expand Down Expand Up @@ -82,13 +82,28 @@ time_t OswDevices::NativeESP32::getTimezoneOffset(const time_t& timestamp, const

setenv("TZ", timezone.c_str(), 1); // overwrite the TZ environment variable
tzset();
const time_t utc = mktime(std::gmtime(&timestamp));
const time_t local = mktime(std::localtime(&timestamp));
std::tm localTm = *std::localtime(&timestamp);

setenv("TZ", "UTC0", 1); // overwrite the TZ environment variable
tzset();
std::tm utcTm = *std::localtime(&timestamp);
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!

// Why not just use this ↓ ? Because the code below works with GMT to UTC and also
// gives the mktime() a non-localozed timestamp, causing the DST to be dropped.
// Therefore it works great in the winter, but breaks in the summer (...).
// setenv("TZ", timezone.c_str(), 1); // overwrite the TZ environment variable
// tzset();
// const time_t utc = mktime(std::gmtime(&timestamp));
// const time_t local = mktime(std::localtime(&timestamp));

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;
}

Expand Down
38 changes: 38 additions & 0 deletions src/devices/virtual.cpp
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(&timestamp);

setenv("TZ", "UTC0", 1); // overwrite the TZ environment variable
tzset();
std::tm utcTm = *std::localtime(&timestamp);
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
}
2 changes: 1 addition & 1 deletion src/hal/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void OswHal::updateTimezoneOffsets() {
// Okay, we still don't have a timezone offset -> set it to 0
this->timezoneOffsetPrimary = 0;
this->timezoneOffsetSecondary = 0;
OSW_LOG_W("Could not resolve timezone offsets!");
OSW_LOG_W("Could not resolve timezone offsets (no capable provider is available)!");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/osw_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ void OswHal::setup(bool fromLightSleep) {
this->setupPower(fromLightSleep);
this->displayOn();
}
this->updateTimezoneOffsets(); // Always update, just in case DST changed during (light) sleep
this->devices()->setup(fromLightSleep);
this->devices()->update(); // Update internal cache to refresh / initialize the value obtained by calling this->getAccelStepCount() - needed for e.g. the step statistics!
this->updateTimezoneOffsets(); // Always update, just in case DST changed during (light) sleep - after all devices are setup/updated, as they might use their time for this calculation
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1 && defined(OSW_FEATURE_STATS_STEPS)
this->environment()->setupStepStatistics();
#endif
Expand Down

0 comments on commit 787df50

Please sign in to comment.