localtime.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <time.h>
  2. #include <sys/time.h>
  3. #include <stdio.h>
  4. #define SEC_DAY 86400
  5. #define fprintf(...)
  6. static struct tm _timevalue;
  7. static int year_is_leap(int year) {
  8. return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
  9. }
  10. // 0 was a Thursday
  11. static int day_of_week(long seconds) {
  12. long day = seconds / SEC_DAY;
  13. day += 4;
  14. return day % 7;
  15. }
  16. static long days_in_month(int month, int year) {
  17. switch(month) {
  18. case 12:
  19. return 31;
  20. case 11:
  21. return 30;
  22. case 10:
  23. return 31;
  24. case 9:
  25. return 30;
  26. case 8:
  27. return 31;
  28. case 7:
  29. return 31;
  30. case 6:
  31. return 30;
  32. case 5:
  33. return 31;
  34. case 4:
  35. return 30;
  36. case 3:
  37. return 31;
  38. case 2:
  39. return year_is_leap(year) ? 29 : 28;
  40. case 1:
  41. return 31;
  42. }
  43. return 0;
  44. }
  45. static long secs_of_month(int months, int year) {
  46. long days = 0;
  47. for (int i = 1; i < months; ++i) {
  48. days += days_in_month(months, year);
  49. }
  50. return days * SEC_DAY;
  51. }
  52. struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) {
  53. fprintf(stderr, "Hello world? %p %d\n", _timevalue, *timep);
  54. long seconds = 0; // this needs to be bigger, but whatever
  55. long year_sec = 0;
  56. for (int year = 1970; year < 2100; ++year) {
  57. fprintf(stderr, "Checking year %d\n", year);
  58. long added = year_is_leap(year) ? 366 : 365;
  59. fprintf(stderr, "adding %d...\n", added);
  60. long secs = added * 86400;
  61. if (seconds + secs > *timep) {
  62. _timevalue->tm_year = year - 1900;
  63. year_sec = seconds;
  64. fprintf(stderr, "The year is %d, year_sec=%d\n", year, year_sec);
  65. for (int month = 1; month <= 12; ++month) {
  66. fprintf(stderr, "Checking %d\n", month);
  67. secs = days_in_month(month, year) * SEC_DAY;
  68. fprintf(stderr, "%d vs %d\n", seconds + secs, *timep);
  69. if (seconds + secs > *timep) {
  70. fprintf(stderr, "The month is %d.\n", month);
  71. _timevalue->tm_mon = month - 1;
  72. for (int day = 1; day <= days_in_month(month, year); ++day) {
  73. secs = 60 * 60 * 24;
  74. fprintf(stderr, "Checking day %d, %d vs . %d\n", day, seconds + secs, *timep);
  75. if (seconds + secs > *timep) {
  76. fprintf(stderr, "The day is %d.\n", day);
  77. _timevalue->tm_mday = day;
  78. for (int hour = 1; hour <= 24; ++hour) {
  79. secs = 60 * 60;
  80. if (seconds + secs > *timep) {
  81. long remaining = *timep - seconds;
  82. _timevalue->tm_hour = hour - 1;
  83. _timevalue->tm_min = remaining / 60;
  84. _timevalue->tm_sec = remaining % 60; // can be 60 on a leap second, ignore that
  85. _timevalue->tm_wday = day_of_week(*timep); // oh shit
  86. _timevalue->tm_yday = (*timep - year_sec) / SEC_DAY;
  87. _timevalue->tm_isdst = 0; // never because UTC
  88. return _timevalue;
  89. } else {
  90. seconds += secs;
  91. }
  92. }
  93. fprintf(stderr, "Failed but this is definitely the right day, returning NULL (seconds = %dbut need %d)\n", seconds, *timep);
  94. return NULL;
  95. } else {
  96. seconds += secs;
  97. }
  98. }
  99. fprintf(stderr, "Failed but this is definitely the right month, returning NULL\n");
  100. return NULL;
  101. } else {
  102. seconds += secs;
  103. }
  104. }
  105. fprintf(stderr, "Failed but this is definitely the right year, returning NULL\n");
  106. return NULL;
  107. } else {
  108. seconds += secs;
  109. }
  110. }
  111. fprintf(stderr, "Uh, no?\n");
  112. return (void *)0; /// uh what
  113. }
  114. static unsigned int secs_of_years(int years) {
  115. unsigned int days = 0;
  116. while (years > 1969) {
  117. days += 365;
  118. if (year_is_leap(years)) {
  119. days++;
  120. }
  121. years--;
  122. }
  123. return days * 86400;
  124. }
  125. time_t mktime(struct tm *tm) {
  126. return
  127. secs_of_years(tm->tm_year + 1900) +
  128. secs_of_month(tm->tm_mon, tm->tm_year + 1900) +
  129. (tm->tm_mday - 1) * 86400 +
  130. (tm->tm_hour) * 3600 +
  131. (tm->tm_min) * 60 +
  132. (tm->tm_sec);
  133. }
  134. struct tm * gmtime_r(const time_t * timep, struct tm * tm) {
  135. return localtime_r(timep, tm);
  136. }
  137. struct tm * localtime(const time_t *timep) {
  138. return localtime_r(timep, &_timevalue);
  139. }
  140. struct tm *gmtime(const time_t *timep) {
  141. return localtime(timep);
  142. }