time.c 14 KB

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