time.c 6.5 KB

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