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

configTime and time seem to ignore DST #2505

Closed
tzapu opened this issue Sep 12, 2016 · 12 comments
Closed

configTime and time seem to ignore DST #2505

tzapu opened this issue Sep 12, 2016 · 12 comments

Comments

@tzapu
Copy link
Contributor

tzapu commented Sep 12, 2016

Basic Infos

configTime and time seem to ignore DST

Hardware

Hardware: ESP-12
Core Version: latest master

Description

doing a config time, with DST offset does not seem to be actually doing anything to returned time. Time zone offset applies alone.

Sketch

#include <Arduino.h>

void setup() {
  configTime(2 * 3600, 1 * 3600, "pool.ntp.org", "time.nist.gov");
  delay(2000);
  time_t now = time(nullptr);
  Serial.println(ctime(&now));
}

void loop() {

}

Debug Messages

Expected GMT + 3 to be printed. It actually prints GMT + 2

Cheers

@Juppit
Copy link
Contributor

Juppit commented Sep 12, 2016

For me it's working okay.

Try this:

#include <ESP8266WiFi.h>
#include "pass.h" /* ssid=; pass=; */
#include "time.h"

time_t this_second = 0;
time_t last_second = 0;
int dst=3600;

void setup() {
     Serial.begin(19200);
     WiFi.mode(WIFI_STA);
     WiFi.begin(ssid, pass);
     while (WiFi.status() != WL_CONNECTED)
     {
         Serial.print(".");
         delay(500);
     }
     /* let us set: timezone in sec, daylightOffset in sec, 
server_name1, server_name2, server_name3 */
     configTime(7200, 3600, "time.nist.gov", "time.windows.com", 
"de.pool.ntp.org");
     // wait until network has actual time
     while(!this_second)
     {
         time(&this_second);
         Serial.print("-");
         delay(100);
     }
     Serial.println();
}

void loop() {
     time(&this_second); // until time is set, it remains 0
     if (this_second != last_second)
     {
         last_second = this_second;
         if ((this_second % 60) == 0)
         {
             Serial.print(ctime(&this_second));
         }
     }
     delay(500);
}

The network needs some seconds to initialize the internal time.
I noticed, that the esp core needs about one hour, to update the time.

Obviously there is a fixed date when DST changes,
I suspect it corresponds to Shanghai time...

On 12.09.2016 at 09:40 wrote tzapu:

  Basic Infos

configTime and time seem to ignore DST

    Hardware

Hardware: ESP-12
Core Version: latest master

  Description

doing a config time, with DST offset does not seem to be actually doing
anything to returned time. Time zone offset applies alone.

  Sketch

#include <Arduino.h>

void setup() {
configTime(2 * 3600, 1 * 3600, "pool.ntp.org", "time.nist.gov");
delay(2000);
time_t now = time(nullptr);
Serial.println(ctime(&now));
}

void loop() {

}

  Debug Messages

Expected GMT + 3 to be printed. It actually prints GMT + 2

Cheers


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#2505, or mute the thread
https://github.com/notifications/unsubscribe-auth/AKUzpA7s1ZL6xtc3f3s0kDE3lGnKd1pIks5qpQH1gaJpZM4J6T42.

@Juppit
Copy link
Contributor

Juppit commented Sep 12, 2016

ATM I have only an old ESP-01 with lots of jumpers...

But I can confirm, parameter dst dosn't have any effect in the last SDK.

This behavior has changed in the last time. I dont know when, but there
was little change in the SDK.

On 12.09.2016 at 18:06 wrote Peter:

For me it's working okay.

Try this:

#include <ESP8266WiFi.h>
#include "pass.h" /* ssid=; pass=; */
#include "time.h"

time_t this_second = 0;
time_t last_second = 0;
int dst=3600;

void setup() {
    Serial.begin(19200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(500);
    }
    /* let us set: timezone in sec, daylightOffset in sec, server_name1,
server_name2, server_name3 */
    configTime(7200, 3600, "time.nist.gov", "time.windows.com",
"de.pool.ntp.org");
    // wait until network has actual time
    while(!this_second)
    {
        time(&this_second);
        Serial.print("-");
        delay(100);
    }
    Serial.println();
}

