time.c 1.3 KB

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