esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
16.08k stars 13.33k forks source link

NTP-TZ-DST esp8266 example displays year "124" #9205

Closed atesin closed 1 week ago

atesin commented 1 week ago

Basic Infos

Platform

Problem Description

set_system_time(ntp_timestamp - NTP_OFFSET);

MCVE Sketch


// just the full NTP-TZ-DST example sketch shipped with esp8266-arduino project version 3.1.2

Debug Messages

localtime: isdst=0 yday=321 wday=0 year=124 mon=10 mday=17 hour=0 min=31 sec=11
gmtime:    isdst=0 yday=321 wday=0 year=124 mon=10 mday=17 hour=0 min=31 sec=11
clock:     60s + 74079000ns
millis:    60074
micros:    60074115
gtod:      1731803471s + 344538us
time:      1731803471
timezone:  GMT0BST,M3.5.0/1,M10.5.0
ctime:     Sun Nov 17 00:31:11 2024
sntp0:     pool.ntp.org (64.176.3.116) - IPv6: No - Reachability: 1

time(): 1731803471   gettimeofday(): 1731803471.973050  seconds are unchanged
time(): 1731803472   gettimeofday(): 1731803472.024875  <-- seconds have changed
atesin commented 1 week ago

i answer myself (after rolling over whole internet and waking up a lot of people...):

◆ tm_year
int16_t tm::tm_year

years since 1900

so... the "124" means that PRESENTLY, HAD BEEN PASSED "124" years since 1900 what is OK...

i was printing a tm struct fields directly.. it seem that the tm structs are not intended to be used directly (or at least use with care) .... same with other numbers that start with "0" instead "1" (i.e. tm_mon 0 = january, etc)... so for printing better use ctime() or strftime() instead

... so, the only questions left i think are:

thanks

mcspr commented 1 week ago

it seem that the tm structs are not intended to be used directly (or at least use with care)

See POSIX ctime reference, this is not something specific to either avr or esp8266 libc https://pubs.opengroup.org/onlinepubs/9799919799/

does configTime() writes configuration to flash?, and if does, what is the safer way to sync periodically without tearing flash?

No. Why would it? Where you got that idea from?

could anybody please split NTP-TZ-DST example into various smaller basic minimal ones to explain separate features?? (no intermediate functions, no macros, no spaghetti codes)

You are welcome to update the example(s)

could anybody please write a doxygen-like esp* sntp functions documentation thad nobody exactly knows?

See lwip documentation for the useful part. This is mentioned in the readme btw. SDK methods are not guaranteed to work correctly, please do not refer to NONOS examples or source using NONOS directly https://www.nongnu.org/lwip/2_1_x/sntp_8h.html

atesin commented 1 week ago

See POSIX ctime reference, this is not something specific to either avr or esp8266 libc https://pubs.opengroup.org/onlinepubs/9799919799/

what is that?... i see info on these pages:

No. Why would it? Where you got that idea from?

i think it prefectly could save config to flash with configTime(ts, srv1, srv2, srv3) and then sync time with configTime() just using stored config, like WiFi.begin() does

See lwip documentation for the useful part. This is mentioned in the readme btw. SDK methods are not guaranteed to work correctly, please do not refer to NONOS examples or source using NONOS directly https://www.nongnu.org/lwip/2_1_x/sntp_8h.html

nice ... however it seem lwip sntp functions cannot be used directly in sketches (i tested and compiler complain me about sntp_init() not defined) ... i would like to found some documentation about esp8266-arduino SNTP CORE functions (those used in NTP-TZ-DST without including any header, except <TZ.h> maybe)

i am not a programmer but a sysadmin, i consider myself a self-learner guy, so sometimes things got harder to me... i am fairly new in programming and C and there are many things i dont know  (i.e. using git, do debug, etc) and lot of things i dont even imagine i am sure xD

mcspr commented 6 days ago

sub-frames are to blame... https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/time.h.html tm struct is both in c standard, and posix. my point here is this is not something platform-specific or unique to esp8266 implementation. cppreference page might've been a better link, as it mentions c standard.

i think it prefectly could save config to flash with configTime(ts, srv1, srv2, srv3) and then sync time with configTime() just using stored config, like WiFi.begin() does

not the case, and never mentioned in the example or the configTime() source) its a helper function that does setenv("TZ", ...); tzset(); and then sets up sntp servers in a single call. TZ is a runtime value, independent of the sntp. SNTP servers are also runtime values, independent of TZ. It is up to the user to set up these strings, either hard-coded or retrieved from some kind of dynamic config store

i would like to found some documentation about esp8266-arduino SNTP CORE functions (those used in NTP-TZ-DST without including any header, except maybe)

#include <sntp.h> does that, right at the top. without the include compiler would now know how to call those functions, just the way c and c++ work. dry symbol documentation can be found at the lwip site, example gives a possible use-case in the code directly

i am not a programmer but a sysadmin, i consider myself a self-learner guy, so sometimes things got harder to me... i am fairly new in programming and C and there are many things i dont know (i.e. using git, do debug, etc) and lot of things i dont even imagine i am sure xD

We cannot presume how much user does or does not know about the programming environment, and pretty sure these examples were never intended to be used as isolated learning exercises. API usage example, yes. Semi-interactive documentation, probably.