time.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <Kernel/Syscall.h>
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <sys/time.h>
  5. #include <sys/times.h>
  6. #include <time.h>
  7. #include <string.h>
  8. extern "C" {
  9. time_t time(time_t* tloc)
  10. {
  11. struct timeval tv;
  12. struct timezone tz;
  13. if (gettimeofday(&tv, &tz) < 0)
  14. return (time_t)-1;
  15. if (tloc)
  16. *tloc = tv.tv_sec;
  17. return tv.tv_sec;
  18. }
  19. int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  20. {
  21. int rc = syscall(SC_gettimeofday, tv);
  22. __RETURN_WITH_ERRNO(rc, rc, -1);
  23. }
  24. char* ctime(const time_t*)
  25. {
  26. return const_cast<char*>("ctime() not implemented");
  27. }
  28. static inline bool __is_leap_year(int year)
  29. {
  30. return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
  31. }
  32. static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  33. static const int __seconds_per_day = 60 * 60 * 24;
  34. static void time_to_tm(struct tm* tm, time_t t)
  35. {
  36. int days = t / __seconds_per_day;
  37. int remaining = t % __seconds_per_day;
  38. tm->tm_sec = remaining % 60;
  39. remaining /= 60;
  40. tm->tm_min = remaining % 60;
  41. tm->tm_hour = remaining / 60;
  42. tm->tm_wday = (4 + days) % 7;
  43. int year;
  44. for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
  45. days -= 365 + __is_leap_year(year);
  46. tm->tm_year = year - 1900;
  47. tm->tm_yday = days;
  48. tm->tm_mday = 1;
  49. if (__is_leap_year(year) && days == 59)
  50. ++tm->tm_mday;
  51. if (__is_leap_year(year) && days >= 59)
  52. --days;
  53. int month;
  54. for (month = 0; month < 11 && days >= __days_per_month[month]; ++month)
  55. days -= __days_per_month[month];
  56. tm->tm_mon = month;
  57. tm->tm_mday += days;
  58. }
  59. time_t mktime(struct tm* tm)
  60. {
  61. int days = 0;
  62. int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  63. for (int year = 70; year < tm->tm_year; ++year)
  64. days += 365 + __is_leap_year(1900 + year);
  65. tm->tm_yday = tm->tm_mday - 1;
  66. for (int month = 0; month < tm->tm_mon; ++month)
  67. tm->tm_yday += __days_per_month[month];
  68. days += tm->tm_yday;
  69. return days * __seconds_per_day + seconds;
  70. }
  71. struct tm* localtime(const time_t* t)
  72. {
  73. if (!t)
  74. return nullptr;
  75. static struct tm tm_buf;
  76. time_to_tm(&tm_buf, *t);
  77. return &tm_buf;
  78. }
  79. struct tm* gmtime(const time_t* t)
  80. {
  81. // FIXME: This is obviously not correct. What about timezones bro?
  82. return localtime(t);
  83. }
  84. char* asctime(const struct tm*)
  85. {
  86. ASSERT_NOT_REACHED();
  87. }
  88. size_t strftime(char* destination, size_t, const char*, const struct tm*)
  89. {
  90. // FIXME: Stubbed function to make nasm work. Should be properly implemented
  91. strcpy(destination, "strftime_unimplemented");
  92. return strlen("strftime_unimplemented");
  93. }
  94. long timezone;
  95. long altzone;
  96. char* tzname[2];
  97. int daylight;
  98. void tzset()
  99. {
  100. ASSERT_NOT_REACHED();
  101. }
  102. clock_t clock()
  103. {
  104. struct tms tms;
  105. times(&tms);
  106. return tms.tms_utime + tms.tms_stime;
  107. }
  108. int clock_gettime(clockid_t clock_id, struct timespec* ts)
  109. {
  110. int rc = syscall(SC_clock_gettime, clock_id, ts);
  111. __RETURN_WITH_ERRNO(rc, rc, -1);
  112. }
  113. int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
  114. {
  115. Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
  116. int rc = syscall(SC_clock_nanosleep, &params);
  117. __RETURN_WITH_ERRNO(rc, rc, -1);
  118. }
  119. }