void loop() {
    time(&this_second); // until time is set, it remains 0
    if (this_second != last_second)
    {
        last_second = this_second;
        if ((this_second % 60) == 0)
        {
            Serial.print(ctime(&this_second));
        }
    }
    delay(500);
}

The network needs some seconds to initialize the internal time.
I noticed, that the esp core needs about one hour, to update the time.

Obviously there is a fixed date when DST changes,
I suspect it corresponds to Shanghai time...

On 12.09.2016 at 09:40 wrote tzapu:

  Basic Infos

configTime and time seem to ignore DST

    Hardware

Hardware: ESP-12
Core Version: latest master

  Description

doing a config time, with DST offset does not seem to be actually doing
anything to returned time. Time zone offset applies alone.

  Sketch

#include <Arduino.h>

void setup() {
configTime(2 * 3600, 1 * 3600, "pool.ntp.org", "time.nist.gov");
delay(2000);
time_t now = time(nullptr);
Serial.println(ctime(&now));
}

void loop() {

}

  Debug Messages

Expected GMT + 3 to be printed. It actually prints GMT + 2

Cheers


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#2505, or mute the thread
https://github.com/notifications/unsubscribe-auth/AKUzpA7s1ZL6xtc3f3s0kDE3lGnKd1pIks5qpQH1gaJpZM4J6T42.

e

Am 12.09.2016 um 18:06 schrieb Peter Dobler:

For me it's working okay.

Try this:

#include <ESP8266WiFi.h>
#include "pass.h" /* ssid=; pass=; */
#include "time.h"

time_t this_second = 0;
time_t last_second = 0;
int dst=3600;

void setup() {
    Serial.begin(19200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(500);
    }
    /* let us set: timezone in sec, daylightOffset in sec, server_name1,
server_name2, server_name3 */
    configTime(7200, 3600, "time.nist.gov", "time.windows.com",
"de.pool.ntp.org");
    // wait until network has actual time
    while(!this_second)
    {
        time(&this_second);
        Serial.print("-");
        delay(100);
    }
    Serial.println();
}

void loop() {
    time(&this_second); // until time is set, it remains 0
    if (this_second != last_second)
    {
        last_second = this_second;
        if ((this_second % 60) == 0)
        {
            Serial.print(ctime(&this_second));
        }
    }
    delay(500);
}

The network needs some seconds to initialize the internal time.
I noticed, that the esp core needs about one hour, to update the time.

Obviously there is a fixed date when DST changes,
I suspect it corresponds to Shanghai time...

On 12.09.2016 at 09:40 wrote tzapu:

  Basic Infos

configTime and time seem to ignore DST

    Hardware

Hardware: ESP-12
Core Version: latest master

  Description

doing a config time, with DST offset does not seem to be actually doing
anything to returned time. Time zone offset applies alone.

  Sketch

#include <Arduino.h>

void setup() {
configTime(2 * 3600, 1 * 3600, "pool.ntp.org", "time.nist.gov");
delay(2000);
time_t now = time(nullptr);
Serial.println(ctime(&now));
}

void loop() {

}

  Debug Messages

Expected GMT + 3 to be printed. It actually prints GMT + 2

Cheers


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#2505, or mute the thread
https://github.com/notifications/unsubscribe-auth/AKUzpA7s1ZL6xtc3f3s0kDE3lGnKd1pIks5qpQH1gaJpZM4J6T42.

@Booli
Copy link

Booli commented Jun 21, 2017

I am also experiencing this same behaviour. DST is not being taken into account.

@FriedCircuits
Copy link

Same problem here. I just adjusted the timezone by 1 hr to account for it.

Seems like at least in time.c this variable gets set and never used again.

s_daylightOffset_sec = daylightOffset_sec;

Granted I haven't looked deeper into the code.

@devyte
Copy link
Collaborator

devyte commented Oct 13, 2017

