Time Zone CLock
#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.
TimeChangeRules can be hard-coded or read from EEPROM, see comments.
Jack Christensen Mar 2012
**********************************************************************/
#include <Timezone.h> // https://github.com/JChristensen/Timezone
//************************* DEFINES ************************************
#define BAUD 9600
//************************* PROTOTYPES ************************************
time_t compileTime();
void printDateTime( time_t t, const char *tz );
//************************* VARIABLES ************************************
// US CENTRAL TIME ZONE (Chicago, Dallas)
TimeChangeRule Dst = {"CDT", Second, Sun, Mar, 2, -300};// Dylht time = UTC - 5 hrs
TimeChangeRule Std = {"CST", First, Sun, Nov, 2, -360};// Std time: UTC - 6 hrs
Timezone Tz( Dst, Std );
// IF TimeChangeRules IN EEPROM, COMMENT OUT THREE
// LINES ABOVE AND UNCOMMENT LINE BELOW
//Timezone Tz( 100 ); // EEPROM ADDRESS 100
TimeChangeRule *Tcr; // POINTER TO TIME CHANGE RULE, USE TO GET TZ ABBREV
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD );
setTime( Tz.toUTC( compileTime() ) );
//setTime( 01, 55, 00, 11, 3, 2012);
// diff way set (hr,min,sec,day,mnth,yr)
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
time_t utc = now();
time_t local = Tz.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]; // TEMP STG FOR MON STRING (DateStrings.cpp 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 );
}
/*F********************************************************************
* return compile date and time as a time_t value
**********************************************************************/
time_t
compileTime()
{
// FUDGE FACTOR TO ALLOW FOR COMPILE TIME (SECONDS, YMMV)
const time_t FUDGE( 10 );
const char *compDate = __DATE__, *compTime = __TIME__, *months
= "JanFebMarAprMayJunJulAugSepOctNovDec";
char chMon[4], *m;
tmElements_t tm;
strncpy( chMon, compDate, 3);
chMon[3] = '\0';
m = strstr( months, chMon);
tm.Month = ( (m - months) / 3 + 1);
tm.Day = atoi( compDate + 4);
tm.Year = atoi( compDate + 7) - 1970;
tm.Hour = atoi( compTime);
tm.Minute = atoi( compTime + 3);
tm.Second = atoi( compTime + 6);
time_t t = makeTime( tm );
return t + FUDGE; // ADD FUDGE FACTOR TO ALLOW FOR COMPILE TIME
}