time.c 18 KB

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