Time library functions with ESP32 core

Does anyone now what the equivalent of the following is with ESP32 core?

The time example is useless - all it does is use a function to print the current time to serial monitor.

And I find it difficult to believe that this is the only thing you can do with ESP32 time library.

Tried looking ESP32 core version for time.h and couldn't make much sense of it in terms of the functions I am after.

year(nEpoch);
month(nEpoch);
day(nEpoch);

Useless time library example

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

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

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

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

void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}
1 Like

Or failing that does anyone have a reliable algorithm to calculate the year and day of year from the epoch time stamp.

With those I can easily calculate the month and day of month.

But on googling I keep seeing people writing that it is difficult to calculate these.

But they managed to do it with the AVR version of Time library, so there must be a reliable algorithm.

If my code could be independent of the savageries of time library versions then all the better!

Hi,

check out the Examples:
Open in your Arduino IDE
Examples -> ESP32 -> Time -> Simple Time

always glad to help :slight_smile:

The esp products use the standard POSIX time library functions.
time(), gmtime(), localtime(), mktime(), gettimeofday() etc.
You can google them to see how they work. (they haven't changed in decades)
To get the broken down into elements you pass a pointer to a time_t to localtime() that fills in the tm structure.
Then reference any member you need.

--- bill

1 Like

Hey !! It's possible to accesses date and time parameters separably.

byte Krish_day;
byte Krish_month;
int Krish_year;

void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
//Serial.println(&timeinfo, "%m %d %Y / %H:%M:%S");
//scanf(&timeinfo, "%m %d %Y / %H:%M:%S")
Krish_hour = timeinfo.tm_hour;
Krish_min = timeinfo.tm_min;
Krish_sec = timeinfo.tm_sec;

Krish_day = timeinfo.tm_mday;
Krish_month = timeinfo.tm_mon + 1;
Krish_year = timeinfo.tm_year +1900;
}

7 Likes

Hello, people. I'm attempting to change the SimpleTime example to show time on an LCD display. At the setup, if I write something in the LCD before the printLocalTime(), it works, but after the printLocalTime(), any call to the LCD functions is totally ignored. I can not find useful examples in the web… Some good soul can help me?

Thanks Abhay

that was useful information.

audiocrush:
Hi,

check out the Examples:
Open in your Arduino IDE
Examples -> ESP32 -> Time -> Simple Time

always glad to help :slight_smile:

Hey! I'm not sure if you read the OP's message clearly, your Simple Time is exactly what he called useless

1 Like

Abhay:
Hey !! It's possible to accesses date and time parameters separably.

byte Krish_day;
byte Krish_month;
int Krish_year;

void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
//Serial.println(&timeinfo, "%m %d %Y / %H:%M:%S");
//scanf(&timeinfo, "%m %d %Y / %H:%M:%S")
Krish_hour = timeinfo.tm_hour;
Krish_min = timeinfo.tm_min;
Krish_sec = timeinfo.tm_sec;

Krish_day = timeinfo.tm_mday;
Krish_month = timeinfo.tm_mon + 1;
Krish_year = timeinfo.tm_year +1900;
}

That is EXACTLY what I needed ! I was searching how to retrieve separate dates and hours and stuff for more than a day ...

Thanks Thanks Thanks !!!

2 Likes

If you want to thank someone who posted a year ago, I suggest sending a PM as it is more likely to be seen.

time.h uses the onboard RTC?

and millis() also uses the onboard RTC?

I wrote a library to set and retrieve time on ESP32 boards, check it out

1 Like

fbiego:
I wrote a library to set and retrieve time on ESP32 boards, check it out

ESP32Time - Arduino Libraries

I think sticking with the bundled/built-in POSIX time API functions would be better as the API functions are well documented since they have't changed in like 40+ years and it is portable across all platforms including linux.
Just combine it with the ESP specific functions to set the TZ environment variable and NTP configuration to initialize things.

Your library is missing quite a bit as it is a wrapper for the bundled time functions but there there does not appear to be a way to set the TZ variable which configures the local timezone information,including DST rules, and it doesn't appear to be able to configure NTP.

--- bill

I needed a way to set the time without using NTP. Might be helpful if you don't want to access the NTP server.
In this project, ESP32_OLED_BLE, I am setting the time via BLE from the phone's local time

fbiego:
I needed a way to set the time without using NTP. Might be helpful if you don't want to access the NTP server.
In this project, ESP32_OLED_BLE, I am setting the time via BLE from the phone's local time

You don't have to use NTP to use the time functions on the esp32.

--- bill

bperrybap:
You don't have to use NTP to use the time functions on the esp32.

Maybe, but the ESP time.h core does not provide a method to set the time.
Or I did not find it.
Give me a clue!

RIN67630:
Maybe, but the ESP time.h core does not provide a method to set the time.
Or I did not find it.
Give me a clue!

The POSIX standard C library function settimeofday()
Same as what is used on linux.
It has existed for decades, long before linux existed.
It is declared in <sys/time.h>
It is mentioned in the esp32 documents:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system_time.html

This function is what fbiego's ESP32Time library uses under the hood in his setTime() method.
(from post #11)

--- bill

bperrybap:
The POSIX standard C library function settimeofday()

This function is what fbiego's ESP32Time library uses under the hood in his setTime() method.
--- bill

That's what I have done as well:

    if (Serial.available() > 0)
    {
      // read in the user input
      Day = Serial.parseInt();
      Month = Serial.parseInt();
      Year = Serial.parseInt();
      Hour = Serial.parseInt();
      Minute = Serial.parseInt();
      Second = Serial.parseInt();
      Console1.printf("I have understood %u/%u/%u %u:%u:%u\n", Day, Month, Year, Hour, Minute, Second);
      boolean validDate = (inRange(Day, 1, 31) && inRange(Month, 1, 12) && inRange(Year, 2021, 2031));
      boolean validTime = (inRange(Hour, 0, 23) && inRange(Minute, 0, 59) && inRange(Second, 0, 59));
      if (validTime && validDate)
      {
        struct tm t;
          time_t t_of_day;
          t.tm_year = Year-1900;  // Year - 1900
          t.tm_mon = Month-1;       // Month, where 0 = jan
          t.tm_mday = Day-1;        // Day of the month
          t.tm_hour = Hour;
          t.tm_min = Minute;
          t.tm_sec = Second;
          t.tm_isdst = -1;         // Is DST on? 1 = yes, 0 = no, -1 = unknown
        t_of_day = mktime(&t);
        sprintf(charbuff, "Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d", Hour, Minute, Second, t_of_day, DayName, Day, MonthName, Year);
        Console3.println(charbuff);
        struct timeval tv;
          tv.tv_sec = t_of_day;  // epoch time (seconds)
          tv.tv_usec = 0;    // microseconds
         settimeofday(&tv,0);
      }
    }

I got it to work, but I find it a bit clumsy and it gives me an offset of 8 hours.
??

[/code]

//t.tm_mday = Day-1; // Day of the month
t.tm_mday = Day

Days of the month run from 1-31.

They are not 0 based like the Month names which you corrected with the -1.

RIN67630:
I got it to work, but I find it a bit clumsy and it gives me an offset of 8 hours.
??

Have you properly set up the TZ variable?

mktime() takes a local time not a GMT local time to generate the time_t value.

I haven't tested that the ESP32 mktime() works properly when using local time.
I set the time using GMT.
i.e.
I keep time in the RTC as GMT set the system time then set the TZ variable to get local time.

--- bill

1 Like