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

sntp_set_timezone_in_seconds doesn't seem to have any effect #6678

Closed
5 of 6 tasks
osmtools opened this issue Oct 28, 2019 · 7 comments · Fixed by #6993
Closed
5 of 6 tasks

sntp_set_timezone_in_seconds doesn't seem to have any effect #6678

osmtools opened this issue Oct 28, 2019 · 7 comments · Fixed by #6993
Assignees
Milestone

Comments

@osmtools
Copy link

osmtools commented Oct 28, 2019

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP-12E (LoLin NodeMCU V3)
  • Core Version: ba50bd5
  • Development Env: Arduino IDE 1.8.10
  • Operating System: Ubuntu LTS 18.04.2

Settings in IDE

  • Module: NodeMCU 1.0 (ESP-12E Module)
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 115200

Problem Description

No matter what value I assign to the timezone_sec arg in

void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)

the output of time_t now = time(nullptr); Serial.println(time(&now)); Serial.println(ctime(&now)); will be UTC+8.

So if i set timezone_sec=0 i would expect the output of GMT/UTC but it won't be.

If i set

static sint32 time_zone = 8 * (60 * 60); // espressif HQ's default timezone

to 0 instead, then you will get the time in GMT/UTC.

It seems that the method

bool sntp_set_timezone_in_seconds(sint32 timezone)

hasn't any effect to the global variable time_zone

Also have a look at #6675

MCVE Sketch

#include <ESP8266WiFi.h>
#include <time.h>

const char* ssid       = "...";
const char* password   = "...";

const int timezone_sec = 0;
const int daylightOffset_sec = 0;
const char* ntpServer = "pool.ntp.org";

void setup()
{
  Serial.begin(115200);
  
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  Serial.println("Call configTime...");
  configTime(timezone_sec, daylightOffset_sec, ntpServer);

  Serial.println("Now it's:");
  time_t now = time(nullptr);
  Serial.println(time(&now));
  Serial.println(ctime(&now));

  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
}

Debug Messages

Run at Mon Oct 28 08:03:51 2019 (GMT/UTC)

Connecting to xxx ....... CONNECTED
Call configTime...
Now it's:
1572275031
Mon Oct 28 15:03:51 2019
@devyte
Copy link
Collaborator

devyte commented Oct 30, 2019

I suspect that your usage is wrong: you're supposed to wait for the time callback to fire, which happens once the time on board has actually been set from the sntp server.
My own run of your MCVE shows that the time certainly has not been set, I get this:

Call configTime...
Now it's:
28804
Thu Jan  1 08:00:04 1970

If I modify the sketch to set a callback and not disconnect right after, I can see the cb firing something like 5 seconds later.
Still, something is fishy, because the above printout does show +8h despite being passed 0 for timezone, so I think it should be Thu Jan 1 00:00:04 1970.

CC @d-a-v .

@osmtools
Copy link
Author

@devyte if i remove

  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);

in setup() and add

  delay(1000);
  Serial.println("Now it's:");
  time_t now = time(nullptr);
  Serial.println(time(&now));
  Serial.println(ctime(&now));

to loop() i get indeed the following debug output:

Connecting to FRITZ!Box WLAN 3170 ....... CONNECTED
Call configTime...
Now it's:
1572536653
Thu Oct 31 15:44:13 2019

Now it's:
1572507853
Thu Oct 31 07:44:13 2019

Now it's:
1572507854
Thu Oct 31 07:44:14 2019

So the first call in loop() is +8hours, the second call in setup() is just fine.

@d-a-v
Copy link
Collaborator

d-a-v commented Oct 31, 2019

That's because two reasons:

  • you configure time after internals send a first sntp request
    this explains why you get two different results
  • you don't let time for SNTP answer to be received
    you can use the callback to know exactly when time has been configured

I updated your MCVE as follow:

#include <ESP8266WiFi.h>
#include <time.h>
#include <coredecls.h>

const int timezone_sec = 0;
const int daylightOffset_sec = 0;
const char* ntpServer = "pool.ntp.org";

void time_is_set ()
{
  Serial.println("Now it's:");
  time_t now = time(nullptr);
  Serial.println(time(&now));
  Serial.println(ctime(&now));

  //WiFi.disconnect(true);
  //WiFi.mode(WIFI_OFF);
}

void setup()
{
  Serial.begin(115200);
  settimeofday_cb(time_is_set);

  Serial.println("Call configTime...");
  configTime(timezone_sec, daylightOffset_sec, ntpServer);
  // move the two above lines after(*) "connected" below and re-run

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");

  // here(*)
}

void loop()
{
}

But I think you'd get better results with the provided example

If the example is too complex, or doesn't match the general use-case,
I'd be pleased if you proposed another one.

@devyte
Copy link
Collaborator

devyte commented Nov 5, 2019

@d-a-v my one question here, per my comment anf experiment above, is: assuming

  • a fresh boot
  • no sntp response
  • setting of timezone to 0 via configTime

then shouldn't ctime(time(nullptr)) print 00:00 instead lf 08:00?

@thorsten-l
Copy link

#include <time.h>
#include <sys/time.h>
...

void setup() {
// initialize internal timer and timezone to UTC (1970-01-01 00:00:00)
struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 0;
struct timezone tz;
tz.tz_dsttime = 0;
tz.tz_minuteswest = 0;
settimeofday( &tv, &tz );

@d-a-v
Copy link
Collaborator

d-a-v commented Dec 19, 2019

@thorsten-l

The second argument is ignored.

Have you seen the provided example accessible from the arduino IDE ?

edit: BTW this issue should be fixed by #6828 (already merged, available in 2.6.3)

@d-a-v d-a-v added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Jan 6, 2020
@d-a-v
Copy link
Collaborator

d-a-v commented Jan 6, 2020

Is this issue still relevant ?

edit will be fixed by #6993

d-a-v added a commit to d-a-v/Arduino that referenced this issue Jan 7, 2020
@d-a-v d-a-v added fixed-by-PR and removed waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. labels Jan 24, 2020
d-a-v added a commit that referenced this issue Feb 18, 2020
* configTime(tzsec,dstsec,): fix UTC/local management
This PR also remove dead code since probably newlib updates
The NTP-TZ-DST example is also updated
* restore sntp_set_timezone_in_seconds()
fixes #6678
* +configTzTime()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants