Skip to content

Set date and time(UTC) from string to RTC chip #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`";
echo "RTC `ds1302_get_utc`"; echo "SYS `date --utc +%FT%TZ`";
28 changes: 28 additions & 0 deletions scripts/ds1302_set_utc_from_string
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
scripts=[
'scripts/ds1302_get_utc',
'scripts/ds1302_set_utc'
'scripts/ds1302_set_utc',
'scripts/ds1302_set_utc_from_string'
]
)