malloc: more compact memory pools
[neatlibc.git] / localtime.c
blob5005eb96270774e0357591c65f0ad991dee7c8bf
1 #include <sys/time.h>
2 #include <sys/types.h>
3 #include <time.h>
5 #define isleap(y) (!((y) % 4) && ((y) % 100) || !((y) % 400))
6 #define SPD (24 * 60 * 60)
8 long timezone;
10 void tzset(void)
12 struct timezone tz;
13 gettimeofday(0, &tz);
14 timezone = tz.tz_minuteswest * 60;
17 static void tp2tm(struct tm *tm, time_t t)
19 static int dpm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
20 long days = t / SPD;
21 long rem = t % SPD;
22 int i;
23 tm->tm_sec = rem % 60;
24 rem /= 60;
25 tm->tm_min = rem % 60;
26 tm->tm_hour = rem / 60;
27 tm->tm_wday = (4 + days) % 7;
29 /* calculating yday and year */
30 for (i = 1970; days >= 365 + isleap(i); i++)
31 days -= 365 + isleap(i);
32 tm->tm_year = i - 1900;
33 tm->tm_yday = days;
35 /* calculating mday and mon */
36 tm->tm_mday = 1;
37 if (isleap(i) && days == 59)
38 tm->tm_mday++;
39 if (isleap(i) && days >= 59)
40 days--;
41 for (i = 0; i < 11 && days >= dpm[i]; i++)
42 days -= dpm[i];
43 tm->tm_mon = i;
44 tm->tm_mday += days;
47 struct tm *localtime(time_t *t)
49 static struct tm tm;
50 tzset();
51 tp2tm(&tm, *t - timezone);
52 return &tm;
55 struct tm *gmtime(time_t *t)
57 static struct tm tm;
58 tp2tm(&tm, *t);
59 return &tm;