time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* UTC wall-clock support for ring-3 commands.
  2. *
  3. * The CMOS RTC is exposed through SYS_RTC_GET/SYS_RTC_SET. time_t is 64-bit
  4. * so the RTC's 1970-2069 range is representable (a signed 32-bit time_t would
  5. * stop at 2038). There is no timezone database: localtime == gmtime. */
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <ucmd_lib.h>
  11. static const char *const wday_abbr[7] = {
  12. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  13. };
  14. static const char *const wday_full[7] = {
  15. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  16. "Saturday"
  17. };
  18. static const char *const mon_abbr[12] = {
  19. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  20. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  21. };
  22. static const char *const mon_full[12] = {
  23. "January", "February", "March", "April", "May", "June",
  24. "July", "August", "September", "October", "November", "December"
  25. };
  26. static long long days_from_civil(int y, unsigned m, unsigned d)
  27. {
  28. long long era;
  29. unsigned yoe, doy, doe;
  30. y -= m <= 2;
  31. era = (y >= 0 ? y : y - 399) / 400;
  32. yoe = (unsigned)(y - era * 400);
  33. doy = (153u * (m + (m > 2 ? (unsigned)-3 : 9u)) + 2u) / 5u + d - 1u;
  34. doe = yoe * 365u + yoe / 4u - yoe / 100u + doy;
  35. return era * 146097LL + (long long)doe - 719468LL;
  36. }
  37. static void civil_from_days(long long z, int *year, unsigned *month,
  38. unsigned *day)
  39. {
  40. long long era;
  41. unsigned doe, yoe, doy, mp;
  42. z += 719468LL;
  43. era = (z >= 0 ? z : z - 146096LL) / 146097LL;
  44. doe = (unsigned)(z - era * 146097LL);
  45. yoe = (doe - doe / 1460u + doe / 36524u - doe / 146096u) / 365u;
  46. *month = 0;
  47. *day = 0;
  48. {
  49. int y = (int)yoe + (int)era * 400;
  50. doy = doe - (365u * yoe + yoe / 4u - yoe / 100u);
  51. mp = (5u * doy + 2u) / 153u;
  52. *day = doy - (153u * mp + 2u) / 5u + 1u;
  53. *month = mp + (mp < 10u ? 3u : (unsigned)-9);
  54. y += (*month <= 2u);
  55. *year = y;
  56. }
  57. }
  58. static int epoch_from_tm(const struct tm *tm, time_t *out)
  59. {
  60. long long days;
  61. long long sec;
  62. if (!tm || tm->tm_mon < 0 || tm->tm_mon > 11 ||
  63. tm->tm_mday < 1 || tm->tm_mday > 31 ||
  64. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  65. tm->tm_min < 0 || tm->tm_min > 59 ||
  66. tm->tm_sec < 0 || tm->tm_sec > 59)
  67. return -1;
  68. if (tm->tm_year < -1900 || tm->tm_year > 8099)
  69. return -1;
  70. days = days_from_civil(tm->tm_year + 1900,
  71. (unsigned)(tm->tm_mon + 1),
  72. (unsigned)tm->tm_mday);
  73. sec = days * 86400LL + tm->tm_hour * 3600LL +
  74. tm->tm_min * 60LL + tm->tm_sec;
  75. *out = (time_t)sec;
  76. return 0;
  77. }
  78. static int rtc_now(struct tm *tm)
  79. {
  80. struct sys_datetime dt;
  81. if (u_rtc_get(&dt) != 0) {
  82. errno = EIO;
  83. return -1;
  84. }
  85. if (dt.year < 1970 || dt.year > 2069 ||
  86. dt.month < 1 || dt.month > 12 ||
  87. dt.day < 1 || dt.day > 31 ||
  88. dt.hour > 23 || dt.minute > 59 || dt.second > 59) {
  89. errno = EINVAL;
  90. return -1;
  91. }
  92. tm->tm_sec = (int)dt.second;
  93. tm->tm_min = (int)dt.minute;
  94. tm->tm_hour = (int)dt.hour;
  95. tm->tm_mday = (int)dt.day;
  96. tm->tm_mon = (int)dt.month - 1;
  97. tm->tm_year = (int)dt.year - 1900;
  98. tm->tm_wday = (int)(dt.wday % 7u);
  99. tm->tm_yday = (int)(days_from_civil((int)dt.year, (unsigned)dt.month,
  100. (unsigned)dt.day) -
  101. days_from_civil((int)dt.year, 1u, 1u));
  102. tm->tm_isdst = 0;
  103. return 0;
  104. }
  105. time_t time(time_t *tloc)
  106. {
  107. struct tm tm;
  108. time_t t;
  109. if (rtc_now(&tm) != 0)
  110. return (time_t)-1;
  111. if (epoch_from_tm(&tm, &t) != 0) {
  112. errno = EINVAL;
  113. return (time_t)-1;
  114. }
  115. if (tloc)
  116. *tloc = t;
  117. return t;
  118. }
  119. struct tm *gmtime(const time_t *timer)
  120. {
  121. static struct tm tm;
  122. long long sec;
  123. long long days;
  124. unsigned sod;
  125. int y;
  126. unsigned m, d;
  127. if (!timer) {
  128. errno = EINVAL;
  129. return 0;
  130. }
  131. sec = (long long)*timer;
  132. if (sec >= 0) {
  133. days = sec / 86400LL;
  134. sod = (unsigned)(sec % 86400LL);
  135. } else {
  136. long long q = (-sec + 86399LL) / 86400LL;
  137. days = -q;
  138. sod = (unsigned)(sec - days * 86400LL);
  139. }
  140. civil_from_days(days, &y, &m, &d);
  141. tm.tm_year = y - 1900;
  142. tm.tm_mon = (int)m - 1;
  143. tm.tm_mday = (int)d;
  144. tm.tm_hour = (int)(sod / 3600u);
  145. tm.tm_min = (int)((sod / 60u) % 60u);
  146. tm.tm_sec = (int)(sod % 60u);
  147. tm.tm_wday = (int)((days + 4) % 7);
  148. if (tm.tm_wday < 0)
  149. tm.tm_wday += 7;
  150. tm.tm_yday = (int)(days - days_from_civil(y, 1u, 1u));
  151. tm.tm_isdst = 0;
  152. return &tm;
  153. }
  154. struct tm *localtime(const time_t *timer)
  155. {
  156. /* No timezone database: the RTC is UTC. */
  157. return gmtime(timer);
  158. }
  159. time_t mktime(struct tm *tm)
  160. {
  161. time_t t;
  162. if (epoch_from_tm(tm, &t) != 0) {
  163. errno = EINVAL;
  164. return (time_t)-1;
  165. }
  166. return t;
  167. }
  168. static int out_ch(char **pp, char *end, char c)
  169. {
  170. if (*pp + 1 >= end)
  171. return -1;
  172. *(*pp)++ = c;
  173. return 0;
  174. }
  175. static int out_dec(char **pp, char *end, unsigned int v, int width)
  176. {
  177. char tmp[12];
  178. int n = 0;
  179. int i;
  180. do {
  181. tmp[n++] = (char)('0' + v % 10u);
  182. v /= 10u;
  183. } while (v);
  184. while (n < width) {
  185. if (out_ch(pp, end, '0'))
  186. return -1;
  187. width--;
  188. }
  189. for (i = n - 1; i >= 0; i--) {
  190. if (out_ch(pp, end, tmp[i]))
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. static int out_str(char **pp, char *end, const char *s)
  196. {
  197. while (*s) {
  198. if (out_ch(pp, end, *s++))
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
  204. {
  205. char *p = s;
  206. char *end = s + max;
  207. if (!s || !format || !tm || max == 0) {
  208. errno = EINVAL;
  209. return 0;
  210. }
  211. for (; *format; format++) {
  212. int c;
  213. if (*format != '%') {
  214. if (out_ch(&p, end, *format))
  215. return 0;
  216. continue;
  217. }
  218. format++;
  219. if (!*format) {
  220. errno = EINVAL;
  221. return 0;
  222. }
  223. c = *format;
  224. switch (c) {
  225. case 'Y':
  226. if (out_dec(&p, end, (unsigned)(tm->tm_year + 1900), 4))
  227. return 0;
  228. break;
  229. case 'y':
  230. if (out_dec(&p, end, (unsigned)(tm->tm_year % 100), 2))
  231. return 0;
  232. break;
  233. case 'm':
  234. if (out_dec(&p, end, (unsigned)(tm->tm_mon + 1), 2))
  235. return 0;
  236. break;
  237. case 'd':
  238. if (out_dec(&p, end, (unsigned)tm->tm_mday, 2))
  239. return 0;
  240. break;
  241. case 'e':
  242. if (out_dec(&p, end, (unsigned)tm->tm_mday, 2))
  243. return 0;
  244. if (p - 2 >= s)
  245. *(p - 2) = ' ';
  246. break;
  247. case 'H':
  248. if (out_dec(&p, end, (unsigned)tm->tm_hour, 2))
  249. return 0;
  250. break;
  251. case 'M':
  252. if (out_dec(&p, end, (unsigned)tm->tm_min, 2))
  253. return 0;
  254. break;
  255. case 'S':
  256. if (out_dec(&p, end, (unsigned)tm->tm_sec, 2))
  257. return 0;
  258. break;
  259. case 'j':
  260. if (out_dec(&p, end, (unsigned)(tm->tm_yday + 1), 3))
  261. return 0;
  262. break;
  263. case 'w':
  264. if (out_ch(&p, end, (char)('0' + (tm->tm_wday % 7))))
  265. return 0;
  266. break;
  267. case 'u':
  268. if (out_ch(&p, end, (char)('0' + (tm->tm_wday ? tm->tm_wday : 7))))
  269. return 0;
  270. break;
  271. case 'a':
  272. if (out_str(&p, end, wday_abbr[(unsigned)(tm->tm_wday % 7)]))
  273. return 0;
  274. break;
  275. case 'A':
  276. if (out_str(&p, end, wday_full[(unsigned)(tm->tm_wday % 7)]))
  277. return 0;
  278. break;
  279. case 'b':
  280. if (out_str(&p, end, mon_abbr[(unsigned)(tm->tm_mon % 12)]))
  281. return 0;
  282. break;
  283. case 'B':
  284. if (out_str(&p, end, mon_full[(unsigned)(tm->tm_mon % 12)]))
  285. return 0;
  286. break;
  287. case 'p':
  288. if (out_str(&p, end, tm->tm_hour < 12 ? "AM" : "PM"))
  289. return 0;
  290. break;
  291. case 'F':
  292. if (out_dec(&p, end, (unsigned)(tm->tm_year + 1900), 4) ||
  293. out_ch(&p, end, '-') ||
  294. out_dec(&p, end, (unsigned)(tm->tm_mon + 1), 2) ||
  295. out_ch(&p, end, '-') ||
  296. out_dec(&p, end, (unsigned)tm->tm_mday, 2))
  297. return 0;
  298. break;
  299. case 'T':
  300. if (out_dec(&p, end, (unsigned)tm->tm_hour, 2) ||
  301. out_ch(&p, end, ':') ||
  302. out_dec(&p, end, (unsigned)tm->tm_min, 2) ||
  303. out_ch(&p, end, ':') ||
  304. out_dec(&p, end, (unsigned)tm->tm_sec, 2))
  305. return 0;
  306. break;
  307. case 's': {
  308. time_t t;
  309. char tmp[32];
  310. char *q = tmp + sizeof(tmp);
  311. long long v;
  312. unsigned long long u;
  313. int neg;
  314. if (epoch_from_tm(tm, &t) != 0)
  315. return 0;
  316. v = (long long)t;
  317. neg = v < 0;
  318. u = neg ? (unsigned long long)(-v) : (unsigned long long)v;
  319. *--q = '\0';
  320. do {
  321. *--q = (char)('0' + u % 10u);
  322. u /= 10u;
  323. } while (u);
  324. if (neg)
  325. *--q = '-';
  326. if (out_str(&p, end, q))
  327. return 0;
  328. break;
  329. }
  330. case 'z':
  331. case 'Z':
  332. /* UTC / no timezone database. */
  333. break;
  334. case '%':
  335. if (out_ch(&p, end, '%'))
  336. return 0;
  337. break;
  338. default:
  339. if (out_ch(&p, end, '%') || out_ch(&p, end, c))
  340. return 0;
  341. break;
  342. }
  343. }
  344. if (p >= end)
  345. return 0;
  346. *p = '\0';
  347. return (size_t)(p - s);
  348. }
  349. char *asctime(const struct tm *tm)
  350. {
  351. static char buf[32];
  352. char *p = buf;
  353. if (!tm) {
  354. errno = EINVAL;
  355. return 0;
  356. }
  357. for (const char *q = wday_abbr[tm->tm_wday % 7]; *q; q++) *p++ = *q;
  358. *p++ = ' ';
  359. for (const char *q = mon_abbr[tm->tm_mon % 12]; *q; q++) *p++ = *q;
  360. *p++ = ' ';
  361. *p++ = (char)('0' + (tm->tm_mday / 10) % 10);
  362. *p++ = (char)('0' + tm->tm_mday % 10);
  363. *p++ = ' ';
  364. *p++ = (char)('0' + (tm->tm_hour / 10) % 10);
  365. *p++ = (char)('0' + tm->tm_hour % 10);
  366. *p++ = ':';
  367. *p++ = (char)('0' + (tm->tm_min / 10) % 10);
  368. *p++ = (char)('0' + tm->tm_min % 10);
  369. *p++ = ':';
  370. *p++ = (char)('0' + (tm->tm_sec / 10) % 10);
  371. *p++ = (char)('0' + tm->tm_sec % 10);
  372. *p++ = ' ';
  373. {
  374. int y = tm->tm_year + 1900;
  375. *p++ = (char)('0' + (y / 1000) % 10);
  376. *p++ = (char)('0' + (y / 100) % 10);
  377. *p++ = (char)('0' + (y / 10) % 10);
  378. *p++ = (char)('0' + y % 10);
  379. }
  380. *p++ = '\n';
  381. *p = '\0';
  382. return buf;
  383. }
  384. char *ctime(const time_t *timer)
  385. {
  386. return asctime(localtime(timer));
  387. }
  388. double difftime(time_t time1, time_t time0)
  389. {
  390. return (double)time1 - (double)time0;
  391. }
  392. int gettimeofday(struct timeval *tv, void *tz)
  393. {
  394. time_t t;
  395. (void)tz;
  396. if (!tv) {
  397. errno = EINVAL;
  398. return -1;
  399. }
  400. t = time(0);
  401. if (t == (time_t)-1)
  402. return -1;
  403. tv->tv_sec = t;
  404. tv->tv_usec = (suseconds_t)((u_ticks_ms() % 1000u) * 1000u);
  405. return 0;
  406. }
  407. int settimeofday(const struct timeval *tv, const void *tz)
  408. {
  409. struct tm tm;
  410. struct sys_datetime dt;
  411. time_t t;
  412. (void)tz;
  413. if (!tv) {
  414. errno = EINVAL;
  415. return -1;
  416. }
  417. t = tv->tv_sec;
  418. {
  419. struct tm *p = gmtime(&t);
  420. if (!p) {
  421. errno = EINVAL;
  422. return -1;
  423. }
  424. tm = *p;
  425. }
  426. if (tm.tm_year + 1900 < 1970 || tm.tm_year + 1900 > 2069) {
  427. errno = EINVAL;
  428. return -1;
  429. }
  430. dt.year = (unsigned)(tm.tm_year + 1900);
  431. dt.month = (unsigned)(tm.tm_mon + 1);
  432. dt.day = (unsigned)tm.tm_mday;
  433. dt.hour = (unsigned)tm.tm_hour;
  434. dt.minute = (unsigned)tm.tm_min;
  435. dt.second = (unsigned)tm.tm_sec;
  436. dt.wday = (unsigned)(tm.tm_wday % 7);
  437. if (u_rtc_set(&dt) != 0) {
  438. errno = EINVAL;
  439. return -1;
  440. }
  441. return 0;
  442. }
  443. int clock_gettime(int clock_id, struct timespec *tp)
  444. {
  445. if (!tp) {
  446. errno = EINVAL;
  447. return -1;
  448. }
  449. if (clock_id == CLOCK_REALTIME) {
  450. struct timeval tv;
  451. if (gettimeofday(&tv, 0) != 0)
  452. return -1;
  453. tp->tv_sec = tv.tv_sec;
  454. tp->tv_nsec = (long)tv.tv_usec * 1000L;
  455. return 0;
  456. }
  457. if (clock_id == CLOCK_MONOTONIC) {
  458. unsigned int ms = u_ticks_ms();
  459. tp->tv_sec = (time_t)(ms / 1000u);
  460. tp->tv_nsec = (long)(ms % 1000u) * 1000000L;
  461. return 0;
  462. }
  463. errno = EINVAL;
  464. return -1;
  465. }
  466. int clock_settime(int clock_id, const struct timespec *tp)
  467. {
  468. if (clock_id != CLOCK_REALTIME || !tp) {
  469. errno = EINVAL;
  470. return -1;
  471. }
  472. {
  473. struct timeval tv;
  474. tv.tv_sec = tp->tv_sec;
  475. tv.tv_usec = tp->tv_nsec / 1000L;
  476. return settimeofday(&tv, 0);
  477. }
  478. }
  479. int nanosleep(const struct timespec *req, struct timespec *rem)
  480. {
  481. unsigned long ms;
  482. if (!req || req->tv_sec < 0 || req->tv_nsec < 0 ||
  483. req->tv_nsec >= 1000000000L) {
  484. errno = EINVAL;
  485. return -1;
  486. }
  487. ms = (unsigned long)req->tv_sec * 1000UL +
  488. (unsigned long)(req->tv_nsec + 999999L) / 1000000L;
  489. if (ms > 0xFFFFFFFFu)
  490. ms = 0xFFFFFFFFu;
  491. u_sleep_ms((unsigned int)ms);
  492. if (rem) {
  493. rem->tv_sec = 0;
  494. rem->tv_nsec = 0;
  495. }
  496. return 0;
  497. }