Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions src/TimezonesGMT.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TIMEZONES_GMT_H
#define TIMEZONES_GMT_H

// This file is used to set up GMT based timezones.

// You don't need to change anything here to be able to set up GMT based time.
// If you set TIMEZONES_NON_GMT_OVERRIDE to 1 (as for get summer time and leaps),
Expand All @@ -10,56 +11,62 @@
// Visit the link below.

#ifndef TIMEZONES_NON_GMT_OVERRIDE
#define TIMEZONES_NON_GMT_OVERRIDE 0
#define TIMEZONES_NON_GMT_OVERRIDE 0 // 0: GMT, 1: Location timezone.
#endif

#ifndef TIMEZONES_SELECTED
#define TIMEZONES_SELECTED 0 // If above is 0, value is index of array below.
#endif // Etc/GMT0

#define TIMEZONES_LENGTH 28
#define TIMEZONES_SELECTED 0

typedef struct TZ {
const char* location;
const char* timezone;
} TZ;


#ifdef TIMEZONES_NON_GMT_OVERRIDE
// https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv
static TZ tz_override = {
"Europe/Madrid",
"CET-1CEST,M3.5.0,M10.5.0/3"
};
#endif


static TZ timeZones[] = {
{"Etc/GMT+0","GMT0"}, // 0
{"Etc/GMT+1","<-01>1"}, // 1
{"Etc/GMT+2","<-02>2"}, // 2
{"Etc/GMT+3","<-03>3"}, // 3
{"Etc/GMT+4","<-04>4"}, // 4
{"Etc/GMT+5","<-05>5"}, // 5
{"Etc/GMT+6","<-06>6"}, // 6
{"Etc/GMT+7","<-07>7"}, // 7
{"Etc/GMT+8","<-08>8"}, // 8
{"Etc/GMT+9","<-09>9"}, // 9
{"Etc/GMT+10","<-10>10"}, // 10
{"Etc/GMT+11","<-11>11"}, // 11
{"Etc/GMT+12","<-12>12"}, // 12
{"Etc/GMT-0","GMT0"}, // 13
{"Etc/GMT-1","<+01>-1"}, // 14
{"Etc/GMT-2","<+02>-2"}, // 15
{"Etc/GMT-3","<+03>-3"}, // 16
{"Etc/GMT-4","<+04>-4"}, // 17
{"Etc/GMT-5","<+05>-5"}, // 18
{"Etc/GMT-6","<+06>-6"}, // 19
{"Etc/GMT-7","<+07>-7"}, // 20
{"Etc/GMT-8","<+08>-8"}, // 21
{"Etc/GMT-9","<+09>-9"}, // 22
{"Etc/GMT-10","<+10>-10"}, // 23
{"Etc/GMT-11","<+11>-11"}, // 24
{"Etc/GMT-12","<+12>-12"}, // 25
{"Etc/GMT-13","<+13>-13"}, // 26
{"Etc/GMT-14","<+14>-14"}, // 27
{"Etc/GMT+0","GMT0"}, // 0
{"Etc/GMT+1","<-01>1"}, // 1
{"Etc/GMT+2","<-02>2"}, // 2
{"Etc/GMT+3","<-03>3"}, // 3
{"Etc/GMT+4","<-04>4"}, // 4
{"Etc/GMT+5","<-05>5"}, // 5
{"Etc/GMT+6","<-06>6"}, // 6
{"Etc/GMT+7","<-07>7"}, // 7
{"Etc/GMT+8","<-08>8"}, // 8
{"Etc/GMT+9","<-09>9"}, // 9
{"Etc/GMT+10","<-10>10"}, // 10
{"Etc/GMT+11","<-11>11"}, // 11
{"Etc/GMT+12","<-12>12"}, // 12
{"Etc/GMT-0","GMT0"}, // 13
{"Etc/GMT-1","<+01>-1"}, // 14
{"Etc/GMT-2","<+02>-2"}, // 15
{"Etc/GMT-3","<+03>-3"}, // 16
{"Etc/GMT-4","<+04>-4"}, // 17
{"Etc/GMT-5","<+05>-5"}, // 18
{"Etc/GMT-6","<+06>-6"}, // 19
{"Etc/GMT-7","<+07>-7"}, // 20
{"Etc/GMT-8","<+08>-8"}, // 21
{"Etc/GMT-9","<+09>-9"}, // 22
{"Etc/GMT-10","<+10>-10"}, // 23
{"Etc/GMT-11","<+11>-11"}, // 24
{"Etc/GMT-12","<+12>-12"}, // 25
{"Etc/GMT-13","<+13>-13"}, // 26
{"Etc/GMT-14","<+14>-14"}, // 27
};

static long OFFSETS_SEC[] = {
0, 3600, 7200, 10800, 14400, 18000, 21600, 25200, 28800, 32400, 36000, 39600, 43200, 0, -3600, -7200, -10800, -14400, -18000, -21600, -25200, -28800, -32400, -36000, -39600, -43200, -46800, -50400
};

