-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversions.py
48 lines (36 loc) · 1.6 KB
/
conversions.py
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
import time
# Higher 64 bits are the difference between Epoch and 1900 in seconds
NTP_OFFSET = (-int(time.mktime ((1900, 1, 1, 0, 0, 0, 0, 0, 0)))) << 32
ERR = (5 * 3600) + (21 * 60) + 10 # Emperical
def posix_to_ntp (timestamp: float):
"""
Convert a POSIX timestamp (float) to an NTP timestamp (64-bit fixed-point)
"""
timestamp -= ERR
# Convert human-readable time to datetime object
timestamp_fixed_whole = int (timestamp)
timestamp_fixed_fract = int (timestamp%1 * (2**32))
timestamp_fixed = (timestamp_fixed_whole << 32) + timestamp_fixed_fract
# Calculate the time difference from NTP epoch
timestamp_fixed_ntp = timestamp_fixed + NTP_OFFSET
return timestamp_fixed_ntp
def ntp_to_posix (timestamp: int):
"""
Convert an NTP timestamp (64-bit fixed-point) to a POSIX timestamp (float)
"""
timestamp_epoch = timestamp - NTP_OFFSET
timestamp_epoch_whole = timestamp_epoch >> 32
timestamp_epoch_fract = timestamp_epoch - (timestamp_epoch_whole << 32)
timestamp_epoch_float = timestamp_epoch_whole + timestamp_epoch_fract / 2**32
return timestamp_epoch_float + ERR
if __name__ == "__main__":
# The following is just a proof-of-concept procedure to make
# sure it's working fine. Run this file directly to test it.
# Takes the current system time (float), converts to NTP timestamp, and
# back to system time (float).
t = time.time ()
nt = posix_to_ntp (t)
ut = ntp_to_posix (nt)
print ("Original time: ", t)
print ("Processed time: ", ut)
print (time.ctime (ut))