-
Notifications
You must be signed in to change notification settings - Fork 37
/
clockmod.py
executable file
·192 lines (151 loc) · 4.9 KB
/
clockmod.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import print_function
import logging
import sys
import time
import os
from datetime import datetime,date,timedelta
#!/usr/bin/env python
from socket import AF_INET, SOCK_DGRAM
import socket
import struct
import clockdbmod
logger = logging.getLogger("hydrosys4."+__name__)
# ///////////////// -- GLOBAL VARIABLES AND INIZIALIZATION --- //////////////////////////////////////////
DATEFORMAT="%d/%m/%Y %H:%M"
timezone=clockdbmod.gettimezone()
os.environ['TZ'] = timezone
time.tzset()
print("timezone set to ->", timezone)
#logger.info('Timezone Set to = %s', timezone) # if this is enabled,for some reason the whole logging become empty
# ///////////////// --- END GLOBAL VARIABLES AND INIT------
def timediffinsec(timestr1, timestr2):
try:
datetime1=datetime.strptime(timestr1, DATEFORMAT)
datetime2=datetime.strptime(timestr2, DATEFORMAT)
except:
print("Time in wrong format, not able to make diffsec ")
return 0
delta=datetime2-datetime1
timediff=abs(delta.total_seconds())
return timediff
def readsystemdatetime():
return datetime.now().strftime(DATEFORMAT)
def getNTPTime(host = "pool.ntp.org"):
port = 123
buf = 1024
address = (host,port)
msg = '\x1b' + 47 * '\0'
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800 # 1970-01-01 00:00:00
# connect to server
try:
client = socket.socket( AF_INET, SOCK_DGRAM)
client.settimeout(2)
client.sendto(msg.encode(), address)
msg, address = client.recvfrom( buf )
except socket.timeout as e:
print("server timeout")
return ""
except socket.error as e:
print("connection error")
return ""
if msg:
t = struct.unpack( "!12I", msg )[10]
t -= TIME1970
#strvalue=time.ctime(t).replace(" "," ")
try:
datetimevalue=datetime.utcfromtimestamp(t)
except:
return ""
strvalueUTC=datetimevalue.strftime(DATEFORMAT)
strvalue=convertUTCtoLOC(strvalueUTC)
return strvalue
else:
print("No valid data in server answer ")
return ""
def getHWclock():
#need to check how to do it
print("not done")
def setHWclock(datetime_format):
datetimeUTC=convertLOCtoUTC(datetime_format)
print("Set HWclock datetime UTC ->" ,datetimeUTC)
datetimetype=datetime.strptime(datetimeUTC, DATEFORMAT)
newformat="%d %b %Y %H:%M:%S"
date_str="\"" + datetimetype.strftime(newformat) + "\""
print("Set HW clock ->" , date_str)
try:
os.system('hwclock --set --date %s --localtime' % date_str)
#time.sleep(1)
#newdatetime=getHWclockUTC()
#delta=newdatetime-datetimetype
#timediff=abs(delta.total_seconds())
#print "time difference ", timediff , " System date " ,newdatetime , " Set Date" , datetimetype
#if timediff<60:
# return "- Hardware Clock Set -"
#else:
# return " - ERROR: not able to set Hardware Clock -"
return " - HardwareClock Set - "
except:
print("Not able to set Hardware Clock ")
#logger.error('Not able to set Hardware Clock')
return "ERROR: not able to set Hardware Clock"
def setsystemclock(datetime_format):
datetimeUTC=convertLOCtoUTC(datetime_format)
print("Set System date to datetime UTC ->" ,datetimeUTC)
datetimetype=datetime.strptime(datetimeUTC, DATEFORMAT)
print("Set system clock ->", datetimetype)
newformat="%d %b %Y %H:%M:%S"
date_str="\"" + datetimetype.strftime(newformat) + "\""
print("Datetime value format for date setting ", date_str)
try:
os.system('date --set %s -u' % date_str)
time.sleep(1)
newdatetime=datetime.utcnow()
delta=newdatetime-datetimetype
timediff=abs(delta.total_seconds())
print("time difference ", timediff , " System date " ,newdatetime , " Set Date" , datetimetype)
if timediff<60:
return "- System Clock Set -"
else:
return " - ERROR: not able to set system Clock -"
except:
print("Not able to set system Clock ")
#logger.error('Not able to set Hardware Clock')
return "- ERROR: not able to set system Clock -"
def settimezone(timezone):
os.environ['TZ'] = timezone
time.tzset()
return ""
def deltadatetimetoUTC():
timeUTC=time.gmtime()
timelocal=time.localtime()
newformat="%d %b %Y %H:%M:%S"
timestrUTC = time.strftime(newformat, timeUTC)
timestrLOC = time.strftime(newformat, timelocal)
datetimeUTC=datetime.strptime(timestrUTC,newformat)
datetimeLOC=datetime.strptime(timestrLOC,newformat)
delta = datetimeLOC-datetimeUTC
return delta
def convertLOCtoUTC(dtime_str):
dtime=datetime.strptime(dtime_str, DATEFORMAT)
delta=deltadatetimetoUTC()
UTCdtime=dtime-delta
UTCdtime_str=UTCdtime.strftime(DATEFORMAT)
return UTCdtime_str
def convertUTCtoLOC(dtime_str):
dtime=datetime.strptime(dtime_str, DATEFORMAT)
delta=deltadatetimetoUTC()
LOCdtime=dtime+delta
LOCdtime_str=LOCdtime.strftime(DATEFORMAT)
return LOCdtime_str
def convertLOCtoUTC_datetime(dtime):
delta=deltadatetimetoUTC()
UTCdtime=dtime-delta
return UTCdtime
def convertUTCtoLOC_datetime(dtime):
delta=deltadatetimetoUTC()
LOCdtime=dtime+delta
return LOCdtime
if __name__ == '__main__':
# comment
a=10