ASR_BASE
Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/package/base-files/files/etc/init.d/sysfixtime b/package/base-files/files/etc/init.d/sysfixtime
new file mode 100755
index 0000000..93f7922
--- /dev/null
+++ b/package/base-files/files/etc/init.d/sysfixtime
@@ -0,0 +1,44 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2013-2014 OpenWrt.org
+
+START=00
+STOP=90
+
+RTC_DEV=/dev/rtc0
+HWCLOCK=/sbin/hwclock
+
+boot() {
+ hwclock_load
+ local maxtime="$(find_max_time)"
+ local curtime="$(date +%s)"
+ if [ $curtime -lt $maxtime ]; then
+ date -s @$maxtime
+ hwclock_save
+ fi
+}
+
+start() {
+ hwclock_load
+}
+
+stop() {
+ hwclock_save
+}
+
+hwclock_load() {
+ [ -e "$RTC_DEV" ] && [ -e "$HWCLOCK" ] && $HWCLOCK -s -u -f $RTC_DEV
+}
+
+hwclock_save(){
+ [ -e "$RTC_DEV" ] && [ -e "$HWCLOCK" ] && $HWCLOCK -w -u -f $RTC_DEV && \
+ logger -t sysfixtime "saved '$(date)' to $RTC_DEV"
+}
+
+find_max_time() {
+ local file newest
+
+ for file in $( find /etc -type f ) ; do
+ [ -z "$newest" -o "$newest" -ot "$file" ] && newest=$file
+ done
+ [ "$newest" ] && date -r "$newest" +%s
+}