time.c 15 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. * 2021-05-01 Meco Man support fixed timezone
  21. */
  22. #include <sys/time.h>
  23. #include <rtthread.h>
  24. #ifdef RT_USING_DEVICE
  25. #include <rtdevice.h>
  26. #endif
  27. #define DBG_TAG "TIME"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. #ifndef RT_LIBC_FIXED_TIMEZONE
  31. #define RT_LIBC_FIXED_TIMEZONE 8 /* UTC+8 */
  32. #endif
  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(const 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 = 0;
  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 + RT_LIBC_FIXED_TIMEZONE * 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 * RT_LIBC_FIXED_TIMEZONE;
  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 = -(RT_LIBC_FIXED_TIMEZONE * 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_sec >= 0
  399. && tv->tv_usec >= 0
  400. && set_timeval(tv) == RT_EOK)
  401. {
  402. return 0;
  403. }
  404. else
  405. {
  406. rt_set_errno(EINVAL);
  407. return -1;
  408. }
  409. }
  410. RTM_EXPORT(settimeofday);
  411. /* inherent in the toolchain */
  412. RTM_EXPORT(difftime);
  413. RTM_EXPORT(strftime);
  414. #ifdef RT_USING_POSIX
  415. static struct timeval _timevalue;
  416. static int clock_time_system_init()
  417. {
  418. time_t time;
  419. rt_tick_t tick;
  420. rt_device_t device;
  421. time = 0;
  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. /* get tick */
  429. tick = rt_tick_get();
  430. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  431. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  432. return 0;
  433. }
  434. INIT_COMPONENT_EXPORT(clock_time_system_init);
  435. int clock_getres(clockid_t clockid, struct timespec *res)
  436. {
  437. int ret = 0;
  438. if (res == RT_NULL)
  439. {
  440. rt_set_errno(EINVAL);
  441. return -1;
  442. }
  443. switch (clockid)
  444. {
  445. case CLOCK_REALTIME:
  446. res->tv_sec = 0;
  447. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  448. break;
  449. #ifdef RT_USING_CPUTIME
  450. case CLOCK_CPUTIME_ID:
  451. res->tv_sec = 0;
  452. res->tv_nsec = clock_cpu_getres();
  453. break;
  454. #endif
  455. default:
  456. ret = -1;
  457. rt_set_errno(EINVAL);
  458. break;
  459. }
  460. return ret;
  461. }
  462. RTM_EXPORT(clock_getres);
  463. int clock_gettime(clockid_t clockid, struct timespec *tp)
  464. {
  465. int ret = 0;
  466. if (tp == RT_NULL)
  467. {
  468. rt_set_errno(EINVAL);
  469. return -1;
  470. }
  471. switch (clockid)
  472. {
  473. case CLOCK_REALTIME:
  474. {
  475. /* get tick */
  476. int tick = rt_tick_get();
  477. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  478. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  479. }
  480. break;
  481. #ifdef RT_USING_CPUTIME
  482. case CLOCK_CPUTIME_ID:
  483. {
  484. float unit = 0;
  485. long long cpu_tick;
  486. unit = clock_cpu_getres();
  487. cpu_tick = clock_cpu_gettime();
  488. tp->tv_sec = ((int)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  489. tp->tv_nsec = ((int)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  490. }
  491. break;
  492. #endif
  493. default:
  494. rt_set_errno(EINVAL);
  495. ret = -1;
  496. }
  497. return ret;
  498. }
  499. RTM_EXPORT(clock_gettime);
  500. int clock_settime(clockid_t clockid, const struct timespec *tp)
  501. {
  502. int second;
  503. rt_tick_t tick;
  504. rt_device_t device;
  505. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  506. {
  507. rt_set_errno(EINVAL);
  508. return -1;
  509. }
  510. /* get second */
  511. second = tp->tv_sec;
  512. /* get tick */
  513. tick = rt_tick_get();
  514. /* update timevalue */
  515. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  516. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  517. /* update for RTC device */
  518. device = rt_device_find("rtc");
  519. if (device != RT_NULL)
  520. {
  521. /* set realtime seconds */
  522. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
  523. }
  524. else
  525. return -1;
  526. return 0;
  527. }
  528. RTM_EXPORT(clock_settime);
  529. int clock_time_to_tick(const struct timespec *time)
  530. {
  531. int tick;
  532. int nsecond, second;
  533. struct timespec tp;
  534. RT_ASSERT(time != RT_NULL);
  535. tick = RT_WAITING_FOREVER;
  536. if (time != NULL)
  537. {
  538. /* get current tp */
  539. clock_gettime(CLOCK_REALTIME, &tp);
  540. if ((time->tv_nsec - tp.tv_nsec) < 0)
  541. {
  542. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  543. second = time->tv_sec - tp.tv_sec - 1;
  544. }
  545. else
  546. {
  547. nsecond = time->tv_nsec - tp.tv_nsec;
  548. second = time->tv_sec - tp.tv_sec;
  549. }
  550. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  551. if (tick < 0) tick = 0;
  552. }
  553. return tick;
  554. }
  555. RTM_EXPORT(clock_time_to_tick);
  556. #endif /* RT_USING_POSIX */