-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys_sync.py
104 lines (63 loc) · 2.35 KB
/
sys_sync.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
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
#Program for system time clock sync
"""
Requires Internet Connection
Reference
https://pypi.org/project/ntplib/
"""
"""
Devansh Shukla
"""
import os
import ntplib # pip3 install ntplib
from datetime import datetime, timezone
def sys_sync_():
try:
print("Need sudo permissions to continue ")
_check_ = os.system("sudo echo Done")
if(_check_ == 0):
print("\n Connecting to NTP Server")
c = ntplib.NTPClient()
# Provide the respective ntp server ip in below function
response = c.request('time.nplindia.org', version=3)
#print(response.offset)
#print(ntplib.leap_to_text(response.leap))
current_time = datetime.fromtimestamp(response.tx_time)
print ("^" , current_time)
os.system("sudo date -s " + "\"" + str(current_time) + "\" " + "\"+%Y-%m-%d %H:%M:%S\"")
print("System Time Synced\n")
except:
print("Requires Internet Connection")
exit(0)
def check_sync():
try:
#print("\n Connecting to NTP Server")
c = ntplib.NTPClient()
# Provide the respective ntp server ip in below function
response = c.request('time.nplindia.org', version=3)
#print(response.offset)
#print(ntplib.leap_to_text(response.leap))
server_time = datetime.fromtimestamp(response.tx_time)
current_time = datetime.now()
t1 = server_time.timestamp()
#print(t1)
t2 = current_time.timestamp()
#print(t2)
global __t__
__t__ = t2 - t1 # System Time - Server Time
#print("Difference in Sync is " , __t__ , "s")
except:
print("Required Internet Connection")
exit(0)
__t__ = 0.5
while(abs(__t__) >= 0.5):
sys_sync_()
check_sync()
print("Difference in Sync is " , __t__ , "s " , "(SysT - ServerT)\n")
# ctime_timestamp = datetime.now().timestamp()
# corrected_time = ctime_timestamp - __t__
# corrected_time_fromtimestamp = datetime.fromtimestamp(corrected_time)
# co_time = corrected_time_fromtimestamp.strftime('%m/%d/%Y %X')
# print("*", corrected_time_fromtimestamp , co_time)
# os.system("sudo date -s " + "\"" + co_time + "\"")
# check_sync()
# print("Difference in Sync is " , __t__ , "s " , "(SysT - ServerT)\n")