time.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #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. if (device != RT_NULL)
  18. {
  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. return time;
  26. }
  27. return 0;
  28. }
  29. #endif
  30. /**
  31. * Returns the current time.
  32. *
  33. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  34. *
  35. * @return time_t return timestamp current.
  36. *
  37. */
  38. #pragma module_name = "?time"
  39. #if _DLIB_TIME_ALLOW_64
  40. time_t __time64(time_t *t)
  41. #else
  42. time_t __time32(time_t *t)
  43. #endif
  44. {
  45. time_t time_now = 0;
  46. #ifdef RT_USING_RTC
  47. static rt_device_t device = RT_NULL;
  48. /* optimization: find rtc device only first. */
  49. if (device == RT_NULL)
  50. {
  51. device = rt_device_find("rtc");
  52. }
  53. /* read timestamp from RTC device. */
  54. if (device != RT_NULL)
  55. {
  56. if (rt_device_open(device, 0) == RT_EOK)
  57. {
  58. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  59. rt_device_close(device);
  60. }
  61. }
  62. #endif /* RT_USING_RTC */
  63. /* if t is not NULL, write timestamp to *t */
  64. if (t != RT_NULL)
  65. {
  66. *t = time_now;
  67. }
  68. return time_now;
  69. }
  70. RT_WEAK clock_t clock(void)
  71. {
  72. return rt_tick_get();
  73. }