/*----------------------------------------------------------------------*
* Timezone library example sketch. *
* Self-adjusting clock for multiple time zones. *
* Jack Christensen Mar 2012 *
* *
* Sources for DST rule information: *
* http://www.timeanddate.com/worldclock/ *
* http://home.tiscali.nl/~t876506/TZworld.html *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <RTClib.h>
#include <TimeLib.h> //http://www.arduino.cc/playground/Code/Time
#include <Timezone.h> //https://github.com/JChristensen/Timezone
//Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; //Central European Standard Time
Timezone CE(CEST, CET);
//United Kingdom (London, Belfast)
TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60}; //British Summer Time
TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 0}; //Standard Time
Timezone UK(BST, GMT);
TimeChangeRule *tcr; //pointer to the time change rule, use to get the TZ abbrev
time_t utc;
RTC_DS3231 rtc;
void setup(void)
{
Serial.begin(115200);
//setTime(CE.toUTC(compileTime()));
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void loop(void)
{
Serial.println();
// see https://arduino.stackexchange.com/questions/17639/the-difference-between-time-t-and-datetime
DateTime dt = rtc.now();
utc = dt.unixtime();
//utc = now();
printTime(CE.toLocal(utc, &tcr), tcr -> abbrev, "Berlin");
printTime(utc, "UTC", " Universal Coordinated Time");
time_t t = CE.toLocal(utc, &tcr);
Serial.print(hour(t));
Serial.print(':');
Serial.print(minute(t));
Serial.print(':');
Serial.print(second(t));
// Serial.println(tcr -> abbrev);
delay(10000);
}
//Function to return the compile date and time as a time_t value
time_t compileTime(void)
{
#define FUDGE 25 //fudge factor to allow for compile time (seconds, YMMV)
char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
char chMon[3], *m;
int d, y;
tmElements_t tm;
time_t t;
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);
t = makeTime(tm);
return t + FUDGE; //add fudge factor to allow for compile time
}
//Function to print time with time zone
void printTime(time_t t, char *tz, char *loc)
{
sPrintI00(hour(t));
sPrintDigits(minute(t));
sPrintDigits(second(t));
Serial.print(' ');
Serial.print(dayShortStr(weekday(t)));
Serial.print(' ');
sPrintI00(day(t));
Serial.print(' ');
Serial.print(monthShortStr(month(t)));
Serial.print(' ');
Serial.print(year(t));
Serial.print(' ');
Serial.print(tz);
Serial.print(' ');
Serial.print(loc);
Serial.println();
}
//Print an integer in "00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintI00(int val)
{
if (val < 10) Serial.print('0');
Serial.print(val, DEC);
return;
}
//Print an integer in ":00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintDigits(int val)
{
Serial.print(':');
if(val < 10) Serial.print('0');
Serial.print(val, DEC);
}