time.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <sys/time.h>
  2. #include <rtthread.h>
  3. #ifdef RT_USING_DEVICE
  4. int gettimeofday(struct timeval *tp, void *ignore)
  5. {
  6. time_t time;
  7. rt_device_t device;
  8. device = rt_device_find("rtc");
  9. if (device != RT_NULL)
  10. {
  11. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  12. if (tp != RT_NULL)
  13. {
  14. tp->tv_sec = time;
  15. tp->tv_usec = 0;
  16. }
  17. return time;
  18. }
  19. return 0;
  20. }
  21. #endif
  22. /**
  23. * Returns the current time.
  24. *
  25. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  26. *
  27. * @return time_t return timestamp current.
  28. *
  29. */
  30. #pragma module_name = "?time"
  31. #if _DLIB_TIME_ALLOW_64
  32. time_t __time64(time_t *t)
  33. #else
  34. time_t __time32(time_t *t)
  35. #endif
  36. {
  37. time_t time_now = 0;
  38. #ifdef RT_USING_RTC
  39. static rt_device_t device = RT_NULL;
  40. /* optimization: find rtc device only first. */
  41. if (device == RT_NULL)
  42. {
  43. device = rt_device_find("rtc");
  44. }
  45. /* read timestamp from RTC device. */
  46. if (device != RT_NULL)
  47. {
  48. if (rt_device_open(device, 0) == RT_EOK)
  49. {
  50. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  51. rt_device_close(device);
  52. }
  53. }
  54. #endif /* RT_USING_RTC */
  55. /* if t is not NULL, write timestamp to *t */
  56. if (t != RT_NULL)
  57. {
  58. *t = time_now;
  59. }
  60. return time_now;
  61. }
  62. RT_WEAK clock_t clock(void)
  63. {
  64. return rt_tick_get();
  65. }