time.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. * 2019-08-21 zhangjun copy from minilibc
  9. * 2020-09-07 Meco Man combine gcc armcc iccarm
  10. */
  11. #include <sys/time.h>
  12. #include <rtdevice.h>
  13. #include <rtthread.h>
  14. #if !defined (__IAR_SYSTEMS_ICC__)
  15. /* days per month -- nonleap! */
  16. const short __spm[13] =
  17. {
  18. 0,
  19. (31),
  20. (31 + 28),
  21. (31 + 28 + 31),
  22. (31 + 28 + 31 + 30),
  23. (31 + 28 + 31 + 30 + 31),
  24. (31 + 28 + 31 + 30 + 31 + 30),
  25. (31 + 28 + 31 + 30 + 31 + 30 + 31),
  26. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
  27. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
  28. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
  29. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
  30. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
  31. };
  32. static const char days[] = "Sun Mon Tue Wed Thu Fri Sat ";
  33. static const char months[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  34. /* seconds per day */
  35. #define SPD 24*60*60
  36. int __isleap(int year)
  37. {
  38. /* every fourth year is a leap year except for century years that are
  39. * not divisible by 400. */
  40. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  41. return (!(year % 4) && ((year % 100) || !(year % 400)));
  42. }
  43. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  44. {
  45. time_t i;
  46. register time_t work = *timep % (SPD);
  47. r->tm_sec = work % 60;
  48. work /= 60;
  49. r->tm_min = work % 60;
  50. r->tm_hour = work / 60;
  51. work = *timep / (SPD);
  52. r->tm_wday = (4 + work) % 7;
  53. for (i = 1970;; ++i)
  54. {
  55. register time_t k = __isleap(i) ? 366 : 365;
  56. if (work >= k)
  57. work -= k;
  58. else
  59. break;
  60. }
  61. r->tm_year = i - 1900;
  62. r->tm_yday = work;
  63. r->tm_mday = 1;
  64. if (__isleap(i) && (work > 58))
  65. {
  66. if (work == 59)
  67. r->tm_mday = 2; /* 29.2. */
  68. work -= 1;
  69. }
  70. for (i = 11; i && (__spm[i] > work); --i)
  71. ;
  72. r->tm_mon = i;
  73. r->tm_mday += work - __spm[i];
  74. return r;
  75. }
  76. struct tm* gmtime(const time_t* t)
  77. {
  78. static struct tm tmp;
  79. return gmtime_r(t, &tmp);
  80. }
  81. /*TODO timezone is not supprt now */
  82. struct tm* localtime_r(const time_t* t, struct tm* r)
  83. {
  84. return gmtime_r(t, r);
  85. }
  86. struct tm* localtime(const time_t* t)
  87. {
  88. static struct tm tmp;
  89. return localtime_r(t, &tmp);
  90. }
  91. time_t mktime(struct tm * const t)
  92. {
  93. register time_t day;
  94. register time_t i;
  95. register time_t years = t->tm_year - 70;
  96. if (t->tm_sec > 60)
  97. {
  98. t->tm_min += t->tm_sec / 60;
  99. t->tm_sec %= 60;
  100. }
  101. if (t->tm_min > 60)
  102. {
  103. t->tm_hour += t->tm_min / 60;
  104. t->tm_min %= 60;
  105. }
  106. if (t->tm_hour > 24)
  107. {
  108. t->tm_mday += t->tm_hour / 24;
  109. t->tm_hour %= 24;
  110. }
  111. if (t->tm_mon > 12)
  112. {
  113. t->tm_year += t->tm_mon / 12;
  114. t->tm_mon %= 12;
  115. }
  116. while (t->tm_mday > __spm[1 + t->tm_mon])
  117. {
  118. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  119. {
  120. --t->tm_mday;
  121. }
  122. t->tm_mday -= __spm[t->tm_mon];
  123. ++t->tm_mon;
  124. if (t->tm_mon > 11)
  125. {
  126. t->tm_mon = 0;
  127. ++t->tm_year;
  128. }
  129. }
  130. if (t->tm_year < 70)
  131. return (time_t) - 1;
  132. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  133. day = years * 365 + (years + 1) / 4;
  134. /* After 2100 we have to substract 3 leap years for every 400 years
  135. This is not intuitive. Most mktime implementations do not support
  136. dates after 2059, anyway, so we might leave this out for it's
  137. bloat. */
  138. if (years >= 131)
  139. {
  140. years -= 131;
  141. years /= 100;
  142. day -= (years >> 2) * 3 + 1;
  143. if ((years &= 3) == 3)
  144. years--;
  145. day -= years;
  146. }
  147. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  148. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  149. /* day is now the number of days since 'Jan 1 1970' */
  150. i = 7;
  151. t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
  152. i = 24;
  153. day *= i;
  154. i = 60;
  155. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  156. }
  157. static void num2str(char *c, int i)
  158. {
  159. c[0] = i / 10 + '0';
  160. c[1] = i % 10 + '0';
  161. }
  162. char* asctime_r(const struct tm *t, char *buf)
  163. {
  164. /* "Wed Jun 30 21:49:08 1993\n" */
  165. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  166. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  167. num2str(buf + 8, t->tm_mday);
  168. if (buf[8] == '0')
  169. buf[8] = ' ';
  170. buf[10] = ' ';
  171. num2str(buf + 11, t->tm_hour);
  172. buf[13] = ':';
  173. num2str(buf + 14, t->tm_min);
  174. buf[16] = ':';
  175. num2str(buf + 17, t->tm_sec);
  176. buf[19] = ' ';
  177. num2str(buf + 20, (t->tm_year + 1900) / 100);
  178. num2str(buf + 22, (t->tm_year + 1900) % 100);
  179. buf[24] = '\n';
  180. return buf;
  181. }
  182. char* asctime(const struct tm *timeptr)
  183. {
  184. static char buf[25];
  185. return asctime_r(timeptr, buf);
  186. }
  187. char* ctime(const time_t *timep)
  188. {
  189. return asctime(localtime(timep));
  190. }
  191. #endif /* __IAR_SYSTEMS_ICC__ */
  192. int gettimeofday(struct timeval *tp, void *ignore)
  193. {
  194. time_t time = 0;
  195. #ifdef RT_USING_DEVICE
  196. rt_device_t device;
  197. device = rt_device_find("rtc");
  198. RT_ASSERT(device != RT_NULL);
  199. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  200. if (tp != RT_NULL)
  201. {
  202. tp->tv_sec = time;
  203. tp->tv_usec = 0;
  204. }
  205. #else
  206. tv->tv_sec = 0;
  207. tv->tv_usec = 0;
  208. #endif
  209. return time;
  210. }
  211. /**
  212. * Returns the current time.
  213. *
  214. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  215. *
  216. * @return time_t return timestamp current.
  217. *
  218. */
  219. /* for IAR 6.2 later Compiler */
  220. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  221. #pragma module_name = "?time"
  222. #if _DLIB_TIME_USES_64
  223. time_t __time64(time_t *t)
  224. #else
  225. time_t __time32(time_t *t)
  226. #endif
  227. #else
  228. time_t time(time_t *t)
  229. #endif
  230. {
  231. time_t time_now = 0;
  232. #ifdef RT_USING_RTC
  233. static rt_device_t device = RT_NULL;
  234. /* optimization: find rtc device only first. */
  235. if (device == RT_NULL)
  236. {
  237. device = rt_device_find("rtc");
  238. }
  239. /* read timestamp from RTC device. */
  240. if (device != RT_NULL)
  241. {
  242. if (rt_device_open(device, 0) == RT_EOK)
  243. {
  244. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  245. rt_device_close(device);
  246. }
  247. }
  248. #endif /* RT_USING_RTC */
  249. /* if t is not NULL, write timestamp to *t */
  250. if (t != RT_NULL)
  251. {
  252. *t = time_now;
  253. }
  254. return time_now;
  255. }
  256. RT_WEAK clock_t clock(void)
  257. {
  258. return rt_tick_get();
  259. }