aboutsummaryrefslogtreecommitdiff
path: root/package/base-files/files/etc/init.d/sysfixtime
blob: 93f792266aac2248ddb6fe54ccca7ca332f2d53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}