esp8266 / esp8266/Arduino

Setting timezone with setenv doesn't work properly any more with V 2.7.1

Open
#7,353 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

***EDIT:***
*Reference is in shipped examples, namely [NTP-TZ-DST.ino](https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino).*
----

### Basic Infos

- [x] This issue complies with the [issue POLICY doc](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [x] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
- [x] I have tested that the issue is present in current master branch (aka latest git).
- [x] I have searched the issue tracker for a similar issue.
- [x] If there is a stack dump, I have decoded it.
- [x] I have filled out all fields below.

#### Platform

- Hardware: [ESP-12]
- Core Version: [2.7.1]
- Development Env: [Arduino IDE]
- Operating System: [Windows]

### Settings in IDE

- Module: [Nodemcu]
- Flash Mode: [qio|dio|other]
- Flash Size: [4MB]
- lwip Variant: [v2 Lower Memory]
- Reset Method: [nodemcu]
- Flash Frequency: [40Mhz]
- CPU Frequency: [80Mhz]
- Upload Using: [OTA AND SERIAL]
- Upload Speed: [115200] (serial upload only)

### Problem Description

The follwing two code snippets worked fine with V 2.6.3:

```
configTime(0, 0, "pool.ntp.org");
setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03" , 1);
```
or, in changed order:
```
setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03" , 1);
configTime(0, 0, "pool.ntp.org");
```
It always delivered the right local time.
With the current version, this seems to be broken - in a very strange way: **Only the first call** of the time function delivers the right local time, all following time server calls don't set the local time and the function delivers UTC time only. I read the changelog and searched the issues, but I coludn't find any explanation. Has there been a change regarding the definition of the local timezone or within the setenv function?

If required, here's the complete code of my time function which worked very well with V 2.6.3:
```
struct tm tm;
char timeshow[10];

void getTimeFromServer(){
uint8_t time_retry=0; // Counter retry counts time server
setenv("TZ", "CET-1CEST,M3.5.0/02,M10.5.0/03", 1);
struct tm initial; // temp struct for checking if year==1970
initial.tm_year=70;

while (initial.tm_year == 70 && time_retry < 15) {
configTime(0, 0, "pool.ntp.org"); // get time from NTP server (ESP8266)
delay(500);
time_t now = time(&now);
localtime_r(&now, &initial);
Serial.print("Time Server connection attempt: ");
Serial.println(time_retry + 1);
Serial.print("current year: ");
Serial.println(1900 + initial.tm_year);
time_retry++;
}

if (time_retry >=15) {
Serial.println("Connection to time server failed");
} else {
time_t now = time(&now);
localtime_r(&now, &tm);
strftime (timeshow, sizeof(timeshow), "%H:%M", &tm);
Serial.print("Successfully requested current time from server: ");
Serial.println(timeshow);
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.