time.c 1.7 KB

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