time.cpp 12 KB

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