localtime.c (783B)
1 #include <time.h> 2 3 static int mday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 4 5 struct tm *localtime(const time_t *tp) 6 { 7 time_t t = *tp; 8 static struct tm tm; 9 10 tm.tm_sec = t % 60; 11 t /= 60; 12 tm.tm_min = t % 60; 13 t /= 60; 14 tm.tm_hour = t % 24; 15 t /= 24; 16 tm.tm_wday = (t + 4) % 7; // Jan 1 1970 = Thursday 17 18 int isleap = 0; 19 for (tm.tm_year=70;; tm.tm_year++) { 20 isleap = (tm.tm_year+1900)%4 == 0; 21 if (t < 365 + isleap) 22 break; 23 t -= 365 + isleap; 24 } 25 26 tm.tm_yday = t; 27 for (tm.tm_mon=0;; tm.tm_mon++) { 28 isleap = (tm.tm_year+1900)%4 == 0 && tm.tm_mon == 1; 29 if (t < mday[tm.tm_mon] + isleap) 30 break; 31 t -= mday[tm.tm_mon] + isleap; 32 } 33 tm.tm_mday = t + 1; 34 tm.tm_isdst = 0; 35 36 return &tm; 37 } 38 39 struct tm *gmtime(const time_t *tp) 40 { 41 return localtime(tp); 42 }