time.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Copyright (c) 2006-2021, 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. * 2021-02-12 Meco Man add errno
  16. * 2012-12-08 Bernard <clock_time.c> fix the issue of _timevalue.tv_usec initialization,
  17. * which found by Rob <rdent@iinet.net.au>
  18. * 2021-02-12 Meco Man move all of the functions located in <clock_time.c> to this file
  19. * 2021-03-15 Meco Man fixed a bug of leaking memory in asctime()
  20. * 2021-05-01 Meco Man support fixed timezone
  21. * 2021-07-21 Meco Man implement that change/set timezone APIs
  22. */
  23. #include "sys/time.h"
  24. #include <sys/errno.h>
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #ifdef RT_USING_DEVICE
  28. #include <rtdevice.h>
  29. #endif
  30. #define DBG_TAG "TIME"
  31. #define DBG_LVL DBG_INFO
  32. #include <rtdbg.h>
  33. /* seconds per day */
  34. #define SPD 24*60*60
  35. /* days per month -- nonleap! */
  36. static const short __spm[13] =
  37. {
  38. 0,
  39. (31),
  40. (31 + 28),
  41. (31 + 28 + 31),
  42. (31 + 28 + 31 + 30),
  43. (31 + 28 + 31 + 30 + 31),
  44. (31 + 28 + 31 + 30 + 31 + 30),
  45. (31 + 28 + 31 + 30 + 31 + 30 + 31),
  46. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
  47. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
  48. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
  49. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
  50. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
  51. };
  52. ALIGN(4) static const char days[] = "Sun Mon Tue Wed Thu Fri Sat ";
  53. ALIGN(4) static const char months[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  54. static int __isleap(int year)
  55. {
  56. /* every fourth year is a leap year except for century years that are
  57. * not divisible by 400. */
  58. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  59. return (!(year % 4) && ((year % 100) || !(year % 400)));
  60. }
  61. static void num2str(char *c, int i)
  62. {
  63. c[0] = i / 10 + '0';
  64. c[1] = i % 10 + '0';
  65. }
  66. /**
  67. * Get time from RTC device (without timezone, UTC+0)
  68. * @param tv: struct timeval
  69. * @return the operation status, RT_EOK on successful
  70. */
  71. static rt_err_t get_timeval(struct timeval *tv)
  72. {
  73. #ifdef RT_USING_RTC
  74. static rt_device_t device = RT_NULL;
  75. rt_err_t rst = -RT_ERROR;
  76. if (tv == RT_NULL)
  77. return -RT_EINVAL;
  78. /* default is 0 */
  79. tv->tv_sec = 0;
  80. tv->tv_usec = 0;
  81. /* optimization: find rtc device only first */
  82. if (device == RT_NULL)
  83. {
  84. device = rt_device_find("rtc");
  85. }
  86. /* read timestamp from RTC device */
  87. if (device != RT_NULL)
  88. {
  89. if (rt_device_open(device, 0) == RT_EOK)
  90. {
  91. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec);
  92. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec);
  93. rt_device_close(device);
  94. }
  95. }
  96. else
  97. {
  98. /* LOG_W will cause a recursive printing if ulog timestamp function is enabled */
  99. rt_kprintf("Cannot find a RTC device to provide time!\r\n");
  100. return -RT_ENOSYS;
  101. }
  102. return rst;
  103. #else
  104. /* LOG_W will cause a recursive printing if ulog timestamp function is enabled */
  105. rt_kprintf("Cannot find a RTC device to provide time!\r\n");
  106. return -RT_ENOSYS;
  107. #endif /* RT_USING_RTC */
  108. }
  109. /**
  110. * Set time to RTC device (without timezone)
  111. * @param tv: struct timeval
  112. * @return the operation status, RT_EOK on successful
  113. */
  114. static int set_timeval(struct timeval *tv)
  115. {
  116. #ifdef RT_USING_RTC
  117. static rt_device_t device = RT_NULL;
  118. rt_err_t rst = -RT_ERROR;
  119. if (tv == RT_NULL)
  120. return -RT_EINVAL;
  121. /* optimization: find rtc device only first */
  122. if (device == RT_NULL)
  123. {
  124. device = rt_device_find("rtc");
  125. }
  126. /* read timestamp from RTC device */
  127. if (device != RT_NULL)
  128. {
  129. if (rt_device_open(device, 0) == RT_EOK)
  130. {
  131. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec);
  132. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME_US, &tv->tv_usec);
  133. rt_device_close(device);
  134. }
  135. }
  136. else
  137. {
  138. LOG_W("Cannot find a RTC device to provide time!");
  139. return -RT_ENOSYS;
  140. }
  141. return rst;
  142. #else
  143. LOG_W("Cannot find a RTC device to provide time!");
  144. return -RT_ENOSYS;
  145. #endif /* RT_USING_RTC */
  146. }
  147. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  148. {
  149. time_t i;
  150. register time_t work = *timep % (SPD);
  151. r->tm_sec = work % 60;
  152. work /= 60;
  153. r->tm_min = work % 60;
  154. r->tm_hour = work / 60;
  155. work = *timep / (SPD);
  156. r->tm_wday = (4 + work) % 7;
  157. for (i = 1970;; ++i)
  158. {
  159. register time_t k = __isleap(i) ? 366 : 365;
  160. if (work >= k)
  161. work -= k;
  162. else
  163. break;
  164. }
  165. r->tm_year = i - 1900;
  166. r->tm_yday = work;
  167. r->tm_mday = 1;
  168. if (__isleap(i) && (work > 58))
  169. {
  170. if (work == 59)
  171. r->tm_mday = 2; /* 29.2. */
  172. work -= 1;
  173. }
  174. for (i = 11; i && (__spm[i] > work); --i)
  175. ;
  176. r->tm_mon = i;
  177. r->tm_mday += work - __spm[i];
  178. r->tm_isdst = tz_is_dst();
  179. return r;
  180. }
  181. RTM_EXPORT(gmtime_r);
  182. struct tm* gmtime(const time_t* t)
  183. {
  184. static struct tm tmp;
  185. return gmtime_r(t, &tmp);
  186. }
  187. RTM_EXPORT(gmtime);
  188. struct tm* localtime_r(const time_t* t, struct tm* r)
  189. {
  190. time_t local_tz;
  191. local_tz = *t + tz_get() * 3600;
  192. return gmtime_r(&local_tz, r);
  193. }
  194. RTM_EXPORT(localtime_r);
  195. struct tm* localtime(const time_t* t)
  196. {
  197. static struct tm tmp;
  198. return localtime_r(t, &tmp);
  199. }
  200. RTM_EXPORT(localtime);
  201. time_t mktime(struct tm * const t)
  202. {
  203. time_t timestamp;
  204. timestamp = timegm(t);
  205. timestamp = timestamp - 3600 * tz_get();
  206. return timestamp;
  207. }
  208. RTM_EXPORT(mktime);
  209. char* asctime_r(const struct tm *t, char *buf)
  210. {
  211. /* "Wed Jun 30 21:49:08 1993\n" */
  212. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  213. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  214. num2str(buf + 8, t->tm_mday);
  215. if (buf[8] == '0')
  216. buf[8] = ' ';
  217. buf[10] = ' ';
  218. num2str(buf + 11, t->tm_hour);
  219. buf[13] = ':';
  220. num2str(buf + 14, t->tm_min);
  221. buf[16] = ':';
  222. num2str(buf + 17, t->tm_sec);
  223. buf[19] = ' ';
  224. num2str(buf + 20, (t->tm_year + 1900) / 100);
  225. num2str(buf + 22, (t->tm_year + 1900) % 100);
  226. buf[24] = '\n';
  227. buf[25] = '\0';
  228. return buf;
  229. }
  230. RTM_EXPORT(asctime_r);
  231. char* asctime(const struct tm *timeptr)
  232. {
  233. static char buf[26];
  234. return asctime_r(timeptr, buf);
  235. }
  236. RTM_EXPORT(asctime);
  237. char *ctime_r(const time_t * tim_p, char * result)
  238. {
  239. struct tm tm;
  240. return asctime_r(localtime_r(tim_p, &tm), result);
  241. }
  242. RTM_EXPORT(ctime_r);
  243. char* ctime(const time_t *tim_p)
  244. {
  245. return asctime(localtime(tim_p));
  246. }
  247. RTM_EXPORT(ctime);
  248. /**
  249. * Returns the current time.
  250. *
  251. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  252. *
  253. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  254. * If timer is not a NULL pointer, the return value is also stored in timer.
  255. *
  256. */
  257. RT_WEAK time_t time(time_t *t)
  258. {
  259. struct timeval now;
  260. if(get_timeval(&now) == RT_EOK)
  261. {
  262. if (t)
  263. {
  264. *t = now.tv_sec;
  265. }
  266. return now.tv_sec;
  267. }
  268. else
  269. {
  270. rt_set_errno(EFAULT);
  271. return ((time_t)-1);
  272. }
  273. }
  274. RTM_EXPORT(time);
  275. RT_WEAK clock_t clock(void)
  276. {
  277. return rt_tick_get();
  278. }
  279. RTM_EXPORT(clock);
  280. int stime(const time_t *t)
  281. {
  282. struct timeval tv;
  283. if (!t)
  284. {
  285. rt_set_errno(EFAULT);
  286. return -1;
  287. }
  288. tv.tv_sec = *t;
  289. if (set_timeval(&tv) == RT_EOK)
  290. {
  291. return 0;
  292. }
  293. else
  294. {
  295. rt_set_errno(EFAULT);
  296. return -1;
  297. }
  298. }
  299. RTM_EXPORT(stime);
  300. time_t timegm(struct tm * const t)
  301. {
  302. register time_t day;
  303. register time_t i;
  304. register time_t years = t->tm_year - 70;
  305. if (t->tm_sec > 60)
  306. {
  307. t->tm_min += t->tm_sec / 60;
  308. t->tm_sec %= 60;
  309. }
  310. if (t->tm_min > 60)
  311. {
  312. t->tm_hour += t->tm_min / 60;
  313. t->tm_min %= 60;
  314. }
  315. if (t->tm_hour > 24)
  316. {
  317. t->tm_mday += t->tm_hour / 24;
  318. t->tm_hour %= 24;
  319. }
  320. if (t->tm_mon > 12)
  321. {
  322. t->tm_year += t->tm_mon / 12;
  323. t->tm_mon %= 12;
  324. }
  325. while (t->tm_mday > __spm[1 + t->tm_mon])
  326. {
  327. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  328. {
  329. --t->tm_mday;
  330. }
  331. t->tm_mday -= __spm[t->tm_mon];
  332. ++t->tm_mon;
  333. if (t->tm_mon > 11)
  334. {
  335. t->tm_mon = 0;
  336. ++t->tm_year;
  337. }
  338. }
  339. if (t->tm_year < 70)
  340. return (time_t) - 1;
  341. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  342. day = years * 365 + (years + 1) / 4;
  343. /* After 2100 we have to substract 3 leap years for every 400 years
  344. This is not intuitive. Most mktime implementations do not support
  345. dates after 2059, anyway, so we might leave this out for it's
  346. bloat. */
  347. if (years >= 131)
  348. {
  349. years -= 131;
  350. years /= 100;
  351. day -= (years >> 2) * 3 + 1;
  352. if ((years &= 3) == 3)
  353. years--;
  354. day -= years;
  355. }
  356. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  357. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  358. /* day is now the number of days since 'Jan 1 1970' */
  359. i = 7;
  360. t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
  361. i = 24;
  362. day *= i;
  363. i = 60;
  364. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  365. }
  366. RTM_EXPORT(timegm);
  367. int gettimeofday(struct timeval *tv, struct timezone *tz)
  368. {
  369. /* The use of the timezone structure is obsolete;
  370. * the tz argument should normally be specified as NULL.
  371. * The tz_dsttime field has never been used under Linux.
  372. * Thus, the following is purely of historic interest.
  373. */
  374. if(tz != RT_NULL)
  375. {
  376. tz->tz_dsttime = DST_NONE;
  377. tz->tz_minuteswest = -(tz_get() * 60);
  378. }
  379. if (tv != RT_NULL && get_timeval(tv) == RT_EOK)
  380. {
  381. return 0;
  382. }
  383. else
  384. {
  385. rt_set_errno(EFAULT);
  386. return -1;
  387. }
  388. }
  389. RTM_EXPORT(gettimeofday);
  390. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  391. {
  392. /* The use of the timezone structure is obsolete;
  393. * the tz argument should normally be specified as NULL.
  394. * The tz_dsttime field has never been used under Linux.
  395. * Thus, the following is purely of historic interest.
  396. */
  397. if (tv != RT_NULL
  398. && tv->tv_usec >= 0
  399. && set_timeval((struct timeval *)tv) == RT_EOK)
  400. {
  401. return 0;
  402. }
  403. else
  404. {
  405. rt_set_errno(EINVAL);
  406. return -1;
  407. }
  408. }
  409. RTM_EXPORT(settimeofday);
  410. /* inherent in the toolchain */
  411. RTM_EXPORT(difftime);
  412. RTM_EXPORT(strftime);
  413. #ifdef RT_USING_POSIX
  414. #ifdef RT_USING_RTC
  415. static volatile struct timeval _timevalue;
  416. static int _rt_clock_time_system_init()
  417. {
  418. register rt_base_t level;
  419. time_t time = 0;
  420. rt_tick_t tick;
  421. rt_device_t device;
  422. device = rt_device_find("rtc");
  423. if (device != RT_NULL)
  424. {
  425. /* get realtime seconds */
  426. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
  427. {
  428. level = rt_hw_interrupt_disable();
  429. tick = rt_tick_get(); /* get tick */
  430. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  431. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  432. rt_hw_interrupt_enable(level);
  433. return 0;
  434. }
  435. }
  436. level = rt_hw_interrupt_disable();
  437. _timevalue.tv_usec = 0;
  438. _timevalue.tv_sec = 0;
  439. rt_hw_interrupt_enable(level);
  440. return -1;
  441. }
  442. INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
  443. #endif /* RT_USING_RTC */
  444. int clock_getres(clockid_t clockid, struct timespec *res)
  445. {
  446. #ifndef RT_USING_RTC
  447. LOG_W("Cannot find a RTC device to save time!");
  448. return -1;
  449. #else
  450. int ret = 0;
  451. if (res == RT_NULL)
  452. {
  453. rt_set_errno(EINVAL);
  454. return -1;
  455. }
  456. switch (clockid)
  457. {
  458. case CLOCK_REALTIME:
  459. res->tv_sec = 0;
  460. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  461. break;
  462. #ifdef RT_USING_CPUTIME
  463. case CLOCK_CPUTIME_ID:
  464. res->tv_sec = 0;
  465. res->tv_nsec = clock_cpu_getres();
  466. break;
  467. #endif
  468. default:
  469. ret = -1;
  470. rt_set_errno(EINVAL);
  471. break;
  472. }
  473. return ret;
  474. #endif /* RT_USING_RTC */
  475. }
  476. RTM_EXPORT(clock_getres);
  477. int clock_gettime(clockid_t clockid, struct timespec *tp)
  478. {
  479. #ifndef RT_USING_RTC
  480. LOG_W("Cannot find a RTC device to save time!");
  481. return -1;
  482. #else
  483. int ret = 0;
  484. if (tp == RT_NULL)
  485. {
  486. rt_set_errno(EINVAL);
  487. return -1;
  488. }
  489. switch (clockid)
  490. {
  491. case CLOCK_REALTIME:
  492. {
  493. int tick;
  494. register rt_base_t level;
  495. level = rt_hw_interrupt_disable();
  496. tick = rt_tick_get(); /* get tick */
  497. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  498. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  499. rt_hw_interrupt_enable(level);
  500. }
  501. break;
  502. #ifdef RT_USING_CPUTIME
  503. case CLOCK_CPUTIME_ID:
  504. {
  505. float unit = 0;
  506. long long cpu_tick;
  507. unit = clock_cpu_getres();
  508. cpu_tick = clock_cpu_gettime();
  509. tp->tv_sec = ((int)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  510. tp->tv_nsec = ((int)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  511. }
  512. break;
  513. #endif
  514. default:
  515. rt_set_errno(EINVAL);
  516. ret = -1;
  517. }
  518. return ret;
  519. #endif /* RT_USING_RTC */
  520. }
  521. RTM_EXPORT(clock_gettime);
  522. int clock_settime(clockid_t clockid, const struct timespec *tp)
  523. {
  524. #ifndef RT_USING_RTC
  525. LOG_W("Cannot find a RTC device to save time!");
  526. return -1;
  527. #else
  528. register rt_base_t level;
  529. int second;
  530. rt_tick_t tick;
  531. rt_device_t device;
  532. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  533. {
  534. rt_set_errno(EINVAL);
  535. return -1;
  536. }
  537. /* get second */
  538. second = tp->tv_sec;
  539. level = rt_hw_interrupt_disable();
  540. tick = rt_tick_get(); /* get tick */
  541. /* update timevalue */
  542. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  543. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  544. rt_hw_interrupt_enable(level);
  545. /* update for RTC device */
  546. device = rt_device_find("rtc");
  547. if (device != RT_NULL)
  548. {
  549. /* set realtime seconds */
  550. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
  551. {
  552. return 0;
  553. }
  554. }
  555. return -1;
  556. #endif /* RT_USING_RTC */
  557. }
  558. RTM_EXPORT(clock_settime);
  559. int rt_timespec_to_tick(const struct timespec *time)
  560. {
  561. int tick;
  562. int nsecond, second;
  563. struct timespec tp;
  564. RT_ASSERT(time != RT_NULL);
  565. tick = RT_WAITING_FOREVER;
  566. if (time != NULL)
  567. {
  568. /* get current tp */
  569. clock_gettime(CLOCK_REALTIME, &tp);
  570. if ((time->tv_nsec - tp.tv_nsec) < 0)
  571. {
  572. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  573. second = time->tv_sec - tp.tv_sec - 1;
  574. }
  575. else
  576. {
  577. nsecond = time->tv_nsec - tp.tv_nsec;
  578. second = time->tv_sec - tp.tv_sec;
  579. }
  580. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  581. if (tick < 0) tick = 0;
  582. }
  583. return tick;
  584. }
  585. RTM_EXPORT(rt_timespec_to_tick);
  586. #endif /* RT_USING_POSIX */
  587. /* timezone */
  588. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  589. #define RT_LIBC_DEFAULT_TIMEZONE 8
  590. #endif
  591. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  592. void tz_set(int8_t tz)
  593. {
  594. register rt_base_t level;
  595. level = rt_hw_interrupt_disable();
  596. _current_timezone = tz;
  597. rt_hw_interrupt_enable(level);
  598. }
  599. int8_t tz_get(void)
  600. {
  601. return _current_timezone;
  602. }
  603. int8_t tz_is_dst(void)
  604. {
  605. return 0;
  606. }