time.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <time.h>
  2. #include <sys/time.h>
  3. #include <errno.h>
  4. #include <assert.h>
  5. #include <Kernel/Syscall.h>
  6. extern "C" {
  7. time_t time(time_t* tloc)
  8. {
  9. struct timeval tv;
  10. struct timezone tz;
  11. if (gettimeofday(&tv, &tz) < 0)
  12. return (time_t)-1;
  13. if (tloc)
  14. *tloc = tv.tv_sec;
  15. return tv.tv_sec;
  16. }
  17. int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
  18. {
  19. int rc = syscall(SC_gettimeofday, tv);
  20. __RETURN_WITH_ERRNO(rc, rc, -1);
  21. }
  22. char* ctime(const time_t*)
  23. {
  24. return const_cast<char*>("ctime() not implemented");
  25. }
  26. static inline bool __is_leap_year(int year)
  27. {
  28. return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
  29. }
  30. static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  31. static const int __seconds_per_day = 60 * 60 * 24;
  32. static void time_to_tm(struct tm* tm, time_t t)
  33. {
  34. int days = t / __seconds_per_day;
  35. int remaining = t % __seconds_per_day;
  36. tm->tm_sec = remaining % 60;
  37. remaining /= 60;
  38. tm->tm_min = remaining % 60;
  39. tm->tm_hour = remaining / 60;
  40. tm->tm_wday = (4 + days) % 7;
  41. int year;
  42. for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
  43. days -= 365 + __is_leap_year(year);
  44. tm->tm_year = year - 1900;
  45. tm->tm_yday = days;
  46. tm->tm_mday = 1;
  47. if (__is_leap_year(year) && days == 59)
  48. ++tm->tm_mday;
  49. if (__is_leap_year(year) && days >= 59)
  50. --days;
  51. int month;
  52. for (month = 0; month < 11 && days >= __days_per_month[month]; ++month)
  53. days -= __days_per_month[month];
  54. tm->tm_mon = month;
  55. tm->tm_mday += days;
  56. }
  57. time_t mktime(struct tm* tm)
  58. {
  59. int days = 0;
  60. int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  61. for (int year = 70; year < tm->tm_year; ++year)
  62. days += 365 + __is_leap_year(1900 + year);
  63. tm->tm_yday = tm->tm_mday - 1;
  64. for (int month = 0; month < tm->tm_mon; ++month)
  65. tm->tm_yday += __days_per_month[month];
  66. days += tm->tm_yday;
  67. return days * __seconds_per_day + seconds;
  68. }
  69. struct tm* localtime(const time_t* t)
  70. {
  71. if (!t)
  72. return nullptr;
  73. static struct tm tm_buf;
  74. time_to_tm(&tm_buf, *t);
  75. return &tm_buf;
  76. }
  77. struct tm* gmtime(const time_t* t)
  78. {
  79. // FIXME: This is obviously not correct. What about timezones bro?
  80. return localtime(t);
  81. }
  82. char *asctime(const struct tm*)
  83. {
  84. assert(false);
  85. }
  86. size_t strftime(char*, size_t, const char*, const struct tm*)
  87. {
  88. assert(false);
  89. }
  90. long timezone;
  91. long altzone;
  92. char* tzname[2];
  93. int daylight;
  94. void tzset()
  95. {
  96. assert(false);
  97. }
  98. }