aab5b153d0714fdead6fa70b3f6347dacbbead74
[openwrt/staging/wigyori.git] / package / base-files / files / etc / init.d / sysfixtime
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2013-2014 OpenWrt.org
3
4 START=00
5 STOP=90
6
7 RTC_DEV=/dev/rtc0
8 HWCLOCK=/sbin/hwclock
9
10 boot() {
11 start && exit 0
12
13 local maxtime="$(maxtime)"
14 local curtime="$(date +%s)"
15 [ $curtime -lt $maxtime ] && date -s @$maxtime
16 }
17
18 start() {
19 [ -e "$RTC_DEV" ] && [ -e "$HWCLOCK" ] && $HWCLOCK -s -u -f $RTC_DEV
20 }
21
22 stop() {
23 [ -e "$RTC_DEV" ] && [ -e "$HWCLOCK" ] && $HWCLOCK -w -u -f $RTC_DEV && \
24 logger -t sysfixtime "saved '$(date)' to $RTC_DEV"
25 }
26
27 maxtime() {
28 local file newest
29
30 for file in $( find /etc -type f ) ; do
31 [ -z "$newest" -o "$newest" -ot "$file" ] && newest=$file
32 done
33 [ "$newest" ] && date -r "$newest" +%s
34 }