diff --git a/README.md b/README.md index 64e3eb2..8724322 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ Simple Python module to deal with DS1302 RTC on Raspberry Pi ds1302_set_utc +### Set date and time(UTC) from string to RTC chip + + ds1302_set_utc_from_string "09/19/18 13:55:26" + ### Update Rpi system time from RTC chip Typically call at RPi startup @@ -39,4 +43,4 @@ Since RTC store only second and not millisecond a 1s delta can occur (or more af # drift in second echo $(($(date -u -d`ds1302_get_utc` +%s) - $(date -u +%s))) # human readable - echo "RTC `ds1302_get_utc`"; echo "SYS `date --utc +%FT%TZ`"; \ No newline at end of file + echo "RTC `ds1302_get_utc`"; echo "SYS `date --utc +%FT%TZ`"; diff --git a/scripts/ds1302_set_utc_from_string b/scripts/ds1302_set_utc_from_string new file mode 100755 index 0000000..c98528f --- /dev/null +++ b/scripts/ds1302_set_utc_from_string @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# read date and time from RTC chip, return ISO 8601 UTC string +# assume DS1302 contain UTC time and not local + +import sys +import pyRPiRTC +import sys +from datetime import datetime + +rtc = pyRPiRTC.DS1302(clk_pin=11, data_pin=13, ce_pin=15) +try: + # write date and time from system to RTC chip (in UTC) + if len(sys.argv)< 2: + exit("use: ds1302_set_utc_from_string '09/19/18 13:55:26' ") + dt_write = datetime.strptime(sys.argv[1], '%m/%d/%y %H:%M:%S') + # update rtc + rtc.write_datetime(dt_write) + # check update is good + dt_read = rtc.read_datetime() + if -2 < (dt_write - dt_read).total_seconds() < +2: + print(dt_write.strftime('%Y-%m-%dT%H:%M:%SZ')) + else: + exit('unable to set RTC time') +except ValueError: + sys.exit('error with RTC chip, check wiring') +finally: + rtc.close() diff --git a/setup.py b/setup.py index 5bf622a..4a0c024 100755 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ ], scripts=[ 'scripts/ds1302_get_utc', - 'scripts/ds1302_set_utc' + 'scripts/ds1302_set_utc', + 'scripts/ds1302_set_utc_from_string' ] )