Fundamental questions about time_t variable and "time" function.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Fundamental questions about time_t variable and "time" function.

Postby Zingemneire » Tue Jun 05, 2018 9:55 am

Hi,

It may be listed somewhere or the question has been answered before but I can't find it as I can't search the forum for "type_t" exactly. It always returns the search results for "time" and says it ignored the "t" and the underscore which results in a few thousand search results. So: here are my questions about "time_t" and the "time" function.

1) What type of variable is time_t? a signed or unsigned 32 bit integer or something else entirely ?
2) What date/time does 0 in variable time_t represent? in FreeRTOS it represents 01/01/2000 00:00:00, in sntp it is 01/01/1990 00:00:00, in Unix/Linux it is 01/01/1970 00:00:00 => so what is the base value in ESP32?
3) is there a description somewhere of how the "time" function works in ESP32? I have looked through all sorts of header files and find abstractions such as _EXFUN etc... but I failed to find the function that actually reads the time from the chip's RTC. Just knowing which file to look at would be OK as well.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Re: Fundamental questions about time_t variable and "time" function.

Postby Zingemneire » Tue Jun 05, 2018 11:08 am

OK, the answer to questions 1&2 are: 0 = 01/01/1970 at 00:00:00 and it is a 4 byte value. ( no idea if is signed or unsigned ). Deduced it by converting a time_t value of 0 to a tm structure via the localtime_r function and simply displaying the year result ( 70 => +1900 = 1970 ).

In addition to the still open question 3 It begs a few other questions though:

- How long can the RTC keep on running? if time_t is a signed 32 bit integer as in Unix/Linux the show stops sometime in 2038.
- How does the RTC work? is it keeping time by means of a 32 bit second counter ? or does it have separate year/month/day etc... registers?
- How does the sntp component, and by extension, the "sntp_example_main.c" handle the difference between being based on 01/01/1990 and the Unix/Linux time_t being based on 01/01/1970 ?

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Fundamental questions about time_t variable and "time" function.

Postby ESP_igrr » Tue Jun 05, 2018 1:35 pm

Zingemneire wrote: is there a description somewhere of how the "time" function works in ESP32?
"time" is implemented in newlib, it calls _gettimeofday_r, which is implemented in IDF. Remaining parts of the same time.c file in IDF handle support for keeping time in RTC and/or FRC (which is a high-resolution timer clocked from main XTAL when the chip is not in sleep mode). There is also code there for other time-related functions, such as "settimeofday", "adjtime", etc.
Zingemneire wrote: - How long can the RTC keep on running? if time_t is a signed 32 bit integer as in Unix/Linux the show stops sometime in 2038.
- How does the RTC work? is it keeping time by means of a 32 bit second counter ? or does it have separate year/month/day etc... registers?
RTC in ESP32 has a 48-bit counter, which runs at RTC_SLOW_CLK frequency. This is either 160kHz (when using internal oscillator) or 32kHz (when using external XTAL). The counter always starts at zero when chip is reset by power-on reset, external reset, RTC watchdog reset, or RTC system reset. When software adjusts time (using e.g. settimeofday or adjtime) this does not affect RTC counter. Offset between Unix time and RTC time is maintained separately (see set_boot_time function). Internally in IDF source code time is often stored in microseconds since Epoch or microseconds since device startup, as a 64-bit value.
Zingemneire wrote: - How does the sntp component, and by extension, the "sntp_example_main.c" handle the difference between being based on 01/01/1990 and the Unix/Linux time_t being based on 01/01/1970 ?
LwIP SNTP module does the conversion and passes Unix timestamp via SNTP_SET_SYSTEM_TIME_US macro to settimeofday function.

Zingemneire
Posts: 68
Joined: Tue Apr 17, 2018 7:35 am

Re: Fundamental questions about time_t variable and "time" function.

Postby Zingemneire » Wed Jun 06, 2018 6:50 am

Hi,

Thanks for the info, part of it I worked out myself already but I will look at the extra info you provided.

Best Regards.

Who is online

Users browsing this forum: No registered users and 134 guests