-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock_sync_mock.py
More file actions
49 lines (39 loc) · 2 KB
/
clock_sync_mock.py
File metadata and controls
49 lines (39 loc) · 2 KB
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
from time import ctime
import time , calendar , os, sys, logging, datetime
from clock_lib.dateutil import relativedelta
from clock_lib import ntplib
from subprocess import Popen, PIPE
#reference points
#<timeserver> will need to be changed to your time server url
#https://stackoverflow.com/questions/12664295/ntp-client-in-python
#https://stackoverflow.com/questions/6574329/how-can-i-produce-a-human-readable-difference-when-subtracting-two-unix-timestam
def test():
c = ntplib.NTPClient() #create a object of the class
response = c.request('<timeserver>')
isntp =response.tx_time
sub= "@"
sub1 = sub + str(isntp)
isOS = calendar.timegm(time.gmtime()) #why use this when you can call time.time() ? --hel
dt1 = datetime.datetime.fromtimestamp(isntp) # 1973-11-29 2i2:33:09
dt2 = datetime.datetime.fromtimestamp(isOS) # 1977-06-07 23:44:50
rd = relativedelta.relativedelta(dt2, dt1)
# 3 years, 6 months, 9 days, 1 hours, 11 minutes and 41 secondsi
#logging config
validate = rd.minutes
if validate < -1 :
# print(" %d hours, %d minutes and %d seconds" % (rd.hours, rd.minutes, rd.seconds))
# print("Time is out of sync & self correctng ")
code =Popen(["date", "-s", sub1])
code.communicate()
logging.basicConfig(format='%(asctime)s %(message)s',filename='clocksync-natprov.log',level=logging.INFO)
logging.info('The clock was out of sync with the NTP server by -%s minutes ' , validate)
elif validate > 1 :
# print(" %d hours, %d minutes and %d seconds" % (rd.hours, rd.minutes, rd.seconds))
# print("Time is out of sync & self correctng ")
log = rd
code =Popen(["date", "-s", sub1])
code.communicate()
logging.basicConfig(format='%(asctime)s %(message)s',filename='clocksync-natprov.log',level=logging.INFO)
logging.info('The clock was out of sync with the NTP server by %s minutes' , validate)
else:
print("Time is synced")