Setting up to use have persistent date/time

Including script to set orin time

Change-Id: Idc5820ea6173af2deb97995e86af544a0befe0f3
Signed-off-by: Jim Ostrowski <yimmy13@gmail.com>
diff --git a/frc971/orin/build_rootfs.py b/frc971/orin/build_rootfs.py
index ea15cf8..92ff11c 100755
--- a/frc971/orin/build_rootfs.py
+++ b/frc971/orin/build_rootfs.py
@@ -1259,6 +1259,14 @@
         target(["systemctl", "enable", "frc971"])
         target(["systemctl", "enable", "frc971chrt"])
 
+        # Set up HW clock to use /dev/rtc0 and install hwclock service
+        target(["ln", "-sf", "/dev/rtc0", "/dev/rtc"])
+        target_unescaped(
+            "sed -i s/ATTR{hctosys}==\\\"1\\\"/ATTR{hctosys}==\\\"0\\\"/ /lib/udev/rules.d/50-udev-default.rules"
+        )
+        copyfile("root:root", "644", "etc/systemd/system/hwclock.service")
+        target(["systemctl", "enable", "hwclock"])
+
         target(["apt-file", "update"])
 
         target(["ldconfig"])
diff --git a/frc971/orin/contents/etc/systemd/system/hwclock.service b/frc971/orin/contents/etc/systemd/system/hwclock.service
new file mode 100644
index 0000000..40c446f
--- /dev/null
+++ b/frc971/orin/contents/etc/systemd/system/hwclock.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Hardware clock synchronization
+[Service]
+Type=oneshot
+ExecStart=/sbin/hwclock --hctosys --utc --noadjfile
+ExecStop=/sbin/hwclock --systohc --utc --noadjfile
+[Install]
+WantedBy=multi-user.target
diff --git a/frc971/orin/set_orin_clock.sh b/frc971/orin/set_orin_clock.sh
new file mode 100755
index 0000000..32edb86
--- /dev/null
+++ b/frc971/orin/set_orin_clock.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# Helper script to set orins clock based on our local timestamp
+
+set -e
+
+ROBOT_PREFIX="9" #71  (Should be one of 79, 89, 99, or 9)
+
+ORIN_LIST="1"
+
+echo "Setting hwclock on Orins"
+
+for orin in $ORIN_LIST; do
+    echo "========================================================"
+    echo "Setting clock for ${ROBOT_PREFIX}71.1${orin}"
+    echo "========================================================"
+    current_time=`sudo hwclock`
+    IFS="."
+    read -ra split_time <<< "$current_time"
+    echo "Setting time to ${split_time[0]}"
+    ssh pi@10.${ROBOT_PREFIX}.71.10${orin} "sudo timedatectl set-timezone US/Pacific"
+    ssh pi@10.${ROBOT_PREFIX}.71.10${orin} "sudo hwclock --set --date \"${split_time[0]}\""
+    ssh pi@10.${ROBOT_PREFIX}.71.10${orin} "sudo hwclock -s"
+done