time_t mktime(struct tm *pt) { time_t res = 0; int unix_year; time_t year_secs; pt->tm_wday = 0; pt->tm_yday = 0; if (tm_out_of_range(pt)) { errno = EOVERFLOW; return (time_t)(-1); } unix_year = (1900 + pt->tm_year) - 1970; if (unix_year != 0) { if (unix_year < 0) { res += get_year_seconds(abs(unix_year) - 1, 1971 + unix_year); } else { res += get_year_seconds(unix_year, 1970); } } pt->tm_yday = get_yday(pt); if (unix_year < 0) { year_secs = pt->tm_yday * 86400; year_secs += pt->tm_hour * 3600; year_secs += pt->tm_min * 60; year_secs += pt->tm_sec; if (is_leap_year(1970 + unix_year)) { res += (31622400 - year_secs); } else { res += (31536000 - year_secs); } res = -res; } else { res += pt->tm_yday * 86400; res += pt->tm_hour * 3600; res += pt->tm_min * 60; res += pt->tm_sec; } pt->tm_wday = get_week_day(res / 86400); _tz_add_offset(&res); return res; }
//-----------------------------------------------------------------// long get_total_day(short year, char mon, char day) { if(year < 1970) return -1L; long d = 0; short i = 1970; while(i < year) { d += (long)get_yday(i); i++; } i = 0; while(i < mon) { d += get_mday(year, i); i++; } d += day - 1; return d; }
//-----------------------------------------------------------------// struct tm *gmtime(const time_t *tp) { time_t t; short i, j, k; t = *tp; time_st_.tm_sec = t % (time_t)60; t /= (time_t)60; time_st_.tm_min = t % (time_t)60; t /= (time_t)60; time_st_.tm_hour = t % (time_t)24; t /= (time_t)24; time_st_.tm_wday = (t + 4) % 7; j = 1970; while(t >= (i = get_yday(j))) { t -= (time_t)i; j++; } time_st_.tm_year = j - 1900; time_st_.tm_yday = t; k = 0; while(t >= (i = get_mday(j, k))) { t -= (time_t)i; k++; } time_st_.tm_mon = k; time_st_.tm_mday = t + 1; return &time_st_; }