time.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Time.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/time.h>
  34. #include <sys/times.h>
  35. #include <syscall.h>
  36. #include <time.h>
  37. #include <utime.h>
  38. extern "C" {
  39. time_t time(time_t* tloc)
  40. {
  41. struct timeval tv;
  42. struct timezone tz;
  43. if (gettimeofday(&tv, &tz) < 0)
  44. return (time_t)-1;
  45. if (tloc)
  46. *tloc = tv.tv_sec;
  47. return tv.tv_sec;
  48. }
  49. int adjtime(const struct timeval* delta, struct timeval* old_delta)
  50. {
  51. int rc = syscall(SC_adjtime, delta, old_delta);
  52. __RETURN_WITH_ERRNO(rc, rc, -1);
  53. }
  54. int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  55. {
  56. int rc = syscall(SC_gettimeofday, tv);
  57. __RETURN_WITH_ERRNO(rc, rc, -1);
  58. }
  59. int settimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  60. {
  61. timespec ts;
  62. TIMEVAL_TO_TIMESPEC(tv, &ts);
  63. return clock_settime(CLOCK_REALTIME, &ts);
  64. }
  65. int utimes(const char* pathname, const struct timeval times[2])
  66. {
  67. if (!times) {
  68. return utime(pathname, nullptr);
  69. }
  70. // FIXME: implement support for tv_usec in the utime (or a new) syscall
  71. utimbuf buf = { times[0].tv_sec, times[1].tv_sec };
  72. return utime(pathname, &buf);
  73. }
  74. char* ctime(const time_t* t)
  75. {
  76. return asctime(localtime(t));
  77. }
  78. static const int __seconds_per_day = 60 * 60 * 24;
  79. static void time_to_tm(struct tm* tm, time_t t)
  80. {
  81. int year = 1970;
  82. for (; t >= days_in_year(year) * __seconds_per_day; ++year)
  83. t -= days_in_year(year) * __seconds_per_day;
  84. for (; t < 0; --year)
  85. t += days_in_year(year - 1) * __seconds_per_day;
  86. tm->tm_year = year - 1900;
  87. VERIFY(t >= 0);
  88. int days = t / __seconds_per_day;
  89. tm->tm_yday = days;
  90. int remaining = t % __seconds_per_day;
  91. tm->tm_sec = remaining % 60;
  92. remaining /= 60;
  93. tm->tm_min = remaining % 60;
  94. tm->tm_hour = remaining / 60;
  95. int month;
  96. for (month = 1; month < 12 && days >= days_in_month(year, month); ++month)
  97. days -= days_in_month(year, month);
  98. tm->tm_mday = days + 1;
  99. tm->tm_wday = day_of_week(year, month, tm->tm_mday);
  100. tm->tm_mon = month - 1;
  101. }
  102. static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)
  103. {
  104. // "The original values of the tm_wday and tm_yday components of the structure are ignored,
  105. // and the original values of the other components are not restricted to the ranges described in <time.h>.
  106. // [...]
  107. // Upon successful completion, the values of the tm_wday and tm_yday components of the structure shall be set appropriately,
  108. // and the other components are set to represent the specified time since the Epoch,
  109. // but with their values forced to the ranges indicated in the <time.h> entry;
  110. // the final value of tm_mday shall not be set until tm_mon and tm_year are determined."
  111. // FIXME: Handle tm_isdst eventually.
  112. tm->tm_year += tm->tm_mon / 12;
  113. tm->tm_mon %= 12;
  114. if (tm->tm_mon < 0) {
  115. tm->tm_year--;
  116. tm->tm_mon += 12;
  117. }
  118. tm->tm_yday = day_of_year(1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
  119. time_t days_since_epoch = years_to_days_since_epoch(1900 + tm->tm_year) + tm->tm_yday;
  120. auto timestamp = ((days_since_epoch * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec + timezone_adjust_seconds;
  121. time_to_tm(tm, timestamp);
  122. return timestamp;
  123. }
  124. time_t mktime(struct tm* tm)
  125. {
  126. return tm_to_time(tm, timezone);
  127. }
  128. struct tm* localtime(const time_t* t)
  129. {
  130. static struct tm tm_buf;
  131. return localtime_r(t, &tm_buf);
  132. }
  133. struct tm* localtime_r(const time_t* t, struct tm* tm)
  134. {
  135. if (!t)
  136. return nullptr;
  137. time_to_tm(tm, (*t) - timezone);
  138. return tm;
  139. }
  140. time_t timegm(struct tm* tm)
  141. {
  142. return tm_to_time(tm, 0);
  143. }
  144. struct tm* gmtime(const time_t* t)
  145. {
  146. static struct tm tm_buf;
  147. return gmtime_r(t, &tm_buf);
  148. }
  149. struct tm* gmtime_r(const time_t* t, struct tm* tm)
  150. {
  151. if (!t)
  152. return nullptr;
  153. time_to_tm(tm, *t);
  154. return tm;
  155. }
  156. char* asctime(const struct tm* tm)
  157. {
  158. static char buffer[69];
  159. strftime(buffer, sizeof buffer, "%a %b %e %T %Y", tm);
  160. return buffer;
  161. }
  162. //FIXME: Some formats are not supported.
  163. size_t strftime(char* destination, size_t max_size, const char* format, const struct tm* tm)
  164. {
  165. const char wday_short_names[7][4] = {
  166. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  167. };
  168. const char wday_long_names[7][10] = {
  169. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  170. };
  171. const char mon_short_names[12][4] = {
  172. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  173. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  174. };
  175. const char mon_long_names[12][10] = {
  176. "January", "February", "March", "April", "May", "June",
  177. "July", "August", "September", "October", "November", "December"
  178. };
  179. StringBuilder builder { max_size };
  180. const int format_len = strlen(format);
  181. for (int i = 0; i < format_len; ++i) {
  182. if (format[i] != '%') {
  183. builder.append(format[i]);
  184. } else {
  185. if (++i >= format_len)
  186. return 0;
  187. switch (format[i]) {
  188. case 'a':
  189. builder.append(wday_short_names[tm->tm_wday]);
  190. break;
  191. case 'A':
  192. builder.append(wday_long_names[tm->tm_wday]);
  193. break;
  194. case 'b':
  195. builder.append(mon_short_names[tm->tm_mon]);
  196. break;
  197. case 'B':
  198. builder.append(mon_long_names[tm->tm_mon]);
  199. break;
  200. case 'C':
  201. builder.appendf("%02d", (tm->tm_year + 1900) / 100);
  202. break;
  203. case 'd':
  204. builder.appendf("%02d", tm->tm_mday);
  205. break;
  206. case 'D':
  207. builder.appendf("%02d/%02d/%02d", tm->tm_mon + 1, tm->tm_mday, (tm->tm_year + 1900) % 100);
  208. break;
  209. case 'e':
  210. builder.appendf("%2d", tm->tm_mday);
  211. break;
  212. case 'h':
  213. builder.append(mon_short_names[tm->tm_mon]);
  214. break;
  215. case 'H':
  216. builder.appendf("%02d", tm->tm_hour);
  217. break;
  218. case 'I':
  219. builder.appendf("%02d", tm->tm_hour % 12);
  220. break;
  221. case 'j':
  222. builder.appendf("%03d", tm->tm_yday + 1);
  223. break;
  224. case 'm':
  225. builder.appendf("%02d", tm->tm_mon + 1);
  226. break;
  227. case 'M':
  228. builder.appendf("%02d", tm->tm_min);
  229. break;
  230. case 'n':
  231. builder.append('\n');
  232. break;
  233. case 'p':
  234. builder.append(tm->tm_hour < 12 ? "a.m." : "p.m.");
  235. break;
  236. case 'r':
  237. builder.appendf("%02d:%02d:%02d %s", tm->tm_hour % 12, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
  238. break;
  239. case 'R':
  240. builder.appendf("%02d:%02d", tm->tm_hour, tm->tm_min);
  241. break;
  242. case 'S':
  243. builder.appendf("%02d", tm->tm_sec);
  244. break;
  245. case 't':
  246. builder.append('\t');
  247. break;
  248. case 'T':
  249. builder.appendf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  250. break;
  251. case 'u':
  252. builder.appendf("%d", tm->tm_wday ? tm->tm_wday : 7);
  253. break;
  254. case 'U': {
  255. const int wday_of_year_beginning = (tm->tm_wday + 6 * tm->tm_yday) % 7;
  256. const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
  257. builder.appendf("%02d", week_number);
  258. break;
  259. }
  260. case 'V': {
  261. const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
  262. int week_number = (tm->tm_yday + wday_of_year_beginning) / 7 + 1;
  263. if (wday_of_year_beginning > 3) {
  264. if (tm->tm_yday >= 7 - wday_of_year_beginning)
  265. --week_number;
  266. else {
  267. const int days_of_last_year = days_in_year(tm->tm_year + 1900 - 1);
  268. const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
  269. week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
  270. if (wday_of_last_year_beginning > 3)
  271. --week_number;
  272. }
  273. }
  274. builder.appendf("%02d", week_number);
  275. break;
  276. }
  277. case 'w':
  278. builder.appendf("%d", tm->tm_wday);
  279. break;
  280. case 'W': {
  281. const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
  282. const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
  283. builder.appendf("%02d", week_number);
  284. break;
  285. }
  286. case 'y':
  287. builder.appendf("%02d", (tm->tm_year + 1900) % 100);
  288. break;
  289. case 'Y':
  290. builder.appendf("%d", tm->tm_year + 1900);
  291. break;
  292. case '%':
  293. builder.append('%');
  294. break;
  295. default:
  296. return 0;
  297. }
  298. }
  299. if (builder.length() + 1 > max_size)
  300. return 0;
  301. }
  302. auto str = builder.build();
  303. bool fits = str.copy_characters_to_buffer(destination, max_size);
  304. return fits ? str.length() : 0;
  305. }
  306. long timezone;
  307. long altzone;
  308. char* tzname[2];
  309. int daylight;
  310. constexpr const char* __utc = "UTC";
  311. void tzset()
  312. {
  313. // FIXME: Here we pretend we are in UTC+0.
  314. timezone = 0;
  315. daylight = 0;
  316. tzname[0] = const_cast<char*>(__utc);
  317. tzname[1] = const_cast<char*>(__utc);
  318. }
  319. clock_t clock()
  320. {
  321. struct tms tms;
  322. times(&tms);
  323. return tms.tms_utime + tms.tms_stime;
  324. }
  325. int clock_gettime(clockid_t clock_id, struct timespec* ts)
  326. {
  327. int rc = syscall(SC_clock_gettime, clock_id, ts);
  328. __RETURN_WITH_ERRNO(rc, rc, -1);
  329. }
  330. int clock_settime(clockid_t clock_id, struct timespec* ts)
  331. {
  332. int rc = syscall(SC_clock_settime, clock_id, ts);
  333. __RETURN_WITH_ERRNO(rc, rc, -1);
  334. }
  335. int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
  336. {
  337. Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
  338. int rc = syscall(SC_clock_nanosleep, &params);
  339. __RETURN_WITH_ERRNO(rc, rc, -1);
  340. }
  341. int nanosleep(const struct timespec* requested_sleep, struct timespec* remaining_sleep)
  342. {
  343. return clock_nanosleep(CLOCK_REALTIME, 0, requested_sleep, remaining_sleep);
  344. }
  345. int clock_getres(clockid_t, struct timespec*)
  346. {
  347. dbgln("FIXME: Implement clock_getres()");
  348. auto rc = -ENOSYS;
  349. __RETURN_WITH_ERRNO(rc, rc, -1);
  350. }
  351. double difftime(time_t t1, time_t t0)
  352. {
  353. return (double)(t1 - t0);
  354. }
  355. }