blob: 9048822776d14ffe02b7346503f24ffaeb2c616a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/* 19950925 */
#include "datetime.h"
void datetime_tai(struct datetime *dt,datetime_sec t)
{
int day;
int tod;
int year;
int yday;
int wday;
int mon;
tod = t % 86400;
day = t / 86400;
if (tod < 0) { tod += 86400; --day; }
dt->hour = tod / 3600;
tod %= 3600;
dt->min = tod / 60;
dt->sec = tod % 60;
wday = (day + 4) % 7; if (wday < 0) wday += 7;
dt->wday = wday;
day -= 11017;
/* day 0 is march 1, 2000 */
year = 5 + day / 146097;
day = day % 146097; if (day < 0) { day += 146097; --year; }
/* from now on, day is nonnegative */
year *= 4;
if (day == 146096) { year += 3; day = 36524; }
else { year += day / 36524; day %= 36524; }
year *= 25;
year += day / 1461;
day %= 1461;
year *= 4;
yday = (day < 306);
if (day == 1460) { year += 3; day = 365; }
else { year += day / 365; day %= 365; }
yday += day;
day *= 10;
mon = (day + 5) / 306;
day = day + 5 - 306 * mon;
day /= 10;
if (mon >= 10) { yday -= 306; ++year; mon -= 10; }
else { yday += 59; mon += 2; }
dt->yday = yday;
dt->year = year - 1900;
dt->mon = mon;
dt->mday = day + 1;
}
/* roughly 100x faster than mktime() */
datetime_sec datetime_untai(struct datetime *dt)
{
int year;
int day;
int mon;
year = dt->year + 1900;
mon = dt->mon;
if (mon >= 2) { mon -= 2; }
else { mon += 10; --year; }
day = (dt->mday - 1) * 10 + 5 + 306 * mon;
day /= 10;
if (day == 365) { year -= 3; day = 1460; }
else { day += 365 * (year % 4); }
year /= 4;
day += 1461 * (year % 25);
year /= 25;
if (day == 36524) { year -= 3; day = 146096; }
else { day += 36524 * (year % 4); }
year /= 4;
day += 146097 * (year - 5);
day += 11017;
return ((day * 24 + dt->hour) * 60 + dt->min) * 60 + dt->sec;
}
|