Timezone Hardware
#include <Timezone.h
>
/*F********************************************************************
* Arduino Timezone Library Copyright (C) 2018 by Jack Christensen and
licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
Arduino Timezone Library example sketch.
Self-adjusting clock for one time zone using an external real-time
clock, either a DS1307 or DS3231 (e.g. Chronodot).
Assumes the RTC is set to UTC.
TimeChangeRules can be hard-coded or read from EEPROM, see comments.
Check out the Chronodot at http://www.macetech.com/store/
Jack Christensen Aug 2012
**********************************************************************/
#include <DS1307RTC.h> // https://github.com/PaulStoffregen/DS1307RTC
#include <Timezone.h> // https://github.com/JChristensen/Timezone
//************************* DEFINES ************************************
#define BAUD 9600
//************************* PROTOTYPES ************************************
void printDateTime( time_t t, const char *tz);
//************************* VARIABLES ************************************
// US CENTRAL Time Zone ( Chicago, Dallas )
TimeChangeRule Dst = { "DST", Second, Sun, Mar, 2, -300};// DST = UTC - 5 hrs
TimeChangeRule Cst = { "CST", First, Sun, Nov, 2, -360}; // CST = UTC - 6 hrs
Timezone myTZ( Cdt, Dst );
// If TimeChangeRules are already stored in EEPROM, comment out three
// lines above and uncomment line below.
//Timezone myTZ( 100 ); // ASSUMES RULES STORED AT EEPROM ADDRESS 100
TimeChangeRule *tcr; // POINTER TO TIME CHANGE RULE, USE TO GET TZ ABBREV
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD );
setSyncProvider( RTC.get); // GET TIME FROM RTC
if(timeStatus()!= timeSet)
Serial.println( "Unable to sync with the RTC");
else
Serial.println( "RTC has set the system time");
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
time_t utc = now();
time_t local = myTZ.toLocal( utc, &tcr );
Serial.println();
printDateTime( utc, "UTC");
printDateTime( local, tcr->abbrev);
delay( 10000 );
}
/*F********************************************************************
* FORMAT AND PRINT A TIME_T VALUE, WITH A TIME ZONE APPENDED
**********************************************************************/
void
printDateTime( time_t t, const char *tz)
{
char buf[32];
char m[4]; // MONTH STRING (DateStrings.cpp USES SHARED BUFFER)
strcpy( m, monthShortStr(month(t)));
sprintf( buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s", hour(t), minute(t)
, second(t), dayShortStr( weekday(t)), day(t), m, year(t), tz);
Serial.println( buf );
}