static int8_t tzIndex = TIMEZONES_SELECTED;
#endif //TIMEZONES_GMT_H
35 changes: 16 additions & 19 deletions src/Watchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GxEPD2_BW<WatchyDisplay, WatchyDisplay::HEIGHT> Watchy::display(

RTC_DATA_ATTR int guiState;
RTC_DATA_ATTR int menuIndex;
RTC_DATA_ATTR BMA423 sensor;
RTC_DATA_ATTR BMA423 sensor;
RTC_DATA_ATTR bool WIFI_CONFIGURED;
RTC_DATA_ATTR bool BLE_CONFIGURED;
RTC_DATA_ATTR weatherData currentWeather;
Expand All @@ -36,6 +36,7 @@ void Watchy::init(String datetime) {
#else
Wire.begin(SDA, SCL); // init i2c
#endif

RTC.init();
// Init the display since is almost sure we will use it
display.epd2.initWatchy();
Expand Down Expand Up @@ -87,8 +88,8 @@ void Watchy::init(String datetime) {
#ifdef ARDUINO_ESP32S3_DEV
pinMode(USB_DET_PIN, INPUT);
USB_PLUGGED_IN = (digitalRead(USB_DET_PIN) == 1);
#endif
gmtOffset = settings.gmtOffset;
#endif
gmtOffset = settings.gmtOffset;
RTC.read(currentTime);
RTC.read(bootTime);
showWatchFace(false); // full update on reset
Expand Down Expand Up @@ -445,9 +446,10 @@ void Watchy::setTime() {
int8_t month = currentTime.Month;
int8_t year = currentTime.Year; //tmYearToY2k(currentTime.Year);
#endif
int8_t gmt = gmtOffset / 3600;
// gmt variable defaults to TIMEZONES_SELECTED if defined
int8_t gmt = OFFSETS_SEC[tzIndex] / 3600;


int8_t tzIndex = TIMEZONES_SELECTED;

int8_t setIndex = SET_HOUR;

Expand Down Expand Up @@ -529,13 +531,8 @@ void Watchy::setTime() {
}


if(tzIndex < 13){
gmt = (tzIndex);
}else if(tzIndex == 13){
gmt = 0;
}else{
gmt = - (tzIndex - 13);
}
gmt = OFFSETS_SEC[tzIndex] / 3600;
gmtOffset = gmt * 3600;

display.fillScreen(GxEPD_BLACK);
display.setTextColor(GxEPD_WHITE);
Expand Down Expand Up @@ -618,15 +615,15 @@ void Watchy::setTime() {
tm.Hour = hour;
tm.Minute = minute;
tm.Second = 0;

gmtOffset = gmt * 3600;

if(TIMEZONES_NON_GMT_OVERRIDE == 0){
setenv("TZ", timeZones[tzIndex].timezone, 1);
} else if (TIMEZONES_NON_GMT_OVERRIDE == 1) {
} else{
#if TIMEZONES_NON_GMT_OVERRIDE == 1
setenv("TZ", tz_override.timezone, 1);
} else {
setenv("TZ", timeZones[TIMEZONES_SELECTED].timezone, 1);
#else
setenv("TZ", timeZones[TIMEZONES_SELECTED].timezone, 1);
#endif
}

tzset();
Expand Down Expand Up @@ -775,7 +772,7 @@ weatherData Watchy::_getWeatherData(String cityID, String lat, String lon, Strin
breakTime((time_t)(int)responseObject["sys"]["sunset"], currentWeather.sunset);
// sync NTP during weather API call and use timezone of lat & lon
gmtOffset = int(responseObject["timezone"]);
syncNTP();
syncNTP(gmtOffset);
} else {
// http error
}
Expand Down Expand Up @@ -1144,7 +1141,7 @@ void Watchy::showSyncNTP() {
display.println(gmtOffset);
display.display(true); // full refresh
if (connectWiFi()) {
if (syncNTP()) {
if (syncNTP(gmtOffset)) {
display.println("NTP Sync Success\n");
display.println("Current Time Is:");

Expand Down
2 changes: 1 addition & 1 deletion src/Watchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct watchySettings {
int8_t weatherUpdateInterval;
// NTP Settings
String ntpServer;
int gmtOffset;
long gmtOffset;
//
bool vibrateOClock;
} watchySettings;
Expand Down
1 change: 0 additions & 1 deletion src/Watchy32KRTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ void Watchy32KRTC::set(tmElements_t tm) {
timeInfo.tm_hour = tm.Hour;
timeInfo.tm_min = tm.Minute;
timeInfo.tm_sec = tm.Second;

// Convert tm to timeval
struct timeval tv;
tv.tv_sec = mktime(&timeInfo);
Expand Down