2018-03-01 03:07:43 +03:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define SEC_DAY 86400
|
|
|
|
|
|
|
|
#define fprintf(...)
|
|
|
|
|
|
|
|
static struct tm _timevalue;
|
|
|
|
|
|
|
|
static int year_is_leap(int year) {
|
|
|
|
return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 was a Thursday
|
|
|
|
|
|
|
|
static int day_of_week(long seconds) {
|
|
|
|
long day = seconds / SEC_DAY;
|
|
|
|
day += 4;
|
|
|
|
return day % 7;
|
|
|
|
}
|
|
|
|
|
2018-06-25 07:15:09 +03:00
|
|
|
static long days_in_month(int month, int year) {
|
2018-03-01 03:07:43 +03:00
|
|
|
switch(month) {
|
2018-12-01 05:09:09 +03:00
|
|
|
case 12:
|
|
|
|
return 31;
|
2018-03-01 03:07:43 +03:00
|
|
|
case 11:
|
|
|
|
return 30;
|
|
|
|
case 10:
|
|
|
|
return 31;
|
|
|
|
case 9:
|
|
|
|
return 30;
|
|
|
|
case 8:
|
|
|
|
return 31;
|
|
|
|
case 7:
|
|
|
|
return 31;
|
|
|
|
case 6:
|
|
|
|
return 30;
|
|
|
|
case 5:
|
|
|
|
return 31;
|
|
|
|
case 4:
|
|
|
|
return 30;
|
|
|
|
case 3:
|
|
|
|
return 31;
|
|
|
|
case 2:
|
|
|
|
return year_is_leap(year) ? 29 : 28;
|
|
|
|
case 1:
|
|
|
|
return 31;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 07:15:09 +03:00
|
|
|
static long secs_of_month(int months, int year) {
|
2018-03-01 03:07:43 +03:00
|
|
|
long days = 0;
|
|
|
|
for (int i = 1; i < months; ++i) {
|
|
|
|
days += days_in_month(months, year);
|
|
|
|
}
|
|
|
|
return days * SEC_DAY;
|
|
|
|
}
|
|
|
|
|
2018-06-25 10:28:13 +03:00
|
|
|
struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) {
|
2018-03-01 03:07:43 +03:00
|
|
|
|
2018-06-26 14:53:48 +03:00
|
|
|
fprintf(stderr, "Hello world? %p %d\n", _timevalue, *timep);
|
2018-03-01 03:07:43 +03:00
|
|
|
|
|
|
|
long seconds = 0; // this needs to be bigger, but whatever
|
|
|
|
|
|
|
|
long year_sec = 0;
|
|
|
|
|
|
|
|
for (int year = 1970; year < 2100; ++year) {
|
|
|
|
fprintf(stderr, "Checking year %d\n", year);
|
|
|
|
long added = year_is_leap(year) ? 366 : 365;
|
|
|
|
fprintf(stderr, "adding %d...\n", added);
|
|
|
|
long secs = added * 86400;
|
|
|
|
|
2019-12-20 14:20:23 +03:00
|
|
|
if (seconds + secs > *timep) {
|
2018-06-25 10:28:13 +03:00
|
|
|
_timevalue->tm_year = year - 1900;
|
2018-03-01 03:07:43 +03:00
|
|
|
year_sec = seconds;
|
|
|
|
fprintf(stderr, "The year is %d, year_sec=%d\n", year, year_sec);
|
|
|
|
for (int month = 1; month <= 12; ++month) {
|
|
|
|
fprintf(stderr, "Checking %d\n", month);
|
|
|
|
secs = days_in_month(month, year) * SEC_DAY;
|
|
|
|
fprintf(stderr, "%d vs %d\n", seconds + secs, *timep);
|
2019-12-20 14:20:23 +03:00
|
|
|
if (seconds + secs > *timep) {
|
2018-03-01 03:07:43 +03:00
|
|
|
fprintf(stderr, "The month is %d.\n", month);
|
2018-06-25 10:28:13 +03:00
|
|
|
_timevalue->tm_mon = month - 1;
|
2018-03-01 03:07:43 +03:00
|
|
|
for (int day = 1; day <= days_in_month(month, year); ++day) {
|
|
|
|
secs = 60 * 60 * 24;
|
|
|
|
fprintf(stderr, "Checking day %d, %d vs . %d\n", day, seconds + secs, *timep);
|
2019-12-20 14:20:23 +03:00
|
|
|
if (seconds + secs > *timep) {
|
2018-03-01 03:07:43 +03:00
|
|
|
fprintf(stderr, "The day is %d.\n", day);
|
2018-06-25 10:28:13 +03:00
|
|
|
_timevalue->tm_mday = day;
|
2018-03-01 03:07:43 +03:00
|
|
|
for (int hour = 1; hour <= 24; ++hour) {
|
|
|
|
secs = 60 * 60;
|
2019-12-20 14:20:23 +03:00
|
|
|
if (seconds + secs > *timep) {
|
2018-03-01 03:07:43 +03:00
|
|
|
long remaining = *timep - seconds;
|
2019-12-20 14:20:23 +03:00
|
|
|
_timevalue->tm_hour = hour - 1;
|
2018-06-25 10:28:13 +03:00
|
|
|
_timevalue->tm_min = remaining / 60;
|
|
|
|
_timevalue->tm_sec = remaining % 60; // can be 60 on a leap second, ignore that
|
|
|
|
_timevalue->tm_wday = day_of_week(*timep); // oh shit
|
|
|
|
_timevalue->tm_yday = (*timep - year_sec) / SEC_DAY;
|
|
|
|
_timevalue->tm_isdst = 0; // never because UTC
|
|
|
|
return _timevalue;
|
2018-03-01 03:07:43 +03:00
|
|
|
} else {
|
|
|
|
seconds += secs;
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 14:53:48 +03:00
|
|
|
fprintf(stderr, "Failed but this is definitely the right day, returning NULL (seconds = %dbut need %d)\n", seconds, *timep);
|
2018-03-01 03:07:43 +03:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
seconds += secs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Failed but this is definitely the right month, returning NULL\n");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
seconds += secs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Failed but this is definitely the right year, returning NULL\n");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
seconds += secs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Uh, no?\n");
|
|
|
|
return (void *)0; /// uh what
|
|
|
|
}
|
2018-06-25 07:15:09 +03:00
|
|
|
|
|
|
|
static unsigned int secs_of_years(int years) {
|
|
|
|
unsigned int days = 0;
|
|
|
|
while (years > 1969) {
|
|
|
|
days += 365;
|
|
|
|
if (year_is_leap(years)) {
|
|
|
|
days++;
|
|
|
|
}
|
|
|
|
years--;
|
|
|
|
}
|
|
|
|
return days * 86400;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t mktime(struct tm *tm) {
|
|
|
|
return
|
|
|
|
secs_of_years(tm->tm_year + 1900) +
|
|
|
|
secs_of_month(tm->tm_mon, tm->tm_year + 1900) +
|
|
|
|
(tm->tm_mday - 1) * 86400 +
|
|
|
|
(tm->tm_hour) * 3600 +
|
|
|
|
(tm->tm_min) * 60 +
|
|
|
|
(tm->tm_sec);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-25 10:28:13 +03:00
|
|
|
struct tm * gmtime_r(const time_t * timep, struct tm * tm) {
|
|
|
|
return localtime_r(timep, tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm * localtime(const time_t *timep) {
|
|
|
|
return localtime_r(timep, &_timevalue);
|
|
|
|
}
|
|
|
|
|
2018-06-25 07:15:09 +03:00
|
|
|
struct tm *gmtime(const time_t *timep) {
|
|
|
|
return localtime(timep);
|
|
|
|
}
|
2018-06-25 10:28:13 +03:00
|
|
|
|