Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: import IANA timezone definitions, expose SNTP API #6373

Merged
merged 38 commits into from
Sep 29, 2019

Conversation

d-a-v
Copy link
Collaborator

@d-a-v d-a-v commented Aug 1, 2019

  • configTime("timezone", "ntp servers...") added
  • timezone definitions by country/cities (TZ.h)
  • script to update timezone definitions
  • updated example
  • sntp additions (see below)

Closes #4637
Closes #5681
Fixes #6562

d-a-v added 5 commits August 1, 2019 17:58
- `configTime("timezone", "ntp servers...")` added
- timezone definitions by country/cities (TZ.h)
- script to update timezone definitions
- updated example
@d-a-v
Copy link
Collaborator Author

d-a-v commented Sep 2, 2019

After this morning's example update, using scheduled functions, here's the new output with my timezone:

22:22:07.395 -> 
22:22:10.549 -> localtime: isdst=1 yday=244 wday=1 year=119 mon=8 mday=2 hour=22 min=22 sec=10
22:22:10.582 -> gmtime:    isdst=0 yday=244 wday=1 year=119 mon=8 mday=2 hour=20 min=22 sec=10
22:22:10.582 -> clock:     649s / 663065000ns
22:22:10.582 -> millis:    649663
22:22:10.582 -> micros:    649663092
22:22:10.582 -> gtod:      1567455730s / 398903us
22:22:10.582 -> time:      1567455730
22:22:10.582 -> timezone:  CET-1CEST,M3.5.0,M10.5.0/3
22:22:10.582 -> in future: no
22:22:10.582 -> ctime:     Mon Sep  2 22:22:10 2019
22:22:10.582 -> 
22:22:13.772 -> 
22:22:13.772 -> -- time machine: artificially moving 6 months forward (future)
22:22:13.772 -> -- observe local time change according to selected TZ's DST rules
22:22:13.772 -> -- (and without assistance from sketch)
22:22:13.772 -> 
22:22:13.772 -> 
22:22:13.772 -> ----------> scheduled CB: settimeofday() has been called 4ms ago <----------
22:22:13.772 -> sleeping 2s
22:22:13.772 -> 
22:22:15.765 -> 
22:22:15.765 -> ----------> loop: settimeofday() has been called 2012ms ago ! <----------
22:22:15.765 -> 
22:22:16.963 -> localtime: isdst=0 yday=62 wday=1 year=119 mon=2 mday=4 hour=21 min=22 sec=16
22:22:16.963 -> gmtime:    isdst=0 yday=62 wday=1 year=119 mon=2 mday=4 hour=20 min=22 sec=16
22:22:16.963 -> clock:     656s / 63085000ns
22:22:16.963 -> millis:    656063
22:22:16.963 -> micros:    656063116
22:22:16.996 -> gtod:      1551730936s / 798835us
22:22:16.996 -> time:      1551730936
22:22:16.996 -> timezone:  CET-1CEST,M3.5.0,M10.5.0/3
22:22:16.996 -> in future: 6 months
22:22:16.996 -> ctime:     Mon Mar  4 21:22:16 2019
22:22:16.996 -> 

(watch ctime, gmtime, localtime and isdst)

@d-a-v
Copy link
Collaborator Author

d-a-v commented Sep 6, 2019

@bperrybap @Tech-TX @Meinsda @whyameye @mcspr ref:#4637,#5681

SNTP API additions (in example):

  // optional: change SNTP update delay (default: 1 hour = SNTP_UPDATE_DELAY_DEFAULT)
  sntp_update_delay_not_less_than_15000 = 600000; // 10mn / 600s / 600'000ms
  
  // optional: disable obtaining SNTP servers from DHCP
  sntp_servermode_dhcp(0); // 0: disable (enabled by default)

@d-a-v d-a-v changed the title time: import IANA timezone definitions time: import IANA timezone definitions, expose SNTP API Sep 6, 2019
Copy link
Collaborator

@mcspr mcspr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #5681, this works for me in both the proposed example and in an existing sketch.

