Thanks to visit codestin.com
Credit goes to github.com

Skip to content

configTime with TZ offset causes incorrect UTC time #6921

Closed
@vlastahajek

Description

@vlastahajek

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-12
  • Core Version: d40dbb4
  • Development Env: Arduino IDE 1.8.10
  • Operating System: Windows 10

Settings in IDE

  • Module: Wemos D1 mini
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 921600

Problem Description

When using the void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr) with non-zero timezone offset to synchronize time, device doesn't differentiate between local and UTC time, where UTC time is wrongly set to local time.

This works OK on ESP32 (1.0.4).

I've checked #6678, but it seems it is just about initialization issue.

The workaround is using void configTime(const char* tz, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr).

MCVE Sketch

#if defined(ESP32)
# include <WiFiMulti.h>
  WiFiMulti wifiMulti;
#elif defined(ESP8266)
# include <ESP8266WiFiMulti.h>
  ESP8266WiFiMulti wifiMulti;
#endif

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

  Serial.println();
  
  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP("SSID","PASSWORD");

  Serial.print("Connecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // TZ offset +1h
  configTime(3600, 0 , "pool.ntp.org", "time.nis.gov");
  //configTime("CET-1CEST,M3.5.0,M10.5.0/3" , "pool.ntp.org", "time.nis.gov");
  // Wait till time is synced
  Serial.print("Syncing time");
  int i = 0;
  while (time(nullptr) < 1000000000ul && i<100) {
    Serial.print(".");
    delay(100);
    i++;
  }
  Serial.println();

  time_t tnow = time(nullptr);
  struct tm *timeinfo;
  char buffer [80];


  timeinfo = localtime (&tnow);
  strftime (buffer,80,"Local time: %H:%M.",timeinfo);
  Serial.println(buffer);
  
  timeinfo = gmtime (&tnow);
  strftime (buffer,80,"UTC time: %H:%M.",timeinfo);
  Serial.println(buffer); 
}

void loop() {
  // put your main code here, to run repeatedly:

}

Debug Messages

Connecting to wifi......................
Syncing time.
Local time: 11:17.
UTC time: 11:17.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions