if the user edits the computer system date time, how can it be checked against the date and time zone with code python. Thanks all!
Asked
Active
Viewed 268 times
0
-
You can connect to a network time server, and compare its time with the system time. – Barmar Jun 14 '21 at 18:13
1 Answers
0
Python use the system datetime.
import datetime
x = datetime.datetime.now()
print(x)
Run this and then run it after you turn you system clock back one hour.
This will print an NTP server time followed by the system time. Can be modified to compare the two.
import socket
import struct
import sys
import time
import datetime
def RequestTimefromNtp(addr='0.de.pool.ntp.org'):
REF_TIME_1970 = 2208988800 # Reference time
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = b'\x1b' + 47 * b'\0'
client.sendto(data, (addr, 123))
data, address = client.recvfrom(1024)
if data:
t = struct.unpack('!12I', data)[10]
t -= REF_TIME_1970
return time.ctime(t), t
if __name__ == "__main__":
print(RequestTimefromNtp())
x = datetime.datetime.now()
print(x)

stefan_aus_hannover
- 1,777
- 12
- 13