Hi,
I'm a newbie.
I an wanting to build a module who's job consists of several tasks, but one is to also display both GMT and local time.
After wrestling around with a 1307 RTC and never getting it to work, I finally replaced the unit with a DS3031 RTC which is now successfully wired to my mega 2560 using the DS3231.h library.
My problem is, is that that library doesn't have anything like getGMT/uct in it so i am stuck building a conversion in the sketch that I really don't want to do if there is already a library out there that does the trick. It does have unix time that i may find useful.
Why is that conversion so unpleasant to do?
Personally I use GPS time in one application and coded the change between summer time and winter time and it works fine.
The TimeLib library does everything you need, including synchronizing the internal time with an RTC.
To interconvert day/date between local and UTC time, all you need to do is add or subtract the time zone correction in seconds to the unix time stamp that you get from the function now().
Simple example:
//time_make_and_add
#include "TimeLib.h"
tmElements_t te; //Time elements structure
time_t unixTime; // a time stamp
void setup() {
Serial.begin(9600);
// new internal clock setting.
// convert a date and time into unix time, offset 1970
te.Second = 0;
te.Hour = 23; //11 pm
te.Minute = 0;
te.Day = 1;
te.Month = 1;
te.Year = 2017-1970; //Y2K, in seconds = 946684800UL
unixTime = makeTime(te);
Serial.print("Example 1/1/2017 23:00 unixTime = ");
Serial.println(unixTime);
setTime(unixTime); //set the current time to the above entered
Serial.print("now() = ");
Serial.println(now());
// print as date_time
print_date_time();
// add
unixTime += 7200UL; //add 2 hours
setTime(unixTime);
Serial.println("After adding 2 hours");
Serial.print("now() = ");
Serial.println(now());
print_date_time();
}
void print_date_time() { //easy way to print date and time
char buf[40];
sprintf(buf, "%02d/%02d/%4d %02d:%02d:%02d", day(), month(), year(), hour(), minute(), second());
Serial.println(buf);
}
void loop() {}
Railroader:
Why is that conversion so unpleasant to do?
Personally I use GPS time in one application and coded the change between summer time and winter time and it works fine.
Remember, I'm a newbie.
Mainly want it because I figure library code is compiled thinner than sketch code and probably runs faster too. Maybe I am wrong about thinner or faster, but it also gets it out of the way.
I just want that type of code hidden in the library where all i do is use it, let the code in the sketch pertain to the functions I want the unit to do rather than adding in a bunch of low level utility code.
I will have several state machines involved to emulate multitasking and i figure having that GMT code and stuff like that in the low level library just keeps the code cleaner and less for me to have to mess with and troubleshoot.