time.c 15 KB

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