ctime.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * Copyright (c) 2006-2022, 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. #include <unistd.h>
  28. #ifdef RT_USING_POSIX_DELAY
  29. #include <delay.h>
  30. #endif
  31. #ifdef RT_USING_RTC
  32. #include <rtdevice.h>
  33. #endif
  34. #define DBG_TAG "time"
  35. #define DBG_LVL DBG_INFO
  36. #include <rtdbg.h>
  37. #define _WARNING_NO_RTC "Cannot find a RTC device!"
  38. /* seconds per day */
  39. #define SPD 24*60*60
  40. /* days per month -- nonleap! */
  41. static const short __spm[13] =
  42. {
  43. 0,
  44. (31),
  45. (31 + 28),
  46. (31 + 28 + 31),
  47. (31 + 28 + 31 + 30),
  48. (31 + 28 + 31 + 30 + 31),
  49. (31 + 28 + 31 + 30 + 31 + 30),
  50. (31 + 28 + 31 + 30 + 31 + 30 + 31),
  51. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
  52. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
  53. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
  54. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
  55. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
  56. };
  57. ALIGN(4) static const char *days = "Sun Mon Tue Wed Thu Fri Sat ";
  58. ALIGN(4) static const char *months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  59. static int __isleap(int year)
  60. {
  61. /* every fourth year is a leap year except for century years that are
  62. * not divisible by 400. */
  63. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  64. return (!(year % 4) && ((year % 100) || !(year % 400)));
  65. }
  66. static void num2str(char *c, int i)
  67. {
  68. c[0] = i / 10 + '0';
  69. c[1] = i % 10 + '0';
  70. }
  71. /**
  72. * Get time from RTC device (without timezone, UTC+0)
  73. * @param tv: struct timeval
  74. * @return the operation status, RT_EOK on successful
  75. */
  76. static rt_err_t get_timeval(struct timeval *tv)
  77. {
  78. #ifdef RT_USING_RTC
  79. static rt_device_t device = RT_NULL;
  80. rt_err_t rst = -RT_ERROR;
  81. if (tv == RT_NULL)
  82. return -RT_EINVAL;
  83. /* default is 0 */
  84. tv->tv_sec = 0;
  85. tv->tv_usec = 0;
  86. /* optimization: find rtc device only first */
  87. if (device == RT_NULL)
  88. {
  89. device = rt_device_find("rtc");
  90. }
  91. /* read timestamp from RTC device */
  92. if (device != RT_NULL)
  93. {
  94. if (rt_device_open(device, 0) == RT_EOK)
  95. {
  96. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec);
  97. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv);
  98. rt_device_close(device);
  99. }
  100. }
  101. else
  102. {
  103. LOG_W(_WARNING_NO_RTC);
  104. return -RT_ENOSYS;
  105. }
  106. return rst;
  107. #else
  108. LOG_W(_WARNING_NO_RTC);
  109. return -RT_ENOSYS;
  110. #endif /* RT_USING_RTC */
  111. }
  112. /**
  113. * Set time to RTC device (without timezone)
  114. * @param tv: struct timeval
  115. * @return the operation status, RT_EOK on successful
  116. */
  117. static int set_timeval(struct timeval *tv)
  118. {
  119. #ifdef RT_USING_RTC
  120. static rt_device_t device = RT_NULL;
  121. rt_err_t rst = -RT_ERROR;
  122. if (tv == RT_NULL)
  123. return -RT_EINVAL;
  124. /* optimization: find rtc device only first */
  125. if (device == RT_NULL)
  126. {
  127. device = rt_device_find("rtc");
  128. }
  129. /* read timestamp from RTC device */
  130. if (device != RT_NULL)
  131. {
  132. if (rt_device_open(device, 0) == RT_EOK)
  133. {
  134. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec);
  135. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIMEVAL, tv);
  136. rt_device_close(device);
  137. }
  138. }
  139. else
  140. {
  141. LOG_W(_WARNING_NO_RTC);
  142. return -RT_ENOSYS;
  143. }
  144. return rst;
  145. #else
  146. LOG_W(_WARNING_NO_RTC);
  147. return -RT_ENOSYS;
  148. #endif /* RT_USING_RTC */
  149. }
  150. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  151. {
  152. int i;
  153. int work = *timep % (SPD);
  154. if(timep == RT_NULL || r == RT_NULL)
  155. {
  156. rt_set_errno(EFAULT);
  157. return RT_NULL;
  158. }
  159. rt_memset(r, RT_NULL, sizeof(struct tm));
  160. r->tm_sec = work % 60;
  161. work /= 60;
  162. r->tm_min = work % 60;
  163. r->tm_hour = work / 60;
  164. work = (int)(*timep / (SPD));
  165. r->tm_wday = (4 + work) % 7;
  166. for (i = 1970;; ++i)
  167. {
  168. int k = __isleap(i) ? 366 : 365;
  169. if (work >= k)
  170. work -= k;
  171. else
  172. break;
  173. }
  174. r->tm_year = i - 1900;
  175. r->tm_yday = work;
  176. r->tm_mday = 1;
  177. if (__isleap(i) && (work > 58))
  178. {
  179. if (work == 59)
  180. r->tm_mday = 2; /* 29.2. */
  181. work -= 1;
  182. }
  183. for (i = 11; i && (__spm[i] > work); --i);
  184. r->tm_mon = i;
  185. r->tm_mday += work - __spm[i];
  186. r->tm_isdst = tz_is_dst();
  187. return r;
  188. }
  189. RTM_EXPORT(gmtime_r);
  190. struct tm* gmtime(const time_t* t)
  191. {
  192. static struct tm tmp;
  193. return gmtime_r(t, &tmp);
  194. }
  195. RTM_EXPORT(gmtime);
  196. struct tm* localtime_r(const time_t* t, struct tm* r)
  197. {
  198. time_t local_tz;
  199. local_tz = *t + (time_t)tz_get() * 3600;
  200. return gmtime_r(&local_tz, r);
  201. }
  202. RTM_EXPORT(localtime_r);
  203. struct tm* localtime(const time_t* t)
  204. {
  205. static struct tm tmp;
  206. return localtime_r(t, &tmp);
  207. }
  208. RTM_EXPORT(localtime);
  209. time_t mktime(struct tm * const t)
  210. {
  211. time_t timestamp;
  212. timestamp = timegm(t);
  213. timestamp = timestamp - 3600 * (time_t)tz_get();
  214. return timestamp;
  215. }
  216. RTM_EXPORT(mktime);
  217. char* asctime_r(const struct tm *t, char *buf)
  218. {
  219. if(t == RT_NULL || buf == RT_NULL)
  220. {
  221. rt_set_errno(EFAULT);
  222. return RT_NULL;
  223. }
  224. rt_memset(buf, RT_NULL, 26);
  225. /* Checking input validity */
  226. if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2))
  227. {
  228. LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
  229. *(int*) buf = *(int*) days;
  230. *(int*) (buf + 4) = *(int*) months;
  231. num2str(buf + 8, t->tm_mday);
  232. if (buf[8] == '0')
  233. buf[8] = ' ';
  234. buf[10] = ' ';
  235. num2str(buf + 11, t->tm_hour);
  236. buf[13] = ':';
  237. num2str(buf + 14, t->tm_min);
  238. buf[16] = ':';
  239. num2str(buf + 17, t->tm_sec);
  240. buf[19] = ' ';
  241. num2str(buf + 20, 2000 / 100);
  242. num2str(buf + 22, 2000 % 100);
  243. buf[24] = '\n';
  244. buf[25] = '\0';
  245. return buf;
  246. }
  247. /* "Wed Jun 30 21:49:08 1993\n" */
  248. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  249. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  250. num2str(buf + 8, t->tm_mday);
  251. if (buf[8] == '0')
  252. buf[8] = ' ';
  253. buf[10] = ' ';
  254. num2str(buf + 11, t->tm_hour);
  255. buf[13] = ':';
  256. num2str(buf + 14, t->tm_min);
  257. buf[16] = ':';
  258. num2str(buf + 17, t->tm_sec);
  259. buf[19] = ' ';
  260. num2str(buf + 20, (t->tm_year + 1900) / 100);
  261. num2str(buf + 22, (t->tm_year + 1900) % 100);
  262. buf[24] = '\n';
  263. buf[25] = '\0';
  264. return buf;
  265. }
  266. RTM_EXPORT(asctime_r);
  267. char* asctime(const struct tm *timeptr)
  268. {
  269. static char buf[26];
  270. return asctime_r(timeptr, buf);
  271. }
  272. RTM_EXPORT(asctime);
  273. char *ctime_r(const time_t * tim_p, char * result)
  274. {
  275. struct tm tm;
  276. return asctime_r(localtime_r(tim_p, &tm), result);
  277. }
  278. RTM_EXPORT(ctime_r);
  279. char* ctime(const time_t *tim_p)
  280. {
  281. return asctime(localtime(tim_p));
  282. }
  283. RTM_EXPORT(ctime);
  284. #ifndef __ICCARM__
  285. double difftime(time_t time1, time_t time2)
  286. {
  287. return (double)(time1 - time2);
  288. }
  289. #endif /* __ICCARM__ */
  290. RTM_EXPORT(difftime);
  291. RTM_EXPORT(strftime); /* inherent in the toolchain */
  292. /**
  293. * Returns the current time.
  294. *
  295. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  296. *
  297. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  298. * If timer is not a NULL pointer, the return value is also stored in timer.
  299. *
  300. */
  301. RT_WEAK time_t time(time_t *t)
  302. {
  303. struct timeval now;
  304. if(get_timeval(&now) == RT_EOK)
  305. {
  306. if (t)
  307. {
  308. *t = now.tv_sec;
  309. }
  310. return now.tv_sec;
  311. }
  312. else
  313. {
  314. rt_set_errno(EFAULT);
  315. return ((time_t)-1);
  316. }
  317. }
  318. RTM_EXPORT(time);
  319. RT_WEAK clock_t clock(void)
  320. {
  321. return rt_tick_get();
  322. }
  323. RTM_EXPORT(clock);
  324. int stime(const time_t *t)
  325. {
  326. struct timeval tv;
  327. if (t == RT_NULL)
  328. {
  329. rt_set_errno(EFAULT);
  330. return -1;
  331. }
  332. tv.tv_sec = *t;
  333. if (set_timeval(&tv) == RT_EOK)
  334. {
  335. return 0;
  336. }
  337. else
  338. {
  339. rt_set_errno(EFAULT);
  340. return -1;
  341. }
  342. }
  343. RTM_EXPORT(stime);
  344. time_t timegm(struct tm * const t)
  345. {
  346. time_t day;
  347. time_t i;
  348. time_t years;
  349. if(t == RT_NULL)
  350. {
  351. rt_set_errno(EFAULT);
  352. return (time_t)-1;
  353. }
  354. years = (time_t)t->tm_year - 70;
  355. if (t->tm_sec > 60) /* seconds after the minute - [0, 60] including leap second */
  356. {
  357. t->tm_min += t->tm_sec / 60;
  358. t->tm_sec %= 60;
  359. }
  360. if (t->tm_min >= 60) /* minutes after the hour - [0, 59] */
  361. {
  362. t->tm_hour += t->tm_min / 60;
  363. t->tm_min %= 60;
  364. }
  365. if (t->tm_hour >= 24) /* hours since midnight - [0, 23] */
  366. {
  367. t->tm_mday += t->tm_hour / 24;
  368. t->tm_hour %= 24;
  369. }
  370. if (t->tm_mon >= 12) /* months since January - [0, 11] */
  371. {
  372. t->tm_year += t->tm_mon / 12;
  373. t->tm_mon %= 12;
  374. }
  375. while (t->tm_mday > __spm[1 + t->tm_mon])
  376. {
  377. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  378. {
  379. --t->tm_mday;
  380. }
  381. t->tm_mday -= __spm[t->tm_mon];
  382. ++t->tm_mon;
  383. if (t->tm_mon > 11)
  384. {
  385. t->tm_mon = 0;
  386. ++t->tm_year;
  387. }
  388. }
  389. if (t->tm_year < 70)
  390. {
  391. rt_set_errno(EINVAL);
  392. return (time_t) -1;
  393. }
  394. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  395. day = years * 365 + (years + 1) / 4;
  396. /* After 2100 we have to substract 3 leap years for every 400 years
  397. This is not intuitive. Most mktime implementations do not support
  398. dates after 2059, anyway, so we might leave this out for it's
  399. bloat. */
  400. if (years >= 131)
  401. {
  402. years -= 131;
  403. years /= 100;
  404. day -= (years >> 2) * 3 + 1;
  405. if ((years &= 3) == 3)
  406. years--;
  407. day -= years;
  408. }
  409. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  410. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  411. /* day is now the number of days since 'Jan 1 1970' */
  412. i = 7;
  413. t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */
  414. i = 24;
  415. day *= i;
  416. i = 60;
  417. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  418. }
  419. RTM_EXPORT(timegm);
  420. int gettimeofday(struct timeval *tv, struct timezone *tz)
  421. {
  422. /* The use of the timezone structure is obsolete;
  423. * the tz argument should normally be specified as NULL.
  424. * The tz_dsttime field has never been used under Linux.
  425. * Thus, the following is purely of historic interest.
  426. */
  427. if(tz != RT_NULL)
  428. {
  429. tz->tz_dsttime = DST_NONE;
  430. tz->tz_minuteswest = -(tz_get() * 60);
  431. }
  432. if (tv != RT_NULL && get_timeval(tv) == RT_EOK)
  433. {
  434. return 0;
  435. }
  436. else
  437. {
  438. rt_set_errno(EINVAL);
  439. return -1;
  440. }
  441. }
  442. RTM_EXPORT(gettimeofday);
  443. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  444. {
  445. /* The use of the timezone structure is obsolete;
  446. * the tz argument should normally be specified as NULL.
  447. * The tz_dsttime field has never been used under Linux.
  448. * Thus, the following is purely of historic interest.
  449. */
  450. if (tv != RT_NULL
  451. && tv->tv_usec >= 0
  452. && set_timeval((struct timeval *)tv) == RT_EOK)
  453. {
  454. return 0;
  455. }
  456. else
  457. {
  458. rt_set_errno(EINVAL);
  459. return -1;
  460. }
  461. }
  462. RTM_EXPORT(settimeofday);
  463. #ifdef RT_USING_POSIX_DELAY
  464. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  465. {
  466. sleep(rqtp->tv_sec);
  467. ndelay(rqtp->tv_nsec);
  468. return 0;
  469. }
  470. RTM_EXPORT(nanosleep);
  471. #endif /* RT_USING_POSIX_DELAY */
  472. #ifdef RT_USING_POSIX_CLOCK
  473. #ifdef RT_USING_RTC
  474. static volatile struct timeval _timevalue;
  475. static int _rt_clock_time_system_init(void)
  476. {
  477. rt_base_t level;
  478. time_t time = 0;
  479. rt_tick_t tick;
  480. rt_device_t device;
  481. device = rt_device_find("rtc");
  482. if (device != RT_NULL)
  483. {
  484. /* get realtime seconds */
  485. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
  486. {
  487. level = rt_hw_interrupt_disable();
  488. tick = rt_tick_get(); /* get tick */
  489. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  490. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  491. rt_hw_interrupt_enable(level);
  492. return 0;
  493. }
  494. }
  495. level = rt_hw_interrupt_disable();
  496. _timevalue.tv_usec = 0;
  497. _timevalue.tv_sec = 0;
  498. rt_hw_interrupt_enable(level);
  499. return -1;
  500. }
  501. INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
  502. #endif /* RT_USING_RTC */
  503. int clock_getres(clockid_t clockid, struct timespec *res)
  504. {
  505. #ifndef RT_USING_RTC
  506. LOG_W(_WARNING_NO_RTC);
  507. return -1;
  508. #else
  509. int ret = 0;
  510. if (res == RT_NULL)
  511. {
  512. rt_set_errno(EFAULT);
  513. return -1;
  514. }
  515. switch (clockid)
  516. {
  517. case CLOCK_REALTIME:
  518. res->tv_sec = 0;
  519. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  520. break;
  521. #ifdef RT_USING_CPUTIME
  522. case CLOCK_CPUTIME_ID:
  523. res->tv_sec = 0;
  524. res->tv_nsec = clock_cpu_getres();
  525. break;
  526. #endif
  527. default:
  528. res->tv_sec = 0;
  529. res->tv_nsec = 0;
  530. ret = -1;
  531. rt_set_errno(EINVAL);
  532. break;
  533. }
  534. return ret;
  535. #endif /* RT_USING_RTC */
  536. }
  537. RTM_EXPORT(clock_getres);
  538. int clock_gettime(clockid_t clockid, struct timespec *tp)
  539. {
  540. #ifndef RT_USING_RTC
  541. LOG_W(_WARNING_NO_RTC);
  542. return -1;
  543. #else
  544. int ret = 0;
  545. if (tp == RT_NULL)
  546. {
  547. rt_set_errno(EFAULT);
  548. return -1;
  549. }
  550. switch (clockid)
  551. {
  552. case CLOCK_REALTIME:
  553. {
  554. int tick;
  555. rt_base_t level;
  556. level = rt_hw_interrupt_disable();
  557. tick = rt_tick_get(); /* get tick */
  558. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  559. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  560. rt_hw_interrupt_enable(level);
  561. }
  562. break;
  563. #ifdef RT_USING_CPUTIME
  564. case CLOCK_CPUTIME_ID:
  565. {
  566. float unit = 0;
  567. long long cpu_tick;
  568. unit = clock_cpu_getres();
  569. cpu_tick = clock_cpu_gettime();
  570. tp->tv_sec = ((long long)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  571. tp->tv_nsec = ((long long)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  572. }
  573. break;
  574. #endif
  575. default:
  576. tp->tv_sec = 0;
  577. tp->tv_nsec = 0;
  578. rt_set_errno(EINVAL);
  579. ret = -1;
  580. }
  581. return ret;
  582. #endif /* RT_USING_RTC */
  583. }
  584. RTM_EXPORT(clock_gettime);
  585. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
  586. {
  587. if ((clockid != CLOCK_REALTIME) || (rqtp == RT_NULL))
  588. {
  589. rt_set_errno(EINVAL);
  590. return -1;
  591. }
  592. return nanosleep(rqtp, rmtp);
  593. }
  594. int clock_settime(clockid_t clockid, const struct timespec *tp)
  595. {
  596. #ifndef RT_USING_RTC
  597. LOG_W(_WARNING_NO_RTC);
  598. return -1;
  599. #else
  600. rt_base_t level;
  601. int second;
  602. rt_tick_t tick;
  603. rt_device_t device;
  604. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  605. {
  606. rt_set_errno(EFAULT);
  607. return -1;
  608. }
  609. /* get second */
  610. second = tp->tv_sec;
  611. level = rt_hw_interrupt_disable();
  612. tick = rt_tick_get(); /* get tick */
  613. /* update timevalue */
  614. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  615. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  616. rt_hw_interrupt_enable(level);
  617. /* update for RTC device */
  618. device = rt_device_find("rtc");
  619. if (device != RT_NULL)
  620. {
  621. /* set realtime seconds */
  622. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
  623. {
  624. return 0;
  625. }
  626. }
  627. return -1;
  628. #endif /* RT_USING_RTC */
  629. }
  630. RTM_EXPORT(clock_settime);
  631. int rt_timespec_to_tick(const struct timespec *time)
  632. {
  633. int tick;
  634. int nsecond, second;
  635. struct timespec tp = {0};
  636. RT_ASSERT(time != RT_NULL);
  637. tick = RT_WAITING_FOREVER;
  638. if (time != NULL)
  639. {
  640. /* get current tp */
  641. clock_gettime(CLOCK_REALTIME, &tp);
  642. if ((time->tv_nsec - tp.tv_nsec) < 0)
  643. {
  644. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  645. second = time->tv_sec - tp.tv_sec - 1;
  646. }
  647. else
  648. {
  649. nsecond = time->tv_nsec - tp.tv_nsec;
  650. second = time->tv_sec - tp.tv_sec;
  651. }
  652. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  653. if (tick < 0) tick = 0;
  654. }
  655. return tick;
  656. }
  657. RTM_EXPORT(rt_timespec_to_tick);
  658. #endif /* RT_USING_POSIX_CLOCK */
  659. #ifdef RT_USING_POSIX_TIMER
  660. #define ACTIVE 1
  661. #define NOT_ACTIVE 0
  662. struct timer_obj
  663. {
  664. struct rt_timer timer;
  665. void (*sigev_notify_function)(union sigval val);
  666. union sigval val;
  667. struct timespec interval; /* Reload value */
  668. rt_uint32_t reload; /* Reload value in ms */
  669. rt_uint32_t status;
  670. };
  671. static void rtthread_timer_wrapper(void *timerobj)
  672. {
  673. struct timer_obj *timer;
  674. timer = (struct timer_obj *)timerobj;
  675. if (timer->reload == 0U)
  676. {
  677. timer->status = NOT_ACTIVE;
  678. }
  679. if(timer->sigev_notify_function != RT_NULL)
  680. {
  681. (timer->sigev_notify_function)(timer->val);
  682. }
  683. }
  684. /**
  685. * @brief Create a per-process timer.
  686. *
  687. * This API does not accept SIGEV_THREAD as valid signal event notification
  688. * type.
  689. *
  690. * See IEEE 1003.1
  691. */
  692. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
  693. {
  694. static int num = 0;
  695. struct timer_obj *timer;
  696. char timername[RT_NAME_MAX] = {0};
  697. if (clockid != CLOCK_MONOTONIC || evp == NULL ||
  698. (evp->sigev_notify != SIGEV_NONE &&
  699. evp->sigev_notify != SIGEV_SIGNAL))
  700. {
  701. rt_set_errno(EINVAL);
  702. return -RT_ERROR;
  703. }
  704. timer = rt_malloc(sizeof(struct timer_obj));
  705. if(timer == RT_NULL)
  706. {
  707. rt_set_errno(ENOMEM);
  708. return -RT_ENOMEM;
  709. }
  710. RT_ASSERT(evp->sigev_notify_function != RT_NULL);
  711. rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
  712. num %= 100;
  713. timer->sigev_notify_function = evp->sigev_notify_function;
  714. timer->val = evp->sigev_value;
  715. timer->interval.tv_sec = 0;
  716. timer->interval.tv_nsec = 0;
  717. timer->reload = 0U;
  718. timer->status = NOT_ACTIVE;
  719. if (evp->sigev_notify == SIGEV_NONE)
  720. {
  721. rt_timer_init(&timer->timer, timername, RT_NULL, RT_NULL, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  722. }
  723. else
  724. {
  725. rt_timer_init(&timer->timer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  726. }
  727. *timerid = (timer_t)timer;
  728. return RT_EOK;
  729. }
  730. RTM_EXPORT(timer_create);
  731. /**
  732. * @brief Delete a per-process timer.
  733. *
  734. * See IEEE 1003.1
  735. */
  736. int timer_delete(timer_t timerid)
  737. {
  738. struct timer_obj *timer = (struct timer_obj *)timerid;
  739. if (timer == RT_NULL)
  740. {
  741. rt_set_errno(EINVAL);
  742. return -RT_ERROR;
  743. }
  744. if (timer->status == ACTIVE)
  745. {
  746. timer->status = NOT_ACTIVE;
  747. rt_timer_stop(&timer->timer);
  748. }
  749. rt_free(timer);
  750. return RT_EOK;
  751. }
  752. RTM_EXPORT(timer_delete);
  753. /**
  754. *
  755. * Return the overrun count for the last timer expiration.
  756. * It is subefficient to create a new structure to get overrun count.
  757. **/
  758. int timer_getoverrun(timer_t timerid)
  759. {
  760. rt_set_errno(ENOSYS);
  761. return -RT_ERROR;
  762. }
  763. /**
  764. * @brief Get amount of time left for expiration on a per-process timer.
  765. *
  766. * See IEEE 1003.1
  767. */
  768. int timer_gettime(timer_t timerid, struct itimerspec *its)
  769. {
  770. struct timer_obj *timer = (struct timer_obj *)timerid;
  771. rt_tick_t remaining;
  772. rt_uint32_t seconds, nanoseconds;
  773. if (timer == NULL)
  774. {
  775. rt_set_errno(EINVAL);
  776. return -RT_ERROR;
  777. }
  778. if (its == NULL)
  779. {
  780. rt_set_errno(EFAULT);
  781. return -RT_ERROR;
  782. }
  783. if (timer->status == ACTIVE)
  784. {
  785. rt_tick_t remain_tick;
  786. rt_timer_control(&timer->timer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
  787. /* 'remain_tick' is minimum-unit in the RT-Thread' timer,
  788. * so the seconds, nanoseconds will be calculated by 'remain_tick'.
  789. */
  790. remaining = remain_tick - rt_tick_get();
  791. /* calculate 'second' */
  792. seconds = remaining / RT_TICK_PER_SECOND;
  793. /* calculate 'nanosecond'; To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  794. *
  795. * remain_tick millisecond remain_tick * MILLISECOND_PER_SECOND
  796. * ------------------------- = -------------------------- ---> millisecond = -------------------------------------------
  797. * RT_TICK_PER_SECOND MILLISECOND_PER_SECOND RT_TICK_PER_SECOND
  798. *
  799. * remain_tick * MILLISECOND_PER_SECOND remain_tick * MILLISECOND_PER_SECOND * MICROSECOND_PER_SECOND
  800. * millisecond = ---------------------------------------- ---> nanosecond = -------------------------------------------------------------------
  801. * RT_TICK_PER_SECOND RT_TICK_PER_SECOND
  802. *
  803. */
  804. nanoseconds = (((remaining % RT_TICK_PER_SECOND) * MILLISECOND_PER_SECOND) * MICROSECOND_PER_SECOND) / RT_TICK_PER_SECOND ;
  805. its->it_value.tv_sec = (rt_int32_t)seconds;
  806. its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
  807. }
  808. else
  809. {
  810. /* Timer is disarmed */
  811. its->it_value.tv_sec = 0;
  812. its->it_value.tv_nsec = 0;
  813. }
  814. /* The interval last set by timer_settime() */
  815. its->it_interval = timer->interval;
  816. return RT_EOK;
  817. }
  818. RTM_EXPORT(timer_gettime);
  819. /**
  820. * @brief Sets expiration time of per-process timer.
  821. *
  822. * See IEEE 1003.1
  823. */
  824. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  825. struct itimerspec *ovalue)
  826. {
  827. struct timer_obj *timer = (struct timer_obj *)timerid;
  828. if (timer == NULL ||
  829. value->it_interval.tv_nsec < 0 ||
  830. value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
  831. value->it_value.tv_nsec < 0 ||
  832. value->it_value.tv_nsec >= NANOSECOND_PER_SECOND)
  833. {
  834. rt_set_errno(EINVAL);
  835. return -RT_ERROR;
  836. }
  837. if (value == NULL || ovalue == NULL)
  838. {
  839. rt_set_errno(EFAULT);
  840. return -RT_ERROR;
  841. }
  842. /* Save time to expire and old reload value. */
  843. if (ovalue != NULL)
  844. {
  845. timer_gettime(timerid, ovalue);
  846. }
  847. /* Stop the timer if the value is 0 */
  848. if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
  849. {
  850. if (timer->status == ACTIVE)
  851. {
  852. rt_timer_stop(&timer->timer);
  853. }
  854. timer->status = NOT_ACTIVE;
  855. return RT_EOK;
  856. }
  857. /* calculate timer period(tick); To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  858. *
  859. * tick nanosecond nanosecond * RT_TICK_PER_SECOND
  860. * ------------------------- = -------------------------- ---> tick = -------------------------------------
  861. * RT_TICK_PER_SECOND NANOSECOND_PER_SECOND NANOSECOND_PER_SECOND
  862. *
  863. */
  864. timer->reload = (value->it_interval.tv_sec * RT_TICK_PER_SECOND) + (value->it_interval.tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  865. timer->interval.tv_sec = value->it_interval.tv_sec;
  866. timer->interval.tv_nsec = value->it_interval.tv_nsec;
  867. if (timer->status == ACTIVE)
  868. {
  869. rt_timer_stop(&timer->timer);
  870. }
  871. timer->status = ACTIVE;
  872. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, (void *)timer->reload);
  873. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  874. rt_timer_start(&timer->timer);
  875. return RT_EOK;
  876. }
  877. RTM_EXPORT(timer_settime);
  878. #endif /* RT_USING_POSIX_TIMER */
  879. /* timezone */
  880. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  881. #define RT_LIBC_DEFAULT_TIMEZONE 8
  882. #endif
  883. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  884. void tz_set(int8_t tz)
  885. {
  886. rt_base_t level;
  887. level = rt_hw_interrupt_disable();
  888. _current_timezone = tz;
  889. rt_hw_interrupt_enable(level);
  890. }
  891. int8_t tz_get(void)
  892. {
  893. return _current_timezone;
  894. }
  895. int8_t tz_is_dst(void)
  896. {
  897. return 0;
  898. }