time.c 1.6 KB

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