Skip to content

Commit 76be91c

Browse files
committed
[libc/time] Add microseconds time get feature in gettimeofday.
1 parent 2a66024 commit 76be91c

File tree

1 file changed

+34
-25
lines changed
  • components/libc/compilers/common

1 file changed

+34
-25
lines changed

components/libc/compilers/common/time.c

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct tm* localtime_r(const time_t* t, struct tm* r)
120120
time_t local_tz;
121121
int utc_plus;
122122

123-
utc_plus = 0; /* GTM: UTC+0 */
123+
utc_plus = 8; /* GMT: UTC+8 */
124124
local_tz = *t + utc_plus * 3600;
125125
return gmtime_r(&local_tz, r);
126126
}
@@ -183,18 +183,14 @@ char* ctime(const time_t *tim_p)
183183
}
184184
RTM_EXPORT(ctime);
185185

186-
/**
187-
* Returns the current time.
188-
*
189-
* @param time_t * t the timestamp pointer, if not used, keep NULL.
190-
*
191-
* @return The value ((time_t)-1) is returned if the calendar time is not available.
192-
* If timer is not a NULL pointer, the return value is also stored in timer.
193-
*
194-
*/
195-
RT_WEAK time_t time(time_t *t)
186+
static void get_timeval(struct timeval *tv)
196187
{
197-
time_t time_now = ((time_t)-1); /* default is not available */
188+
if (tv == RT_NULL)
189+
return;
190+
/* default is not available */
191+
tv->tv_sec = -1;
192+
/* default is 0 */
193+
tv->tv_usec = 0;
198194

199195
#ifdef RT_USING_RTC
200196
static rt_device_t device = RT_NULL;
@@ -210,26 +206,41 @@ RT_WEAK time_t time(time_t *t)
210206
{
211207
if (rt_device_open(device, 0) == RT_EOK)
212208
{
213-
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
209+
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec);
210+
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec);
214211
rt_device_close(device);
215212
}
216213
}
217214
#endif /* RT_USING_RTC */
218215

219-
/* if t is not NULL, write timestamp to *t */
220-
if (t != RT_NULL)
221-
{
222-
*t = time_now;
223-
}
224-
225-
if(time_now == (time_t)-1)
216+
if (tv->tv_sec == (time_t) -1)
226217
{
227218
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
228219
rt_kprintf("Cannot find a RTC device to provide time!\r\n");
229220
errno = ENOSYS;
230221
}
222+
}
231223

232-
return time_now;
224+
/**
225+
* Returns the current time.
226+
*
227+
* @param time_t * t the timestamp pointer, if not used, keep NULL.
228+
*
229+
* @return The value ((time_t)-1) is returned if the calendar time is not available.
230+
* If timer is not a NULL pointer, the return value is also stored in timer.
231+
*
232+
*/
233+
RT_WEAK time_t time(time_t *t)
234+
{
235+
struct timeval now;
236+
237+
get_timeval(&now);
238+
239+
if (t)
240+
{
241+
*t = now.tv_sec;
242+
}
243+
return now.tv_sec;
233244
}
234245
RTM_EXPORT(time);
235246

@@ -344,12 +355,10 @@ RTM_EXPORT(timegm);
344355
/* TODO: timezone */
345356
int gettimeofday(struct timeval *tv, struct timezone *tz)
346357
{
347-
time_t t = time(RT_NULL);
358+
get_timeval(tv);
348359

349-
if (tv != RT_NULL && t != (time_t)-1)
360+
if (tv != RT_NULL && tv->tv_sec != (time_t) -1)
350361
{
351-
tv->tv_sec = t;
352-
tv->tv_usec = 0;
353362
return 0;
354363
}
355364
else

0 commit comments

Comments
 (0)