Created
October 1, 2020 02:44
-
-
Save docprofsky/68fa506d6d99589e790b095130848de2 to your computer and use it in GitHub Desktop.
Script to set the time on the alpha clock five over serial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import datetime, serial, sys | |
if len(sys.argv) != 2: | |
print(f'''Usage: {sys.argv[0]} <serial port>\n | |
This may reset the clock when opening the serial port. | |
example: {sys.argv[0]} /dev/ttyUSB0 | |
will set the time on an Alpha Clock Five connected to /dev/ttyUSB0''') | |
exit(1) | |
# figure out our current offset from UTC | |
# Using the current Unix timestamp for UTC, we find the time of UTC and of | |
# our local time. Using the difference in these times, we get our current | |
# timezone's offset. | |
# We use the current time (instead of 1 Jan 1970) for finding this offset, | |
# because DST varies with time. | |
# `import time; time.timezone` does not work because it ignores DST. | |
timestamp = datetime.datetime.now().timestamp() | |
utc_time = datetime.datetime.utcfromtimestamp(timestamp) | |
local_time = datetime.datetime.fromtimestamp(timestamp) | |
local_time_utc_offset = local_time - utc_time | |
ser = serial.Serial(sys.argv[1], 19200) | |
local_timestamp = (datetime.datetime.now() + local_time_utc_offset).timestamp() | |
ser.write(b'\xffST' + bytes(str(int(local_timestamp)), 'ascii')) | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment