Go Down

Topic: Library for time zone conversions and automatic DST adjustments (Read 45936 times) previous topic - next topic

mickeypop

Mitch

WiFi101 is just a WiFi shield for Arduino and will run time.h just fine
MKR1000 a Cortex-M0 MCU, this i have used time.h on this core


Time.h will run on almost any Atmel chip and most other processors.  I have even used time.h on the attiny13 and thats not much memory.

i should note i have used time.h on ST Cortex-M0, 80C52, several Atmel, ESP8266, ESP32 and TI 430 processors with good results.   only the 80C52 needed a special #ifdef to compile

i expect your library should run on all of these since it is really just an interface to the time.h in the first place. 

The ONLY chip specific code I see are the EEPROM read/write functions and fortunately you already have the #ifdefs in place to deal with this.


only the "#include <WProgram.h> " is questionable for other IDEs than Arduino since WProgram.h is part of Arduino.   "else if ARDUINO < 100" will catch lesser Arduino versions and remove the Arduino headers in NON-Arduino IDEs.

  Something like this should remove the Arduino headers if used outside of the Arduino IDE.
   #if ARDUINO >= 100
   #include <Arduino.h>
   #else if ARDUINO < 100
   #include <WProgram.h>
   #endif



Excellent work.


MitchSF

This one looks like a combination of Fabrice Weinberg's NTPClient, and the Timzone library by Jack Christensen:

https://github.com/BinEP/NTPClientZone


It's devoid of examples, but it's on my list to try because it puts both NTP time, and timezone calculations, in one place.

EDIT: It doesn't appear to work. Here's what I used to test:

Code: [Select]
#include <WiFi.h>
#include <TimeLib.h>
#include <NTPClientTZ.h>
#include <SPI.h>



// initialize and instantiate tz object
//TimeChangeRule dSt; // = {};
//TimeChangeRule sTd; // = {};
//Timezone1 tz(dSt, sTd);

const char *ssid     = "SSID";
const char *password = "Password";

TimeChangeRule dSt = {"DST", 2, 1, 3, 2, 0}; // March, 2nd Sunday, 2:00am
TimeChangeRule sTd = {"STD", 1, 1, 11, 2, 0}; // November, 1st Sunday, 2:00am

// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
NTPClientTZ timeClient("us.pool.ntp.org", 3600, 60000, dSt, sTd);

void setup() {
  Serial.begin(115200);
  delay(500);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
//changeRule();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}
void changeRule() {
// rule, week of month, day of week, month, hour, offset
TimeChangeRule dSt = {"DST", 2, 1, 3, 2, 0}; // March, 2nd Sunday, 2:00am
TimeChangeRule sTd = {"STD", 1, 1, 11, 2, 0}; // November, 1st Sunday, 2:00am
timeClient.setRule(dSt, sTd); // set rules
}

ERMESuino

HELLO!
I have some questions about this library and RTC library of arduino IDE.

so i work in a project in which i use the ds1307 module and i want to use  both libraries, can i do that with out any problem?

An other question is; if i write the code for the  ds1307 could i replace the ds1307 with ds3231 without changes in the code?

Thank you in advance.



MitchSF

You can use Time, TimeZone, and one of the RTC libraries together. In fact, TimeZone is dependent on Time.

I believe the DS1307 library will work with the DS3231.

tigger

Brilliant library
I've been sweating blood trying to get the daylight saving rules to work in Excel prior to transferring to code and getting nowhere fast.
I have an energy monitoring programme that counts the meter (gas, electricity etc.) pulses that puts the RTC (GMT) time/date-stamp on it and for clarity needed the local time/date as well.
Saved me a lot of midnight oil, especially galling as I just found I installed the library last year.
I will get the Excel version going for personal satisfaction, but the rules to compare the system date/time against the changeover date/time are a lot harder than they appear.
Personally speaking, I'm in favour of leaving the damn clocks alone, as "putting the clocks back" in October just brings the gloom on one hour earlier.

MitchSF

Agreed. I hate DST. Imagine writing DST rules for Israel, where they take religious holidays into account, too!

odometer

Brilliant library
I've been sweating blood trying to get the daylight saving rules to work in Excel prior to transferring to code and getting nowhere fast.
I have an energy monitoring programme that counts the meter (gas, electricity etc.) pulses that puts the RTC (GMT) time/date-stamp on it and for clarity needed the local time/date as well.
Saved me a lot of midnight oil, especially galling as I just found I installed the library last year.
I will get the Excel version going for personal satisfaction, but the rules to compare the system date/time against the changeover date/time are a lot harder than they appear.
Personally speaking, I'm in favour of leaving the damn clocks alone, as "putting the clocks back" in October just brings the gloom on one hour earlier.
Here is one method, which probably would not be too hard to make work in Excel:
https://forum.arduino.cc/index.php?topic=370460.msg2554547#msg2554547

Go Up