@@ -31,23 +31,27 @@ struct timeval {
31
31
32
32
extern char * sntp_asctime (const struct tm * t );
33
33
extern struct tm * sntp_localtime (const time_t * clock );
34
+ extern uint64_t micros64 ();
34
35
35
36
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
36
37
#define DIFF1900TO1970 2208988800UL
37
38
38
39
static int s_daylightOffset_sec = 0 ;
39
40
static long s_timezone_sec = 0 ;
40
- static time_t s_bootTime = 0 ;
41
+ static bool s_bootTimeSet = false;
42
+ static uint64_t s_bootTime_us = 0 ;
41
43
42
44
// calculate offset used in gettimeofday
43
45
static void ensureBootTimeIsSet ()
44
46
{
45
- if (!s_bootTime )
47
+ // Check just a bool flag instead of the full 64-bit s_bootTime for zero.
48
+ if (!s_bootTimeSet )
46
49
{
47
- time_t now = sntp_get_current_timestamp ();
48
- if (now )
50
+ time_t now_s = sntp_get_current_timestamp ();
51
+ if (now_s )
49
52
{
50
- s_bootTime = now - millis () / 1000 ;
53
+ s_bootTime_us = now_s * 1000000ULL - micros64 ();
54
+ s_bootTimeSet = true;
51
55
}
52
56
}
53
57
}
@@ -100,8 +104,9 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
100
104
if (tp )
101
105
{
102
106
ensureBootTimeIsSet ();
103
- tp -> tv_sec = s_bootTime + millis () / 1000 ;
104
- tp -> tv_usec = micros ();
107
+ uint64_t currentTime_us = s_bootTime_us + micros64 ();
108
+ tp -> tv_sec = currentTime_us / 1000000ULL ;
109
+ tp -> tv_usec = currentTime_us % 1000000ULL ;
105
110
}
106
111
return 0 ;
107
112
}
0 commit comments