TioRuben / TTGO-T-Wristband

First steps with TTGO T-Wristband
MIT License
65 stars 23 forks source link

Timezone and isDST function problem #4

Closed johnheenan closed 4 years ago

johnheenan commented 4 years ago

The customise the code for different time zones hardcoding is required in two files: wifi/ntp.cpp and hardware/clock.cpp.

In function isDST (for is Daylight Savings Time) the return line: return previousSunday >= 20; looks OK for month 3 but not for month 10.

Suggested change:

if (now.month == 3)
    return previousSunday >= 20;
else // now.month can now only be 10 
    return previousSunday < 20;

John Heenan

Entire isDST() function:

bool isDST(RTC_Date now)
{
  uint8_t dayOfWeek = rtc.getDayOfWeek(now.day, now.month, now.year);
  if (now.month < 3 || now.month > 10)
  {
    return false;
  }
  if (now.month > 3 && now.month < 10)
  {
    return true;
  }
  int previousSunday = now.day - dayOfWeek;
  return previousSunday >= 20;
}
johnheenan commented 4 years ago

I am preparing another PR with timezone code to replace the one I withdrew above.

It uses variables. Three examples are provided that can be easily adjusted, one without using daylight savings and two with: one for the northern hemisphere and one for the southern hemisphere.

It should not be difficult to add in code to allow users to vary a UTC offset with the button. For example a long press to allow to set offset and a short press following to vary hour.

Allowing full timezone with daylight savings would mean coding in timezones for major cities and asking a user to choose a city. Not easy on a wristband with one button.

johnheenan commented 4 years ago

I have replacement timezones code in the timezones branch at https://github.com/johnheenan/TTGO-T-Wristband.git

I have tested it with a northern hemisphere dual timezone (CEST/CET), a southern hemisphere dual timezone (AEST/AEDT) and with just one timezone (no daylight saving: just same UTC offset all year). The code is general enough to allow any other dual daylight savings timezones to be used and it should be easy to change a dual timezone set without a firmware rebuild and without having to dig into internally.

The code makes an assumption that the day of week and the week of month will be the same on both timezone change dates but if not the code can be easily adapted to cater for such an unlikely difference.

There are some minor issues that need review and test (for example for the hour at timezone changeover).

While the RTC stores local time and UTC time is calculated, as before, I don't think there is any particular advantage in storing UTC and then calculating local time. There are still tricky tests.

The code will get accepted as a PR, if wanted.

To test the code

git clone https://github.com/johnheenan/TTGO-T-Wristband.git
cd TTGO-T-Wristband
git checkout -b origin/timezones 
johnheenan commented 4 years ago

I have tagged a branch at https://github.com/johnheenan/TTGO-T-Wristband/tree/v0.1-alpha which adds menu for UTC Offsets and STD Regions as well a sother improvements. There is no point in keeping this issue open any longer.