time.c 7.3 KB

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