1
0

time.c 7.9 KB

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