File: set-time

package info (click to toggle)
xdialog 2.0.6-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,616 kB
  • ctags: 1,064
  • sloc: ansic: 8,505; sh: 4,202; yacc: 318; makefile: 80
file content (85 lines) | stat: -rwxr-xr-x 2,257 bytes parent folder | download | duplicates (4)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh

# A small script used to set the time under Linux with hwclock...

# Title to be used for all Xdialog boxes.
TITLE="Set time tool"

# Function used to abort the script.
function abort() {
	Xdialog --title "$TITLE" --msgbox "Aborted." 0 0
	exit 0
}


# Now check for hwclock existence...
if ! [ -f /sbin/hwclock ] ; then
	Xdialog --title "$TITLE" --msgbox "/sbin/hwclock not found..." 0 0
	exit 0
fi

# Search for adjtime IOT to know if the RTC was last set in UTC.
# If adjtime is not found then look at the /etc/sysconfig/clock
# file and in last ressort, if nothing can be deduced automatically,
# then ask the user...
if [ -f /etc/adjtime ] ; then
	UTC=`grep UTC /etc/adjtime`
	if [ "$UTC" == "UTC" ] ; then
		UTC="--utc"
	fi
else
	if [ -f /etc/sysconfig/clock ] ; then
		. /etc/sysconfig/clock
		if [ "$UTC" == "no" ] || [ "$UTC" == "false" ] ; then
			UTC=""
		else
			UTC="--utc"
		fi
	else
		Xdialog --title "$TITLE" --yesno "Is the RTC set in UTC ?" 0 0
		case $? in
			0)
				UTC="--utc" ;;
			1)
				UTC="" ;;
			255)
				abort ;;
		esac
	fi
fi

# Get the date (returned in DD/MM/YYYY format by Xdialog.
ENTEREDDATE=`Xdialog --stdout --title "$TITLE" --calendar "Please set the date..." 0 0 0 0 0`
if (( $? != 0 )) ; then
	abort
fi

# Convert the date to the MM/DD/YYYY format needed by hwclock.
NEWDATE=`echo "$ENTEREDDATE" | awk --source 'BEGIN { FS="/" }' --source '{ print $2 "/" $1 "/" $3 }'`

# Get the time in HH:MM:SS format.
NEWTIME=`Xdialog --stdout --title "$TITLE" --timebox "Please set the time..." 0 0`
if (( $? != 0 )) ; then
	abort
fi

# Prepare the error log file.
echo "Error while trying to set the system clock !" >/tmp/set-time.err.$$
echo "Reason:" >>/tmp/set-time.err.$$
echo "" >>/tmp/set-time.err.$$

# Set the hardware clock (RTC) and then the system clock, appending any error
# message to the error log...
/sbin/hwclock --set $UTC --date "$NEWDATE $NEWTIME" 2>>/tmp/set-time.err.$$
if (( $? == 0 )) ; then
	/sbin/hwclock --hctosys $UTC 2>>/tmp/set-time.err.$$
fi

# Give the result (success or failure+reason)...
if (( $? == 0 )) ; then
	Xdialog --title "$TITLE" --msgbox "The system clock has been set." 0 0
else
	Xdialog --title "$TITLE" --textbox /tmp/set-time.err.$$ 60 30
fi

rm -f /tmp/set-time.err.$$