time.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <rtdevice.h>
  11. #ifdef RT_USING_DEVICE
  12. int gettimeofday(struct timeval *tp, void *ignore)
  13. {
  14. time_t time;
  15. rt_device_t device;
  16. device = rt_device_find("rtc");
  17. RT_ASSERT(device != RT_NULL);
  18. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  19. if (tp != RT_NULL)
  20. {
  21. tp->tv_sec = time;
  22. tp->tv_usec = 0;
  23. }
  24. return time;
  25. }
  26. #endif
  27. time_t time(time_t *t)
  28. {
  29. time_t time_now = 0;
  30. #ifdef RT_USING_RTC
  31. static rt_device_t device = RT_NULL;
  32. /* optimization: find rtc device only first. */
  33. if (device == RT_NULL)
  34. {
  35. device = rt_device_find("rtc");
  36. }
  37. /* read timestamp from RTC device. */
  38. if (device != RT_NULL)
  39. {
  40. if (rt_device_open(device, 0) == RT_EOK)
  41. {
  42. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  43. rt_device_close(device);
  44. }
  45. }
  46. #endif /* RT_USING_RTC */
  47. /* if t is not NULL, write timestamp to *t */
  48. if (t != RT_NULL)
  49. {
  50. *t = time_now;
  51. }
  52. return time_now;
  53. }
  54. RT_WEAK clock_t clock(void)
  55. {
  56. return rt_tick_get();
  57. }