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

Backport getLocalTime from ESP32 #8405

Open
h1aji opened this issue Dec 13, 2021 · 5 comments
Open

Backport getLocalTime from ESP32 #8405

h1aji opened this issue Dec 13, 2021 · 5 comments

Comments

@h1aji
Copy link

h1aji commented Dec 13, 2021

Was trying to port this code from ESP32, but found that getLocalTime doesnt exist. Is there a replacement?

void gettime()
{
  static int16_t delaycount = 0 ;                         // To reduce number of NTP requests
  static int16_t retrycount = 100 ;

  if ( timeinfo.tm_year )                                 // Legal time found?
  {
    sprintf ( timetxt, "%02d:%02d:%02d",                  // Yes, format to a string
              timeinfo.tm_hour,
              timeinfo.tm_min,
              timeinfo.tm_sec ) ;
  }
  if ( --delaycount <= 0 )                                // Sync every few hours
  {
    delaycount = 7200 ;                                   // Reset counter
    if ( timeinfo.tm_year )                               // Legal time found?
    {
      dbgprint ( "Sync TOD, old value is %s", timetxt ) ;
    }
    dbgprint ( "Sync TOD" ) ;
    if ( !getLocalTime ( &timeinfo, 5000 ) )              // Read from NTP server
    {
      dbgprint ( "Failed to obtain time!" ) ;             // Error
      timeinfo.tm_year = 0 ;                              // Set current time to illegal
      if ( retrycount )                                   // Give up syncing?
      {
        retrycount-- ;                                    // No try again
        delaycount = 5 ;                                  // Retry after 5 seconds
      }
    }
    else
    {
      sprintf ( timetxt, "%02d:%02d:%02d",                // Format new time to a string
                timeinfo.tm_hour,
                timeinfo.tm_min,
                timeinfo.tm_sec ) ;
      dbgprint ( "Sync TOD, new value is %s", timetxt ) ;
    }
  }
}

Or is there any chance to add this from esp32-hal-time.c

bool getLocalTime(struct tm * info, uint32_t ms)
{
    uint32_t start = millis();
    time_t now;
    while((millis()-start) <= ms) {
        time(&now);
        localtime_r(&now, info);
        if(info->tm_year > (2016 - 1900)){
            return true;
        }
        delay(10);
    }
    return false;
}
@d-a-v
Copy link
Collaborator

d-a-v commented Dec 13, 2021

Why don't you directly use localtime() ?

Anyway if you think this core should get bool getLocalTime(struct tm * info, uint32_t ms) for better inter-operability, then your PR is welcome.

@h1aji
Copy link
Author

h1aji commented Dec 13, 2021

Can you give me a tip? do you mean if ( !localtime ( &timeinfo ) ) ?

@d-a-v
Copy link
Collaborator

d-a-v commented Dec 13, 2021

Yes. There is an example.
I guess you can also simply copy-paste and use your above getLocalTime().

@h1aji
Copy link
Author

h1aji commented Dec 14, 2021

Tried localtime_r() but it shows incorrect time.
Anyway, its good to have this function for ESP8266 available. Created PR, please review here #8407
Thanks

@h1aji
Copy link
Author

h1aji commented Dec 14, 2021

new PR here #8407

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

No branches or pull requests

2 participants