I still see the code referenced in the previous comment in latest git: the static variable s_daylightOffset_sec
is init, set, but never used.
Also, in file time.c function _gettimeofday_r() this looks wrong to me, probably a copy-pasto:
tp->tv_usec = micros() * 1000;

@Juppit
Copy link
Contributor

Juppit commented Oct 13, 2017

As long as there is no function in the SDK to set DST, sntp_get_current_timestamp will only be correct in Shanghai. :-)
I made a PR to correct _gettimeofday_r.

@mlinaje
Copy link

mlinaje commented Nov 4, 2017

A quick search for "s_daylightOffset_sec" in the repository https://github.com/esp8266/Arduino/search?utf8=%E2%9C%93&q=s_daylightOffset_sec&type= shows DST it is still not used.

An interesting function in ../core/sntp.c is:
void ICACHE_FLASH_ATTR sntp_set_daylight(int daylight){
if (sntp_get_timetype()){
RTC_DST_SET(daylight);
}
}

that seems to write the ESP8266 RTC memory, being RTC_DST_SET defined as:
do {uint32 value = READ_PERI_REG(RTC_STORE3);

However, I haven't found where sntp_set_daylight is invoked.

By the way, some native (i.e., system_) functions seem interesting:

`#include <time.h>

#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif

uint32 thetime, RTCtime, RTCcali;
time_t now;

void setup() {
// WiFi.begin(); // wifi disabled for example
// configTime(3*3600, 0, "not.avail.ntp");
Serial.begin(115200);
}

void loop() {
Serial.println();

now = time(nullptr);
thetime = system_get_time();
RTCtime = system_get_rtc_time();
RTCcali = system_rtc_clock_cali_proc();

Serial.print("time(): ");
Serial.print(ctime(&now));

Serial.print("system_get_time(): ");
Serial.println(thetime);

/*If system_get_rtc_time returns 10 (it means 10 RTC cycles), and
system_rtc_clock_cali_proc returns 5 (means 5us per RTC cycle), then the
real time is 10 x 5 = 50 us.
*/
Serial.print("system_get_rtc_time(): ");
Serial.println(RTCtime);

Serial.print("system_rtc_clock_cali_proc(): ");
Serial.println(RTCcali);

Serial.print("mul: ");
Serial.println(RTCtime * RTCcali);

delay(1000);
}`

@Burt-Silverman
Copy link

Juppit mentions:

while(!this_second)
{
time(&this_second);
Serial.print("-");
delay(100);
}

That is not a good test. this_second will be set to nonzero the first time time(&this_second) is executed regardless of the SNTP code. Something like this is more of a starting point if you set the global variables appropriately.

void
burtTime()
{
static bool gDash = true;

configTime(-5 * 3600, 3600, "pool.ntp.org", "time.nist.gov", "time.windows.com");
while(this_second < checkTime){ // wait until either network has actual time or this_second was merely bumped up
time(&this_second);
Serial.print(gDash ? '-' : '+');
delay(100);
}
if (this_second > checkTime) {
Serial.println();
Serial.println(this_second);
Serial.print(ctime(&this_second));
}
else ++checkTime;
gDash = !gDash;
}

In setup()
time(&this_second);
checkTime = this_second + 1;
burtTime();

In loop()
if (this_second < checkTime)
burtTime();

Or simply wait for the configTime() call to finish doing its thing.

@FernandoGarcia
Copy link

DST offset doesn't work for me too.

@tablatronix
Copy link
Contributor

#4637

@chrisspen
Copy link

Yeah, this is also happening for me. DST is completely ignored.

@d-a-v
Copy link
Collaborator

d-a-v commented Oct 1, 2019

SNTP support and example have receive updates, and this thread is old.
Closing but feel free to open a new issue based on git-current sntp code and example.

Current code allows to update sntp startup and update time,
also once your timezone is set (with "continent/city"), DST is automatically handled and automatically updated on transition time without reflash/reboot.
check example.

#6373 (comment)

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

Successfully merging a pull request may close this issue.