Esp32 example help

I have used the SimpleTime.ino sketch for esp32. I would like to find the code that gets the time from the buffer.

where did you get the demo program from?

From the examples on the arduino ide.

is this the program?

#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"

const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASS";

const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;

const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3";  // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("No time available (yet)");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();
}

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

  // First step is to configure WiFi STA and connect in order to get the current time and date.
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);

  /**
   * NTP server address could be acquired via DHCP,
   *
   * NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
   * otherwise SNTP option 42 would be rejected by default.
   * NOTE: configTime() function call if made AFTER DHCP-client run
   * will OVERRIDE acquired NTP server address
   */
  esp_sntp_servermode_dhcp(1);  // (optional)

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");

  // set notification call-back function
  sntp_set_time_sync_notification_cb(timeavailable);

  /**
   * This will set configured ntp servers and constant TimeZone/daylightOffset
   * should be OK if your time zone does not need to adjust daylightOffset twice a year,
   * in such a case time adjustment won't be handled automagically.
   */
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

  /**
   * A more convenient approach to handle TimeZones with daylightOffset
   * would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
   * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
   */
  //configTzTime(time_zone, ntpServer1, ntpServer2);
}

void loop() {
  delay(5000);
  printLocalTime();  // it will take some time to sync time :)
}

from Example\SimpleTime.ino help - #3 by gilshultz

Yes. My question is not really about any one program, it's really about finding out what is happening in the libraries. Arduino is very good at providing working examples, but I find it very difficult to dig deeper.

the library source is available. is this what you want or do you want an explanation of how it works?

perhaps you're interested in time.h

Looks like the timespec structure should do what I want. Thanks

All of the above code is "the old way"
NTP with DST is now included in the latest ESP core.
This is the absolute minimum you need.
Default NTP update is three hours.

#include <WiFi.h>  // ESP32
unsigned long prevTime;
time_t now;  // holds epoch
tm tm;       // time struct

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PASS"); // WiFi credentials
   // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  configTzTime("GMT0BST,M3.5.0/1,M10.5.0", "uk.pool.ntp.org");  // London
}

void loop() {
  time(&now);
  if (now != prevTime) {     // if time (in seconds) has changed
    prevTime = now;          // remember
    localtime_r(&now, &tm);  // convert epoch to local time
    printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}

and with added callback, to wait for a valid NTP update

#include <WiFi.h>  // ESP32
#include "esp_sntp.h" // for callback
unsigned long prevTime;
bool reSync;
time_t now;  // holds epoch
tm tm;       // time struct
char timeStr[9];

void cbSyncTime(struct timeval *tv) {  // sync callback
  reSync = true;
  Serial.println("Time was updated"); // prints every three hours
}

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PASS"); // WiFi credentials
  sntp_set_time_sync_notification_cb(cbSyncTime); // enable callback
  configTzTime("GMT0BST,M3.5.0/2,M10.5.0/2", "uk.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  while (!reSync) yield(); // wait here for a valid time string
}

void loop() {
  time(&now);
  if (now != prevTime) {     // if time has changed
    prevTime = now;          // remember
    localtime_r(&now, &tm);  // convert epoch to local time
    printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
} // to print more elements, month, year, summer/winter etc. see this page:
  // https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm

I've found an example that uses ntpclient. I've modified the library to return the fractional part of the timestamp.

Most examples with ntpclient for the ESP32 are outdated.
It will work, but my way is newer/shorter.
Which fractional pard do you need.
As hinted at the bottom of post#8, there are more time elements available.

  Serial.print("year:");
  Serial.print(tm.tm_year + 1900);    // years since 1900
  Serial.print("\tmonth:");
  Serial.print(tm.tm_mon + 1);        // January = 0 (!)
  Serial.print("\tday:");
  Serial.print(tm.tm_mday);           // day of month
  Serial.print("\thour:");
  Serial.print(tm.tm_hour);           // hours since midnight 0-23
  Serial.print("\tmin:");
  Serial.print(tm.tm_min);            // minutes after the hour 0-59
  Serial.print("\tsec:");
  Serial.print(tm.tm_sec);            // seconds after the minute 0-61*
  Serial.print("\twday");
  Serial.print(tm.tm_wday);           // days since Sunday 0-6
  if (tm.tm_isdst == 1)               // Daylight Saving Time flag
    Serial.print("\tDST");  
  else
    Serial.print("\tstandard");
  Serial.println();

and
Serial.print (now); // prints epoch
Which you can save as a timestamp and later convert again to local time with localtime_r(&now, &tm);
Leo..