ropg / ezTime

ezTime — pronounced "Easy Time" — is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, user events, millisecond precision and more.
MIT License
327 stars 92 forks source link

Using country code "za" does not set timezone.setLocation as expected #44

Closed emeyedeejay closed 4 years ago

emeyedeejay commented 5 years ago

I'm attempting to build an "always right" NTP based clock with a dot matrix display and have come across the awesome ezTime library which looks perfect for my needs (and way beyond!)

I have run into an issue whereby using a country code does not seem to set my location and hence the relevant timezone info.

I have a test sketch which uses "de" as per the ethernet example sketch which functions fine but when I change only the "de" to "za" I don't get a result returned. Exactly the same happens if I try to use the GeoIP lookup.

I'm in South Africa which has a single time zone of UTC+2.

The below illustrates first the working scenario with a "de" sketch and associated debug output and the second a non-working scenario and associated debug output.

I'm no developer and tend to hack my way through examples of various libraries to put sketches together so it's completely probable that I'm missing something along the way but thought I'd report this anyway,

Working Scenario - "de"

#include <ezTime.h>
#include <ESP8266WiFi.h>          // Replace with WiFi.h for ESP32
#include <ESP8266WebServer.h>     // Replace with WebServer.h for ESP32
#include <AutoConnect.h>

ESP8266WebServer Server;          // Replace with WebServer for ESP32
AutoConnect Portal(Server);

void rootPage() {
  char content[] = "Hello, world ... browse to http://<ip addres>/_ac to configure AP, SSID, password etc. settings";
  Server.send(200, "text/plain", content);
}

void setup() {

  // Set ezTime debug level as required [https://github.com/ropg/ezTime#setdebug]
  setDebug(DEBUG);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);

  // wait for serial port to connect. Needed for native USB port only
  while (!Serial) {
    ;
  }

  //Beging Debug Output
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println("----Debug Begins--------------------------------------------");
  Serial.println();

  // Enable Autoconnect portal and if successful, output Ip to debug
  Server.on("/", rootPage);
  if (Portal.begin()) {
    Serial.println("WiFi connected: " + WiFi.localIP().toString());
  }

  // We're now online

  // Wait for ezTime to get its time synchronized and wait 5 seconds
  waitForSync();
  delay(5000);  

  // Create a timezonr called myTZ
  Timezone myTZ;

  // Output UTC
  Serial.println();
  Serial.println("<ezTime> UTC: " + UTC.dateTime());
  Serial.println();

  //Set myTZ's location and outpit the result
  myTZ.setLocation(F("de"));
  Serial.print(F("<ezTime> myTZ: "));
  Serial.println(myTZ.dateTime());
  myTZ.getTimezoneName();
  myTZ.getOlson();

  //Set NTP update frequency in seconds
  setInterval(60); //[https://github.com/ropg/ezTime#setserver-and-setinterval]
}

void loop() {
  //Handle the Autoconnect client
  Portal.handleClient();

  //Handle the ezTmine events //[https://github.com/ropg/ezTime#events]
  events();
}

----Debug Begins--------------------------------------------

