This source file includes following definitions.
- __isleap
- math_div
- leaps_between
- time64_to_tm
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 #include <linux/time.h>
32 #include <linux/module.h>
33
34
35
36
37
38 static int __isleap(long year)
39 {
40 return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0);
41 }
42
43
44 static long math_div(long a, long b)
45 {
46 return a / b - (a % b < 0);
47 }
48
49
50 static long leaps_between(long y1, long y2)
51 {
52 long leaps1 = math_div(y1 - 1, 4) - math_div(y1 - 1, 100)
53 + math_div(y1 - 1, 400);
54 long leaps2 = math_div(y2 - 1, 4) - math_div(y2 - 1, 100)
55 + math_div(y2 - 1, 400);
56 return leaps2 - leaps1;
57 }
58
59
60 static const unsigned short __mon_yday[2][13] = {
61
62 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
63
64 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
65 };
66
67 #define SECS_PER_HOUR (60 * 60)
68 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
69
70
71
72
73
74
75
76
77
78 void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
79 {
80 long days, rem, y;
81 int remainder;
82 const unsigned short *ip;
83
84 days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
85 rem = remainder;
86 rem += offset;
87 while (rem < 0) {
88 rem += SECS_PER_DAY;
89 --days;
90 }
91 while (rem >= SECS_PER_DAY) {
92 rem -= SECS_PER_DAY;
93 ++days;
94 }
95
96 result->tm_hour = rem / SECS_PER_HOUR;
97 rem %= SECS_PER_HOUR;
98 result->tm_min = rem / 60;
99 result->tm_sec = rem % 60;
100
101
102 result->tm_wday = (4 + days) % 7;
103 if (result->tm_wday < 0)
104 result->tm_wday += 7;
105
106 y = 1970;
107
108 while (days < 0 || days >= (__isleap(y) ? 366 : 365)) {
109
110 long yg = y + math_div(days, 365);
111
112
113 days -= (yg - y) * 365 + leaps_between(y, yg);
114 y = yg;
115 }
116
117 result->tm_year = y - 1900;
118
119 result->tm_yday = days;
120
121 ip = __mon_yday[__isleap(y)];
122 for (y = 11; days < ip[y]; y--)
123 continue;
124 days -= ip[y];
125
126 result->tm_mon = y;
127 result->tm_mday = days + 1;
128 }
129 EXPORT_SYMBOL(time64_to_tm);