time.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. /* Checking input validity */
  212. if (rt_strlen(days) <= (t->tm_wday << 2) || rt_strlen(months) <= (t->tm_mon << 2))
  213. {
  214. LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
  215. *(int*) buf = *(int*) days;
  216. *(int*) (buf + 4) = *(int*) months;
  217. num2str(buf + 8, t->tm_mday);
  218. if (buf[8] == '0')
  219. buf[8] = ' ';
  220. buf[10] = ' ';
  221. num2str(buf + 11, t->tm_hour);
  222. buf[13] = ':';
  223. num2str(buf + 14, t->tm_min);
  224. buf[16] = ':';
  225. num2str(buf + 17, t->tm_sec);
  226. buf[19] = ' ';
  227. num2str(buf + 20, 2000 / 100);
  228. num2str(buf + 22, 2000 % 100);
  229. buf[24] = '\n';
  230. buf[25] = '\0';
  231. return buf;
  232. }
  233. /* "Wed Jun 30 21:49:08 1993\n" */
  234. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  235. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  236. num2str(buf + 8, t->tm_mday);
  237. if (buf[8] == '0')
  238. buf[8] = ' ';
  239. buf[10] = ' ';
  240. num2str(buf + 11, t->tm_hour);
  241. buf[13] = ':';
  242. num2str(buf + 14, t->tm_min);
  243. buf[16] = ':';
  244. num2str(buf + 17, t->tm_sec);
  245. buf[19] = ' ';
  246. num2str(buf + 20, (t->tm_year + 1900) / 100);
  247. num2str(buf + 22, (t->tm_year + 1900) % 100);
  248. buf[24] = '\n';
  249. buf[25] = '\0';
  250. return buf;
  251. }
  252. RTM_EXPORT(asctime_r);
  253. char* asctime(const struct tm *timeptr)
  254. {
  255. static char buf[26];
  256. return asctime_r(timeptr, buf);
  257. }
  258. RTM_EXPORT(asctime);
  259. char *ctime_r(const time_t * tim_p, char * result)
  260. {
  261. struct tm tm;
  262. return asctime_r(localtime_r(tim_p, &tm), result);
  263. }
  264. RTM_EXPORT(ctime_r);
  265. char* ctime(const time_t *tim_p)
  266. {
  267. return asctime(localtime(tim_p));
  268. }
  269. RTM_EXPORT(ctime);
  270. /**
  271. * Returns the current time.
  272. *
  273. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  274. *
  275. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  276. * If timer is not a NULL pointer, the return value is also stored in timer.
  277. *
  278. */
  279. RT_WEAK time_t time(time_t *t)
  280. {
  281. struct timeval now;
  282. if(get_timeval(&now) == RT_EOK)
  283. {
  284. if (t)
  285. {
  286. *t = now.tv_sec;
  287. }
  288. return now.tv_sec;
  289. }
  290. else
  291. {
  292. rt_set_errno(EFAULT);
  293. return ((time_t)-1);
  294. }
  295. }
  296. RTM_EXPORT(time);
  297. RT_WEAK clock_t clock(void)
  298. {
  299. return rt_tick_get();
  300. }
  301. RTM_EXPORT(clock);
  302. int stime(const time_t *t)
  303. {
  304. struct timeval tv;
  305. if (!t)
  306. {
  307. rt_set_errno(EFAULT);
  308. return -1;
  309. }
  310. tv.tv_sec = *t;
  311. if (set_timeval(&tv) == RT_EOK)
  312. {
  313. return 0;
  314. }
  315. else
  316. {
  317. rt_set_errno(EFAULT);
  318. return -1;
  319. }
  320. }
  321. RTM_EXPORT(stime);
  322. time_t timegm(struct tm * const t)
  323. {
  324. register time_t day;
  325. register time_t i;
  326. register time_t years = t->tm_year - 70;
  327. if (t->tm_sec > 60)
  328. {
  329. t->tm_min += t->tm_sec / 60;
  330. t->tm_sec %= 60;
  331. }
  332. if (t->tm_min > 60)
  333. {
  334. t->tm_hour += t->tm_min / 60;
  335. t->tm_min %= 60;
  336. }
  337. if (t->tm_hour > 24)
  338. {
  339. t->tm_mday += t->tm_hour / 24;
  340. t->tm_hour %= 24;
  341. }
  342. if (t->tm_mon > 12)
  343. {
  344. t->tm_year += t->tm_mon / 12;
  345. t->tm_mon %= 12;
  346. }
  347. while (t->tm_mday > __spm[1 + t->tm_mon])
  348. {
  349. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  350. {
  351. --t->tm_mday;
  352. }
  353. t->tm_mday -= __spm[t->tm_mon];
  354. ++t->tm_mon;
  355. if (t->tm_mon > 11)
  356. {
  357. t->tm_mon = 0;
  358. ++t->tm_year;
  359. }
  360. }
  361. if (t->tm_year < 70)
  362. return (time_t) - 1;
  363. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  364. day = years * 365 + (years + 1) / 4;
  365. /* After 2100 we have to substract 3 leap years for every 400 years
  366. This is not intuitive. Most mktime implementations do not support
  367. dates after 2059, anyway, so we might leave this out for it's
  368. bloat. */
  369. if (years >= 131)
  370. {
  371. years -= 131;
  372. years /= 100;
  373. day -= (years >> 2) * 3 + 1;
  374. if ((years &= 3) == 3)
  375. years--;
  376. day -= years;
  377. }
  378. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  379. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  380. /* day is now the number of days since 'Jan 1 1970' */
  381. i = 7;
  382. t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
  383. i = 24;
  384. day *= i;
  385. i = 60;
  386. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  387. }
  388. RTM_EXPORT(timegm);
  389. int gettimeofday(struct timeval *tv, 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(tz != RT_NULL)
  397. {
  398. tz->tz_dsttime = DST_NONE;
  399. tz->tz_minuteswest = -(tz_get() * 60);
  400. }
  401. if (tv != RT_NULL && get_timeval(tv) == RT_EOK)
  402. {
  403. return 0;
  404. }
  405. else
  406. {
  407. rt_set_errno(EFAULT);
  408. return -1;
  409. }
  410. }
  411. RTM_EXPORT(gettimeofday);
  412. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  413. {
  414. /* The use of the timezone structure is obsolete;
  415. * the tz argument should normally be specified as NULL.
  416. * The tz_dsttime field has never been used under Linux.
  417. * Thus, the following is purely of historic interest.
  418. */
  419. if (tv != RT_NULL
  420. && tv->tv_usec >= 0
  421. && set_timeval((struct timeval *)tv) == RT_EOK)
  422. {
  423. return 0;
  424. }
  425. else
  426. {
  427. rt_set_errno(EINVAL);
  428. return -1;
  429. }
  430. }
  431. RTM_EXPORT(settimeofday);
  432. /* inherent in the toolchain */
  433. RTM_EXPORT(difftime);
  434. RTM_EXPORT(strftime);
  435. #ifdef RT_USING_POSIX
  436. #ifdef RT_USING_RTC
  437. static volatile struct timeval _timevalue;
  438. static int _rt_clock_time_system_init()
  439. {
  440. register rt_base_t level;
  441. time_t time = 0;
  442. rt_tick_t tick;
  443. rt_device_t device;
  444. device = rt_device_find("rtc");
  445. if (device != RT_NULL)
  446. {
  447. /* get realtime seconds */
  448. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
  449. {
  450. level = rt_hw_interrupt_disable();
  451. tick = rt_tick_get(); /* get tick */
  452. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  453. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  454. rt_hw_interrupt_enable(level);
  455. return 0;
  456. }
  457. }
  458. level = rt_hw_interrupt_disable();
  459. _timevalue.tv_usec = 0;
  460. _timevalue.tv_sec = 0;
  461. rt_hw_interrupt_enable(level);
  462. return -1;
  463. }
  464. INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
  465. #endif /* RT_USING_RTC */
  466. int clock_getres(clockid_t clockid, struct timespec *res)
  467. {
  468. #ifndef RT_USING_RTC
  469. LOG_W("Cannot find a RTC device to save time!");
  470. return -1;
  471. #else
  472. int ret = 0;
  473. if (res == RT_NULL)
  474. {
  475. rt_set_errno(EINVAL);
  476. return -1;
  477. }
  478. switch (clockid)
  479. {
  480. case CLOCK_REALTIME:
  481. res->tv_sec = 0;
  482. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  483. break;
  484. #ifdef RT_USING_CPUTIME
  485. case CLOCK_CPUTIME_ID:
  486. res->tv_sec = 0;
  487. res->tv_nsec = clock_cpu_getres();
  488. break;
  489. #endif
  490. default:
  491. ret = -1;
  492. rt_set_errno(EINVAL);
  493. break;
  494. }
  495. return ret;
  496. #endif /* RT_USING_RTC */
  497. }
  498. RTM_EXPORT(clock_getres);
  499. int clock_gettime(clockid_t clockid, struct timespec *tp)
  500. {
  501. #ifndef RT_USING_RTC
  502. LOG_W("Cannot find a RTC device to save time!");
  503. return -1;
  504. #else
  505. int ret = 0;
  506. if (tp == RT_NULL)
  507. {
  508. rt_set_errno(EINVAL);
  509. return -1;
  510. }
  511. switch (clockid)
  512. {
  513. case CLOCK_REALTIME:
  514. {
  515. int tick;
  516. register rt_base_t level;
  517. level = rt_hw_interrupt_disable();
  518. tick = rt_tick_get(); /* get tick */
  519. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  520. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  521. rt_hw_interrupt_enable(level);
  522. }
  523. break;
  524. #ifdef RT_USING_CPUTIME
  525. case CLOCK_CPUTIME_ID:
  526. {
  527. float unit = 0;
  528. long long cpu_tick;
  529. unit = clock_cpu_getres();
  530. cpu_tick = clock_cpu_gettime();
  531. tp->tv_sec = ((int)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  532. tp->tv_nsec = ((int)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  533. }
  534. break;
  535. #endif
  536. default:
  537. rt_set_errno(EINVAL);
  538. ret = -1;
  539. }
  540. return ret;
  541. #endif /* RT_USING_RTC */
  542. }
  543. RTM_EXPORT(clock_gettime);
  544. int clock_settime(clockid_t clockid, const struct timespec *tp)
  545. {
  546. #ifndef RT_USING_RTC
  547. LOG_W("Cannot find a RTC device to save time!");
  548. return -1;
  549. #else
  550. register rt_base_t level;
  551. int second;
  552. rt_tick_t tick;
  553. rt_device_t device;
  554. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  555. {
  556. rt_set_errno(EINVAL);
  557. return -1;
  558. }
  559. /* get second */
  560. second = tp->tv_sec;
  561. level = rt_hw_interrupt_disable();
  562. tick = rt_tick_get(); /* get tick */
  563. /* update timevalue */
  564. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  565. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  566. rt_hw_interrupt_enable(level);
  567. /* update for RTC device */
  568. device = rt_device_find("rtc");
  569. if (device != RT_NULL)
  570. {
  571. /* set realtime seconds */
  572. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
  573. {
  574. return 0;
  575. }
  576. }
  577. return -1;
  578. #endif /* RT_USING_RTC */
  579. }
  580. RTM_EXPORT(clock_settime);
  581. int rt_timespec_to_tick(const struct timespec *time)
  582. {
  583. int tick;
  584. int nsecond, second;
  585. struct timespec tp;
  586. RT_ASSERT(time != RT_NULL);
  587. tick = RT_WAITING_FOREVER;
  588. if (time != NULL)
  589. {
  590. /* get current tp */
  591. clock_gettime(CLOCK_REALTIME, &tp);
  592. if ((time->tv_nsec - tp.tv_nsec) < 0)
  593. {
  594. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  595. second = time->tv_sec - tp.tv_sec - 1;
  596. }
  597. else
  598. {
  599. nsecond = time->tv_nsec - tp.tv_nsec;
  600. second = time->tv_sec - tp.tv_sec;
  601. }
  602. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  603. if (tick < 0) tick = 0;
  604. }
  605. return tick;
  606. }
  607. RTM_EXPORT(rt_timespec_to_tick);
  608. #endif /* RT_USING_POSIX */
  609. /* timezone */
  610. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  611. #define RT_LIBC_DEFAULT_TIMEZONE 8
  612. #endif
  613. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  614. void tz_set(int8_t tz)
  615. {
  616. register rt_base_t level;
  617. level = rt_hw_interrupt_disable();
  618. _current_timezone = tz;
  619. rt_hw_interrupt_enable(level);
  620. }
  621. int8_t tz_get(void)
  622. {
  623. return _current_timezone;
  624. }
  625. int8_t tz_is_dst(void)
  626. {
  627. return 0;
  628. }