-
Notifications
You must be signed in to change notification settings - Fork 5
/
untplib.patch
109 lines (93 loc) · 3.05 KB
/
untplib.patch
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
--- C:/Temp/untplib/ntplib.py Mon Jul 27 20:20:45 2015
+++ C:/Temp/untplib/untplib.py Sun Oct 25 15:48:30 2015
@@ -21,6 +21,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
+
+#############################################
+# Changed to run in MicroPython on the WiPy
+# by Andrew Hedges
+# 24th Oct 2015
+#############################################
+
"""Python NTP library.
Implementation of client-side NTP (RFC-1305), and useful NTP-related
@@ -28,7 +35,7 @@
"""
-import datetime
+#import datetime
import socket
import struct
import time
@@ -42,11 +49,12 @@
class NTP:
"""Helper class defining constants."""
- _SYSTEM_EPOCH = datetime.date(*time.gmtime(0)[0:3])
+ #_SYSTEM_EPOCH = datetime.date(*time.gmtime(0)[0:3])
"""system epoch"""
- _NTP_EPOCH = datetime.date(1900, 1, 1)
+ #_NTP_EPOCH = datetime.date(1900, 1, 1)
"""NTP epoch"""
- NTP_DELTA = (_SYSTEM_EPOCH - _NTP_EPOCH).days * 24 * 3600
+ #NTP_DELTA = (_SYSTEM_EPOCH - _NTP_EPOCH).days * 24 * 3600
+ NTP_DELTA = 3155673600
"""delta between system and NTP time"""
REF_ID_TABLE = {
@@ -118,7 +126,7 @@
This represents an NTP packet.
"""
- _PACKET_FORMAT = "!B B B b 11I"
+ _PACKET_FORMAT = "!BBBbIIIIIIIIIII"
"""packet format to pack/unpack"""
def __init__(self, version=2, mode=3, tx_timestamp=0):
@@ -209,8 +217,8 @@
self.stratum = unpacked[1]
self.poll = unpacked[2]
self.precision = unpacked[3]
- self.root_delay = float(unpacked[4])/2**16
- self.root_dispersion = float(unpacked[5])/2**16
+ self.root_delay = (unpacked[4])//2**16
+ self.root_dispersion = (unpacked[5])//2**16
self.ref_id = unpacked[6]
self.ref_timestamp = _to_time(unpacked[7], unpacked[8])
self.orig_timestamp = _to_time(unpacked[9], unpacked[10])
@@ -235,7 +243,7 @@
def offset(self):
"""offset"""
return ((self.recv_timestamp - self.orig_timestamp) +
- (self.tx_timestamp - self.dest_timestamp))/2
+ (self.tx_timestamp - self.dest_timestamp))//2
@property
def delay(self):
@@ -276,7 +284,7 @@
"""Constructor."""
pass
- def request(self, host, version=2, port='ntp', timeout=5):
+ def request(self, host, version=2, port=123, timeout=5):
"""Query a NTP server.
Parameters:
@@ -334,7 +342,8 @@
Retuns:
integral part
"""
- return int(timestamp)
+ return timestamp
+ #return int(timestamp)
def _to_frac(timestamp, n=32):
@@ -347,7 +356,8 @@
Retuns:
fractional part
"""
- return int(abs(timestamp - _to_int(timestamp)) * 2**n)
+ return 0
+ #return int(abs(timestamp - _to_int(timestamp)) * 2**n)
def _to_time(integ, frac, n=32):
@@ -361,7 +371,8 @@
Retuns:
timestamp
"""
- return integ + float(frac)/2**n
+ return integ
+ #return integ + float(frac)/2**n
def ntp_to_system_time(timestamp):