1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 */ 9 #include <sys/time.h> 10 #include <rtthread.h> 11 12 #ifdef RT_USING_DEVICE 13 int gettimeofday(struct timeval *tp, void *ignore) 14 { 15 time_t time; 16 rt_device_t device; 17 18 device = rt_device_find("rtc"); 19 if (device != RT_NULL) 20 { 21 rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time); 22 if (tp != RT_NULL) 23 { 24 tp->tv_sec = time; 25 tp->tv_usec = 0; 26 } 27 28 return time; 29 } 30 31 return 0; 32 } 33 #endif 34 35 /** 36 * Returns the current time. 37 * 38 * @param time_t * t the timestamp pointer, if not used, keep NULL. 39 * 40 * @return time_t return timestamp current. 41 * 42 */ 43 #pragma module_name = "?time" 44 #if _DLIB_TIME_USES_64 45 time_t __time64(time_t *t) 46 #else 47 /* for IAR 6.2 later Compiler */ 48 #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000 49 time_t __time32(time_t *t) 50 #else 51 time_t time(time_t *t) 52 #endif 53 #endif 54 { 55 time_t time_now = 0; 56 57 #ifdef RT_USING_RTC 58 static rt_device_t device = RT_NULL; 59 60 /* optimization: find rtc device only first. */ 61 if (device == RT_NULL) 62 { 63 device = rt_device_find("rtc"); 64 } 65 66 /* read timestamp from RTC device. */ 67 if (device != RT_NULL) 68 { 69 if (rt_device_open(device, 0) == RT_EOK) 70 { 71 rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now); 72 rt_device_close(device); 73 } 74 } 75 #endif /* RT_USING_RTC */ 76 77 /* if t is not NULL, write timestamp to *t */ 78 if (t != RT_NULL) 79 { 80 *t = time_now; 81 } 82 83 return time_now; 84 } 85 86 RT_WEAK clock_t clock(void) 87 { 88 return rt_tick_get(); 89 } 90