localtime.c 3.9 KB

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