time.c 1.6 KB

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