stdio: puts() and vprintf()
[neatlibc.git] / localtime.c
blobb7aca6412ec1ec99d7897273c12e02550d4710e7
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;
62 time_t mktime(struct tm *tm)
64 static int dpm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
65 int d = 0, s = 0;
66 int i;
67 s = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
68 for (i = 70; i < tm->tm_year; i++)
69 d += 365 + isleap(1900 + i);
70 tm->tm_yday = tm->tm_mday - 1;
71 for (i = 0; i < tm->tm_mon; i++)
72 tm->tm_yday += dpm[i];
73 d += tm->tm_yday;
74 tzset();
75 return d * 24 * 3600 + s + timezone;