Line 0
Link Here
|
|
|
1 |
/* $MirOS: src/usr.sbin/rdate/ntpleaps.c,v 1.6 2006/06/09 20:58:09 tg Exp $ */ |
2 |
|
3 |
/*- |
4 |
* Copyright (c) 2006 |
5 |
* Thorsten Glaser <tg@mirbsd.de> |
6 |
* Based upon code placed into the public domain by Dan J. Bernstein. |
7 |
* |
8 |
* Licensee is hereby permitted to deal in this work without restric- |
9 |
* tion, including unlimited rights to use, publicly perform, modify, |
10 |
* merge, distribute, sell, give away or sublicence, provided all co- |
11 |
* pyright notices above, these terms and the disclaimer are retained |
12 |
* in all redistributions or reproduced in accompanying documentation |
13 |
* or other materials provided with binary redistributions. |
14 |
* |
15 |
* Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind, |
16 |
* express, or implied, to the maximum extent permitted by applicable |
17 |
* law, without malicious intent or gross negligence; in no event may |
18 |
* licensor, an author or contributor be held liable for any indirect |
19 |
* or other damage, or direct damage except proven a consequence of a |
20 |
* direct error of said person and intended use of this work, loss or |
21 |
* other issues arising in any way out of its use, even if advised of |
22 |
* the possibility of such damage or existence of a nontrivial bug. |
23 |
*/ |
24 |
|
25 |
#include <sys/types.h> |
26 |
#include <sys/time.h> |
27 |
#include <inttypes.h> |
28 |
#include <time.h> |
29 |
|
30 |
#include "ntpleaps.h" |
31 |
|
32 |
#ifndef __RCSID |
33 |
#define __RCSID(x) static const char __rcsid[] __attribute__((used)) = (x) |
34 |
#endif |
35 |
|
36 |
__RCSID("$MirOS: src/usr.sbin/rdate/ntpleaps.c,v 1.6 2006/06/09 20:58:09 tg Exp $"); |
37 |
|
38 |
time_t |
39 |
tick2utc(time_t kerneltick) |
40 |
{ |
41 |
static unsigned long times365[4] = { |
42 |
0, 365, 730, 1095 |
43 |
}; |
44 |
static unsigned long times36524[4] = { |
45 |
0, 36524UL, 73048UL, 109572UL |
46 |
}; |
47 |
static unsigned long montab[12] = { |
48 |
0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337 |
49 |
}; |
50 |
|
51 |
struct tm *tm; |
52 |
int64_t utc, d = 1900; |
53 |
int m, y; |
54 |
|
55 |
tm = localtime(&kerneltick); |
56 |
d += tm->tm_year; |
57 |
|
58 |
if (d < 0) |
59 |
++d; |
60 |
y = (int)(d % 400LL); |
61 |
d = 146097LL * (d / 400) + tm->tm_mday - 678882LL; |
62 |
utc = tm->tm_sec - tm->tm_gmtoff + 60 * (tm->tm_min + 60 * tm->tm_hour); |
63 |
|
64 |
while (utc < 0L) { |
65 |
utc += 86400L; |
66 |
--d; |
67 |
} |
68 |
while (utc > 86400L) { |
69 |
utc -= 86400L; |
70 |
++d; |
71 |
} |
72 |
|
73 |
if ((m = tm->tm_mon) >= 2) { |
74 |
m -= 2; |
75 |
} else { |
76 |
m += 10; |
77 |
--y; |
78 |
} |
79 |
|
80 |
y += (m / 12); |
81 |
m %= 12; |
82 |
if (m < 0) { |
83 |
m += 12; |
84 |
--y; |
85 |
} |
86 |
d += montab[m]; |
87 |
|
88 |
d += 146097LL * (y / 400LL); |
89 |
y %= 400; |
90 |
while (y < 0) { |
91 |
y += 400; |
92 |
d -= 146097LL; |
93 |
} |
94 |
|
95 |
d += times365[y & 3]; |
96 |
y /= 4; |
97 |
|
98 |
d += 1461LL * (y % 25); |
99 |
y /= 25; |
100 |
|
101 |
d += times36524[y & 3]; |
102 |
|
103 |
/* |
104 |
* d now contains the date part of the MJD |
105 |
* corresponding to tm / kerneltick; the |
106 |
* second part is already stored in utc. |
107 |
*/ |
108 |
|
109 |
utc += (d - 40587LL) * 86400LL; |
110 |
return (utc); |
111 |
} |