time.cpp 11 KB

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