[AC] WiFi.config(IP=0.0.0.0, Gateway=0.0.0.0, Subnetmask=0.0.0.0, DNS1=0.0.0.0, DNS2=0.0.0.0)
[AC] DHCP client(STARTED)
[AC] WiFi.begin()
[AC] Connecting.established IP:192.168.0.123
[AC] http server started
WiFi connected: 192.168.0.123
Waiting for time sync
Querying pool.ntp.org ... Received data:
0: 24, 2, 9, EC, 
4: 0, 0, 28, 9B, 
8: 0, 0, 0, 2F, 
12: 55, C7, D6, 62, 
16: E0, 53, 45, A9, 
20: 75, 9F, 5D, 1A, 
24: 0, 0, 0, 0, 
28: 0, 0, 0, 0, 
32: E0, 53, 48, 67, 
36: 8E, E8, 28, 34, 
40: E0, 53, 48, 67, 
44: 8E, EA, DD, 90, 
success (round trip 54 ms)
Received time: Saturday, 06-Apr-19 15:46:47.587 UTC
Set event (#1) to trigger on: Saturday, 06-Apr-2019 16:16:48 UTC
Time is in sync

<ezTime> UTC: Saturday, 06-Apr-2019 15:46:52 UTC

Timezone lookup for: de ... (round-trip 173 ms)  success.
  Olson: Europe/Berlin
  Posix: CET-1CEST,M3.5.0,M10.5.0/3
<ezTime> myTZ: Saturday, 06-Apr-2019 17:46:52 CEST
Deleted event (#1), set for Saturday, 06-Apr-2019 16:16:48 UTC
Set event (#1) to trigger on: Saturday, 06-Apr-2019 15:47:52 UTC

Failing scenario - "za"

#include <ezTime.h>
#include <ESP8266WiFi.h>          // Replace with WiFi.h for ESP32
#include <ESP8266WebServer.h>     // Replace with WebServer.h for ESP32
#include <AutoConnect.h>

ESP8266WebServer Server;          // Replace with WebServer for ESP32
AutoConnect Portal(Server);

void rootPage() {
  char content[] = "Hello, world ... browse to http://<ip addres>/_ac to configure AP, SSID, password etc. settings";
  Server.send(200, "text/plain", content);
}

void setup() {

  // Set ezTime debug level as required [https://github.com/ropg/ezTime#setdebug]
  setDebug(DEBUG);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);

  // wait for serial port to connect. Needed for native USB port only
  while (!Serial) {
    ;
  }

  //Beging Debug Output
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println("----Debug Begins--------------------------------------------");
  Serial.println();

  // Enable Autoconnect portal and if successful, output Ip to debug
  Server.on("/", rootPage);
  if (Portal.begin()) {
    Serial.println("WiFi connected: " + WiFi.localIP().toString());
  }

  // We're now online

  // Wait for ezTime to get its time synchronized and wait 5 seconds
  waitForSync();
  delay(5000);  

  // Create a timezonr called myTZ
  Timezone myTZ;

  // Output UTC
  Serial.println();
  Serial.println("<ezTime> UTC: " + UTC.dateTime());
  Serial.println();

  //Set myTZ's location and outpit the result
  myTZ.setLocation(F("za"));
  Serial.print(F("<ezTime> myTZ: "));
  Serial.println(myTZ.dateTime());
  myTZ.getTimezoneName();
  myTZ.getOlson();

  //Set NTP update frequency in seconds
  setInterval(60); //[https://github.com/ropg/ezTime#setserver-and-setinterval]
}

void loop() {
  //Handle the Autoconnect client
  Portal.handleClient();

  //Handle the ezTmine events //[https://github.com/ropg/ezTime#events]
  events();
}

----Debug Begins--------------------------------------------

[AC] WiFi.config(IP=0.0.0.0, Gateway=0.0.0.0, Subnetmask=0.0.0.0, DNS1=0.0.0.0, DNS2=0.0.0.0)
[AC] DHCP client(STARTED)
[AC] WiFi.begin()
[AC] Connecting.established IP:192.168.0.123
[AC] http server started
WiFi connected: 192.168.0.123
Waiting for time sync
Querying pool.ntp.org ... Received data:
0: 24, 2, 9, E9, 
4: 0, 0, 0, 82, 
8: 0, 0, 9, 5D, 
12: C4, 15, BB, 2, 
16: E0, 53, 6A, 85, 
20: B5, 48, 0, BB, 
24: 0, 0, 0, 0, 
28: 0, 0, 0, 0, 
32: E0, 53, 6F, 6E, 
36: C3, 81, FF, 2B, 
40: E0, 53, 6F, 6E, 
44: C3, 82, CC, 2, 
success (round trip 53 ms)
Received time: Saturday, 06-Apr-19 18:33:18.792 UTC
Set event (#1) to trigger on: Saturday, 06-Apr-2019 19:03:19 UTC
Time is in sync

<ezTime> UTC: Saturday, 06-Apr-2019 18:33:23 UTC

Timezone lookup for: za ... (round-trip 504 ms)  <ezTime> myTZ: Saturday, 06-Apr-2019 18:33:24 UTC
Deleted event (#1), set for Saturday, 06-Apr-2019 19:03:19 UTC
Set event (#1) to trigger on: Saturday, 06-Apr-2019 18:34:24 UTC
ropg commented 4 years ago

I fixed an issue where it wouldn't recognise most two-letter country codes. za now correctly resolves to Africa/Johannesburg SAST-2