As a sidenote, since we are changing sntp a bit.
Should startup delay also be configurable? (ref the old #5564)
And is there a reason not to do something like this, and user then defines those in a sketch when needed? https://gist.github.com/mcspr/a68cd059304c14f487a4653f7b03e091
(besides exposing another function that runs in a system context)

libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino Outdated Show resolved Hide resolved
@d-a-v
Copy link
Collaborator Author

d-a-v commented Sep 6, 2019

@mcspr Following lwIP's logic, I added a weak function (and only after I followed your link to see that it was what you did for both parameters (even the former which is proposed as a constant in lwIP)).

@mcspr
Copy link
Collaborator

mcspr commented Sep 7, 2019

Yes, sorry, I would've changed linklayer repo directly to show a proper patch. I somehow missed that arduino lwipopts.h is also there (edit: re-reading the sentence, now I understand that we thought about the same thing :))

tbh, I liked the idea to have them both weak functions for consistency sake. even if the other macro called _func, they both are used without calling()

update example
fix for lwip1.4
@d-a-v
Copy link
Collaborator Author

d-a-v commented Sep 8, 2019

@mcspr, both are now weak functions

Copy link
Collaborator

@mcspr mcspr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. long names seem to solve the documentation problem

libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino Outdated Show resolved Hide resolved
tools/TZupdate.sh Outdated Show resolved Hide resolved
tools/TZupdate.sh Outdated Show resolved Hide resolved
@d-a-v d-a-v requested a review from devyte September 22, 2019 13:34
Copy link
Collaborator

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor technical debt and default demo issues, but OTW LGTM.

cores/esp8266/Arduino.h Show resolved Hide resolved
cores/esp8266/time.cpp Outdated Show resolved Hide resolved
libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino Outdated Show resolved Hide resolved
cores/esp8266/time.cpp Outdated Show resolved Hide resolved
@d-a-v
Copy link
Collaborator Author

d-a-v commented Sep 29, 2019

New output for the example, I did my best to show that everything is handled by newlib and lwIP/sntp,
so everyone can always have the most accurate datetime without even taking care of it,
after having selected the right timezone (like one does on Linux, Windows, RPi, iPhone etc).

Starting...

Time is currently set by a constant:

localtime: isdst=0 yday=316 wday=1 year=117 mon=10 mday=13 hour=17 min=7 sec=5
gmtime:    isdst=0 yday=316 wday=1 year=117 mon=10 mday=13 hour=17 min=7 sec=5
clock:     0s / 62643000ns
millis:    62
micros:    62669
gtod:      1510592825s / 2240us
time:      1510592825
timezone:  GMT0BST,M3.5.0/1,M10.5.0
ctime:     Mon Nov 13 17:07:05 2017

---- settimeofday() has been called - possibly from SNTP
     (starting time machine demo to show libc's automatic DST handling)

future=  0days: DST=true  - Sun Sep 29 01:26:26 2019
future= 30days: DST=false - Tue Oct 29 00:26:26 2019
future= 60days: DST=false - Thu Nov 28 00:26:26 2019
future= 90days: DST=false - Sat Dec 28 00:26:26 2019
future=120days: DST=false - Mon Jan 27 00:26:26 2020
future=150days: DST=false - Wed Feb 26 00:26:26 2020
future=180days: DST=false - Fri Mar 27 00:26:26 2020
future=210days: DST=true  - Sun Apr 26 01:26:26 2020
future=240days: DST=true  - Tue May 26 01:26:26 2020
future=270days: DST=true  - Thu Jun 25 01:26:26 2020
future=300days: DST=true  - Sat Jul 25 01:26:27 2020
future=330days: DST=true  - Mon Aug 24 01:26:27 2020
future=360days: DST=true  - Wed Sep 23 01:26:27 2020

localtime: isdst=1 yday=271 wday=0 year=119 mon=8 mday=29 hour=1 min=26 sec=27
gmtime:    isdst=0 yday=271 wday=0 year=119 mon=8 mday=29 hour=0 min=26 sec=27
clock:     4s / 44458000ns
millis:    4044
micros:    4044478
gtod:      1569716787s / 14962us
time:      1569716787
timezone:  GMT0BST,M3.5.0/1,M10.5.0
ctime:     Sun Sep 29 01:26:27 2019

Copy link
Collaborator

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, and thanks for the LWIP build fix slipstream, too.

@devyte
Copy link
Collaborator

devyte commented Sep 29, 2019

Good work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants