/Olson2POSIX

This library offers the ability to convert from Olson to POSIX timezones as well as it will store the Olson and POSIX in RTC memory to survive Deep Sleep. This library is adapted to work with the Arduino ESP32 and any other project that has similar libraries.

Primary LanguageC++MIT LicenseMIT

Olson2POSIX Arduino Lint

This library offers the ability to convert from Olson to POSIX TimeZones as well as it will store the Olson and POSIX in RTC memory to survive Deep Sleep. This library is adapted to work with the Arduino ESP32 and any other project that has similar libraries.

String getPOSIX(String inOlson): Returns the POSIX value from the Olson TimeZone, if it cannot find the TimeZone, "--MISSING--" (Olson2POSIX.TZMISSING) will be returned.

bool setCurrentTimeZone(): Sets the current POSIX TimeZone to the environment variable, must be done after a Deep Sleep, usually just before using localtime.

bool setOlsonTimeZone(String inOlson): Sets the current POSIX TimeZone to the environment variable once found, if not found will return false.

String getCurrentPOSIX(): Returns the current POSIX TimeZone in a String.

String getCurrentOlson(): Returns the current Olson TimeZone in a String.

bool setCurrentPOSIX(String NewPOSIX): Sets the new POSIX as the current POSIX TimeZone and environment variable.

bool beginOlsonFromWeb(WiFiClient &client): Starts the operation to ask for a TimeZone based on IP address (WiFi needs to be on and connected). As of 1.4, requires WiFiClient object.

endOlsonFromWeb(): This tells the operation to end, do so once the function gotOlsonFromWeb() returns true.

bool gotOlsonFromWeb(): Returns true if it recieved an Olson TimeZone.

int getOlsonWebError(): Returns a value for HTTP Response based on an error value (normal acceptances will not be here).

init(): Do this from the switch (wakeup_reason) in default: or not, the above functions do it if you don't.

The current Olson and POSIX TimeZones will remain after a Deep Sleep, but not a reboot, so be sure to record them if you're wanting them to stay.

How to use in your Watchy.

#include <Olson2POSIX.h>

...

Olson2POSIX OP; // Declare Olson2POSIX object

I would recommend including SmallNTP and SmallRTC for a complete suite of functionality.

The most common use is at the switch (wakeup_reason), in default: do an OP.init(); and if you're obtaining the POSIX or Olson TimeZone from Non-Volatile Storage (NVS), do so here and set the timezone using the appropriate function (OP.setOlsonTimeZone(?) or OP.setCurrentPOSIX(?), depending on what you retrieved from NVS). Then when you RTC.read, you need to OP.setCurrentTimeZone() to set the timezone to the one you've setup from the default:. Once that is done, you can use localtime as such:

    OP.setCurrentTimeZone();
    tmElements_t T;
    RTC.read(T);
    struct tm * TM = localtime(&makeTime(T));
    currentTime.Second = TM->tm_sec;
    currentTime.Minute = TM->tm_min;
    currentTime.Hour = TM->tm_hour;
    currentTime.Wday = TM->tm_wday;
    currentTime.Day = TM->tm_mday;
    currentTime.Month = TM->tm_mon + 1;
    currentTime.Year = TM->tm_year;

To get the current TimeZone, you would:

  if (OP.beginOlsonFromWeb()){
    int s = 0;
    bool b = false;
    while (!b && s < 100){
      s++; 
      delay(50);
      b = OP.gotOlsonFromWeb();
  }
  if (b) OP.setOlsonTimeZone(OP.getCurrentOlson());
  OP.endOlsonFromWeb();

From there, the TimeZone will be stored in the class (survives Deep Sleep, but not resets).