time.c 7.7 KB

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