Set GMT timezone in run time and location based timezone in compile time.

pull/264/head
Elías A. Angulo Klein 2024-07-08 09:11:41 +02:00
parent e32eb01bb2
commit a9a71698a6
5 changed files with 134 additions and 16 deletions

65
src/TimezonesGMT.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef TIMEZONES_GMT_H
#define TIMEZONES_GMT_H
// 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),
// you must provide one location based timezone.
// 0: GMT, 1: Location timezone.
// Visit the link below.
#ifndef TIMEZONES_NON_GMT_OVERRIDE
#define TIMEZONES_NON_GMT_OVERRIDE 0
#endif
#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
};
#endif //TIMEZONES_GMT_H

View File

@ -17,7 +17,11 @@ RTC_DATA_ATTR bool WIFI_CONFIGURED;
RTC_DATA_ATTR bool BLE_CONFIGURED;
RTC_DATA_ATTR weatherData currentWeather;
RTC_DATA_ATTR int weatherIntervalCounter = -1;
#ifdef GMT_OFFSET_SEC
RTC_DATA_ATTR long gmtOffset = GMT_OFFSET_SEC;
#else
RTC_DATA_ATTR long gmtOffset = 0;
#endif
RTC_DATA_ATTR bool alreadyInMenu = true;
RTC_DATA_ATTR bool USB_PLUGGED_IN = false;
RTC_DATA_ATTR tmElements_t bootTime;
@ -439,8 +443,11 @@ void Watchy::setTime() {
int8_t hour = currentTime.Hour;
int8_t day = currentTime.Day;
int8_t month = currentTime.Month;
int8_t year = tmYearToY2k(currentTime.Year);
int8_t year = currentTime.Year; //tmYearToY2k(currentTime.Year);
#endif
int8_t gmt = gmtOffset / 3600;
int8_t tzIndex = TIMEZONES_SELECTED;
int8_t setIndex = SET_HOUR;
@ -487,6 +494,9 @@ void Watchy::setTime() {
case SET_DAY:
day == 31 ? (day = 1) : day++;
break;
case SET_TZ:
tzIndex == TIMEZONES_LENGTH - 1 ? (tzIndex = 0) : tzIndex++;
break;
default:
break;
}
@ -510,11 +520,23 @@ void Watchy::setTime() {
case SET_DAY:
day == 1 ? (day = 31) : day--;
break;
case SET_TZ:
tzIndex == 0 ? (tzIndex = TIMEZONES_LENGTH - 1) : tzIndex--;
break;
default:
break;
}
}
if(tzIndex < 13){
gmt = (tzIndex);
}else if(tzIndex == 13){
gmt = 0;
}else{
gmt = - (tzIndex - 13);
}
display.fillScreen(GxEPD_BLACK);
display.setTextColor(GxEPD_WHITE);
display.setFont(&DSEG7_Classic_Bold_53);
@ -540,14 +562,25 @@ void Watchy::setTime() {
}
display.print(minute);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_WHITE);
display.setCursor(82, 140);
if (setIndex == SET_TZ) { // blink minute digits
display.setTextColor(blink ? GxEPD_WHITE : GxEPD_BLACK);
}
display.print("GMT");
display.print(gmt);
display.setTextColor(GxEPD_WHITE);
display.setFont(&FreeMonoBold9pt7b);
display.setCursor(45, 150);
display.setCursor(60, 165);
if (setIndex == SET_YEAR) { // blink minute digits
display.setTextColor(blink ? GxEPD_WHITE : GxEPD_BLACK);
}
display.print(2000 + year);
display.print(year);
display.setTextColor(GxEPD_WHITE);
display.print("/");
@ -570,6 +603,7 @@ void Watchy::setTime() {
display.print("0");
}
display.print(day);
display.display(true); // partial refresh
}
@ -579,14 +613,26 @@ void Watchy::setTime() {
#ifdef ARDUINO_ESP32S3_DEV
tm.Year = year;
#else
tm.Year = y2kYearToTm(year);
tm.Year = year; //y2kYearToTm(year);
#endif
tm.Hour = hour;
tm.Minute = minute;
tm.Second = 0;
RTC.set(tm);
gmtOffset = gmt * 3600;
if(TIMEZONES_NON_GMT_OVERRIDE == 0){
setenv("TZ", timeZones[tzIndex].timezone, 1);
} else if (TIMEZONES_NON_GMT_OVERRIDE == 1) {
setenv("TZ", tz_override.timezone, 1);
} else {
setenv("TZ", timeZones[TIMEZONES_SELECTED].timezone, 1);
}
tzset();
RTC.set(tm);
showMenu(menuIndex, true);
}
@ -729,7 +775,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(gmtOffset);
syncNTP();
} else {
// http error
}
@ -929,6 +975,7 @@ void Watchy::setupWifi() {
weatherIntervalCounter = -1; // Reset to force weather to be read again
lastIPAddress = WiFi.localIP();
WiFi.SSID().toCharArray(lastSSID, 30);
getWeatherData(); // force weather update
}
display.display(true); // full refresh
// turn off radios
@ -1145,6 +1192,7 @@ bool Watchy::syncNTP(long gmt, String ntpServer) {
// WiFi and remember to turn it back off
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer.c_str(), gmt);
timeClient.setTimeOffset(gmt);
timeClient.begin();
if (!timeClient.forceUpdate()) {
return false; // NTP sync failed

View File

@ -16,6 +16,7 @@
#include "bma.h"
#include "config.h"
#include "esp_chip_info.h"
#include "TimezonesGMT.h"
#ifdef ARDUINO_ESP32S3_DEV
#include "Watchy32KRTC.h"
#include "soc/rtc.h"

View File

@ -1,17 +1,19 @@
#include "Watchy32KRTC.h"
#include "TimezonesGMT.h"
Watchy32KRTC::Watchy32KRTC(){}
void Watchy32KRTC::init() {
if(TIMEZONES_NON_GMT_OVERRIDE == 0){
setenv("TZ", timeZones[TIMEZONES_SELECTED].timezone, 1);
} else if (TIMEZONES_NON_GMT_OVERRIDE == 1) {
setenv("TZ", tz_override.timezone, 1);
} else {
setenv("TZ", "GMT0", 1);
}
}
/*
setenv("TZ", "", 1);
tzset();
*/
}
void Watchy32KRTC::config(String datetime) { // String datetime format is YYYY:MM:DD:HH:MM:SS
struct tm timeInfo;

View File

@ -104,9 +104,11 @@
// set time
#define SET_HOUR 0
#define SET_MINUTE 1
#define SET_YEAR 2
#define SET_MONTH 3
#define SET_DAY 4
#define SET_TZ 2
#define SET_YEAR 3
#define SET_MONTH 4
#define SET_DAY 5
#define HOUR_12_24 24
// BLE OTA
#define BLE_DEVICE_NAME "Watchy BLE OTA"