time.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_USES_64
  40. time_t __time64(time_t *t)
  41. #else
  42. /* for IAR 6.2 later Compiler */
  43. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  44. time_t __time32(time_t *t)
  45. #else
  46. time_t time(time_t *t)
  47. #endif
  48. #endif
  49. {
  50. time_t time_now = 0;
  51. #ifdef RT_USING_RTC
  52. static rt_device_t device = RT_NULL;
  53. /* optimization: find rtc device only first. */
  54. if (device == RT_NULL)
  55. {
  56. device = rt_device_find("rtc");
  57. }
  58. /* read timestamp from RTC device. */
  59. if (device != RT_NULL)
  60. {
  61. if (rt_device_open(device, 0) == RT_EOK)
  62. {
  63. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  64. rt_device_close(device);
  65. }
  66. }
  67. #endif /* RT_USING_RTC */
  68. /* if t is not NULL, write timestamp to *t */
  69. if (t != RT_NULL)
  70. {
  71. *t = time_now;
  72. }
  73. return time_now;
  74. }
  75. RT_WEAK clock_t clock(void)
  76. {
  77. return rt_tick_get();
  78. }