1
0

time.c 12 KB

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