time.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <Kernel/API/Syscall.h>
  30. #include <assert.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/time.h>
  35. #include <sys/times.h>
  36. #include <time.h>
  37. extern "C" {
  38. time_t time(time_t* tloc)
  39. {
  40. struct timeval tv;
  41. struct timezone tz;
  42. if (gettimeofday(&tv, &tz) < 0)
  43. return (time_t)-1;
  44. if (tloc)
  45. *tloc = tv.tv_sec;
  46. return tv.tv_sec;
  47. }
  48. int adjtime(const struct timeval* delta, struct timeval* old_delta)
  49. {
  50. int rc = syscall(SC_adjtime, delta, old_delta);
  51. __RETURN_WITH_ERRNO(rc, rc, -1);
  52. }
  53. int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  54. {
  55. int rc = syscall(SC_gettimeofday, tv);
  56. __RETURN_WITH_ERRNO(rc, rc, -1);
  57. }
  58. int settimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  59. {
  60. timespec ts;
  61. TIMEVAL_TO_TIMESPEC(tv, &ts);
  62. return clock_settime(CLOCK_REALTIME, &ts);
  63. }
  64. char* ctime(const time_t* t)
  65. {
  66. return asctime(localtime(t));
  67. }
  68. static const int __seconds_per_day = 60 * 60 * 24;
  69. static void time_to_tm(struct tm* tm, time_t t)
  70. {
  71. int year = 1970;
  72. for (; t >= days_in_year(year) * __seconds_per_day; ++year)
  73. t -= days_in_year(year) * __seconds_per_day;
  74. for (; t < 0; --year)
  75. t += days_in_year(year - 1) * __seconds_per_day;
  76. tm->tm_year = year - 1900;
  77. ASSERT(t >= 0);
  78. int days = t / __seconds_per_day;
  79. tm->tm_yday = days;
  80. int remaining = t % __seconds_per_day;
  81. tm->tm_sec = remaining % 60;
  82. remaining /= 60;
  83. tm->tm_min = remaining % 60;
  84. tm->tm_hour = remaining / 60;
  85. int month;
  86. for (month = 1; month < 12 && days >= days_in_month(year, month); ++month)
  87. days -= days_in_month(year, month);
  88. tm->tm_mday = days + 1;
  89. tm->tm_wday = day_of_week(year, month, tm->tm_mday);
  90. tm->tm_mon = month - 1;
  91. }
  92. static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)
  93. {
  94. // "The original values of the tm_wday and tm_yday components of the structure are ignored,
  95. // and the original values of the other components are not restricted to the ranges described in <time.h>.
  96. // [...]
  97. // Upon successful completion, the values of the tm_wday and tm_yday components of the structure shall be set appropriately,
  98. // and the other components are set to represent the specified time since the Epoch,
  99. // but with their values forced to the ranges indicated in the <time.h> entry;
  100. // the final value of tm_mday shall not be set until tm_mon and tm_year are determined."
  101. // FIXME: Handle tm_isdst eventually.
  102. tm->tm_year += tm->tm_mon / 12;
  103. tm->tm_mon %= 12;
  104. if (tm->tm_mon < 0) {
  105. tm->tm_year--;
  106. tm->tm_mon += 12;
  107. }
  108. tm->tm_yday = day_of_year(1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
  109. time_t days_since_epoch = years_to_days_since_epoch(1900 + tm->tm_year) + tm->tm_yday;
  110. auto timestamp = ((days_since_epoch * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec + timezone_adjust_seconds;
  111. time_to_tm(tm, timestamp);
  112. return timestamp;
  113. }
  114. time_t mktime(struct tm* tm)
  115. {
  116. return tm_to_time(tm, timezone);
  117. }
  118. struct tm* localtime(const time_t* t)
  119. {
  120. static struct tm tm_buf;
  121. return localtime_r(t, &tm_buf);
  122. }
  123. struct tm* localtime_r(const time_t* t, struct tm* tm)
  124. {
  125. if (!t)
  126. return nullptr;
  127. time_to_tm(tm, (*t) - timezone);
  128. return tm;
  129. }
  130. time_t timegm(struct tm* tm)
  131. {
  132. return tm_to_time(tm, 0);
  133. }
  134. struct tm* gmtime(const time_t* t)
  135. {
  136. static struct tm tm_buf;
  137. return gmtime_r(t, &tm_buf);
  138. }
  139. struct tm* gmtime_r(const time_t* t, struct tm* tm)
  140. {
  141. if (!t)
  142. return nullptr;
  143. time_to_tm(tm, *t);
  144. return tm;
  145. }
  146. char* asctime(const struct tm* tm)
  147. {
  148. static char buffer[69];
  149. strftime(buffer, sizeof buffer, "%a %b %e %T %Y", tm);
  150. return buffer;
  151. }
  152. //FIXME: Some formats are not supported.
  153. size_t strftime(char* destination, size_t max_size, const char* format, const struct tm* tm)
  154. {
  155. const char wday_short_names[7][4] = {
  156. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  157. };
  158. const char wday_long_names[7][10] = {
  159. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  160. };
  161. const char mon_short_names[12][4] = {
  162. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  163. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  164. };
  165. const char mon_long_names[12][10] = {
  166. "January", "February", "March", "April", "May", "June",
  167. "July", "August", "September", "October", "November", "December"
  168. };
  169. StringBuilder builder { max_size };
  170. const int format_len = strlen(format);
  171. for (int i = 0; i < format_len; ++i) {
  172. if (format[i] != '%') {
  173. builder.append(format[i]);
  174. } else {
  175. if (++i >= format_len)
  176. return 0;
  177. switch (format[i]) {
  178. case 'a':
  179. builder.append(wday_short_names[tm->tm_wday]);
  180. break;
  181. case 'A':
  182. builder.append(wday_long_names[tm->tm_wday]);
  183. break;
  184. case 'b':
  185. builder.append(mon_short_names[tm->tm_mon]);
  186. break;
  187. case 'B':
  188. builder.append(mon_long_names[tm->tm_mon]);
  189. break;
  190. case 'C':
  191. builder.appendf("%02d", (tm->tm_year + 1900) / 100);
  192. break;
  193. case 'd':
  194. builder.appendf("%02d", tm->tm_mday);
  195. break;
  196. case 'D':
  197. builder.appendf("%02d/%02d/%02d", tm->tm_mon + 1, tm->tm_mday, (tm->tm_year + 1900) % 100);
  198. break;
  199. case 'e':
  200. builder.appendf("%2d", tm->tm_mday);
  201. break;
  202. case 'h':
  203. builder.append(mon_short_names[tm->tm_mon]);
  204. break;
  205. case 'H':
  206. builder.appendf("%02d", tm->tm_hour);
  207. break;
  208. case 'I':
  209. builder.appendf("%02d", tm->tm_hour % 12);
  210. break;
  211. case 'j':
  212. builder.appendf("%03d", tm->tm_yday + 1);
  213. break;
  214. case 'm':
  215. builder.appendf("%02d", tm->tm_mon + 1);
  216. break;
  217. case 'M':
  218. builder.appendf("%02d", tm->tm_min);
  219. break;
  220. case 'n':
  221. builder.append('\n');
  222. break;
  223. case 'p':
  224. builder.append(tm->tm_hour < 12 ? "a.m." : "p.m.");
  225. break;
  226. case 'r':
  227. builder.appendf("%02d:%02d:%02d %s", tm->tm_hour % 12, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
  228. break;
  229. case 'R':
  230. builder.appendf("%02d:%02d", tm->tm_hour, tm->tm_min);
  231. break;
  232. case 'S':
  233. builder.appendf("%02d", tm->tm_sec);
  234. break;
  235. case 't':
  236. builder.append('\t');
  237. break;
  238. case 'T':
  239. builder.appendf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  240. break;
  241. case 'u':
  242. builder.appendf("%d", tm->tm_wday ? tm->tm_wday : 7);
  243. break;
  244. case 'U': {
  245. const int wday_of_year_beginning = (tm->tm_wday + 6 * tm->tm_yday) % 7;
  246. const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
  247. builder.appendf("%02d", week_number);
  248. break;
  249. }
  250. case 'V': {
  251. const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
  252. int week_number = (tm->tm_yday + wday_of_year_beginning) / 7 + 1;
  253. if (wday_of_year_beginning > 3) {
  254. if (tm->tm_yday >= 7 - wday_of_year_beginning)
  255. --week_number;
  256. else {
  257. const int days_of_last_year = days_in_year(tm->tm_year + 1900 - 1);
  258. const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
  259. week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
  260. if (wday_of_last_year_beginning > 3)
  261. --week_number;
  262. }
  263. }
  264. builder.appendf("%02d", week_number);
  265. break;
  266. }
  267. case 'w':
  268. builder.appendf("%d", tm->tm_wday);
  269. break;
  270. case 'W': {
  271. const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
  272. const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
  273. builder.appendf("%02d", week_number);
  274. break;
  275. }
  276. case 'y':
  277. builder.appendf("%02d", (tm->tm_year + 1900) % 100);
  278. break;
  279. case 'Y':
  280. builder.appendf("%d", tm->tm_year + 1900);
  281. break;
  282. case '%':
  283. builder.append('%');
  284. break;
  285. default:
  286. return 0;
  287. }
  288. }
  289. if (builder.length() + 1 > max_size)
  290. return 0;
  291. }
  292. auto str = builder.build();
  293. bool fits = str.copy_characters_to_buffer(destination, max_size);
  294. return fits ? str.length() : 0;
  295. }
  296. long timezone = 0;
  297. long altzone;
  298. char* tzname[2];
  299. int daylight;
  300. void tzset()
  301. {
  302. //FIXME: Here we prepend we are in UTC+0.
  303. timezone = 0;
  304. }
  305. clock_t clock()
  306. {
  307. struct tms tms;
  308. times(&tms);
  309. return tms.tms_utime + tms.tms_stime;
  310. }
  311. int clock_gettime(clockid_t clock_id, struct timespec* ts)
  312. {
  313. int rc = syscall(SC_clock_gettime, clock_id, ts);
  314. __RETURN_WITH_ERRNO(rc, rc, -1);
  315. }
  316. int clock_settime(clockid_t clock_id, struct timespec* ts)
  317. {
  318. int rc = syscall(SC_clock_settime, clock_id, ts);
  319. __RETURN_WITH_ERRNO(rc, rc, -1);
  320. }
  321. int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
  322. {
  323. Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
  324. int rc = syscall(SC_clock_nanosleep, &params);
  325. __RETURN_WITH_ERRNO(rc, rc, -1);
  326. }
  327. int nanosleep(const struct timespec* requested_sleep, struct timespec* remaining_sleep)
  328. {
  329. return clock_nanosleep(CLOCK_REALTIME, 0, requested_sleep, remaining_sleep);
  330. }
  331. int clock_getres(clockid_t, struct timespec*)
  332. {
  333. ASSERT_NOT_REACHED();
  334. }
  335. double difftime(time_t t1, time_t t0)
  336. {
  337. return (double)(t1 - t0);
  338. }
  339. }