Upload Tizen:Base source
[framework/base/util-linux-ng.git] / tests / ts / hwclock / systohc
1 #!/bin/bash
2
3 #
4 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
5 #
6 # This file is part of util-linux-ng.
7 #
8 # This file is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License or
11 # (at your option) any later version.
12 #
13 # This file is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18
19 TS_TOPDIR="$(dirname $0)/../.."
20 TS_DESC="system to hw"
21 NTP_SERVER="0.fedora.pool.ntp.org"
22
23 . $TS_TOPDIR/functions.sh
24 ts_init "$*"
25 ts_skip_nonroot
26
27 [ -x "/usr/sbin/ntpdate" ] || ts_skip "cannot found ntpdate command"
28
29 set -o pipefail
30
31 # sync with server
32 (ntpdate $NTP_SERVER | sed "s/^.*offset \([0-9.]*\) sec/\1/g") &> /dev/null
33 [ "$?" == "1" ] && ts_skip "cannot sync with $NTP_SERVER"
34
35 # sync again and check difference
36 OFFSET=$( ntpdate $NTP_SERVER 2> /dev/null | sed "s/^.*offset [\-]*\([0-9.]*\) sec/\1/g" )
37 [ "$?" == "1" ] && ts_skip "cannot sync with $NTP_SERVER (2nd attempt)"
38
39 DIFF=$( echo "$OFFSET > 1" | bc )
40 [ "$DIFF" == "1" ] && ts_skip "diff between systime and NTP is greated than 1 second"
41
42 # call hwclock
43 for i in `seq 0 10`; do
44         #echo "sync #$i"
45         $TS_CMD_HWCLOCK --systohc
46         $TS_CMD_HWCLOCK --hctosys
47 done
48
49 # sync with NTP and check new difference
50 OFFSET=$( ntpdate $NTP_SERVER 2> /dev/null | sed "s/^.*offset [\-]*\([0-9.]*\) sec/\1/g" )
51 [ "$?" == "1" ] && ts_skip "cannot sync with $NTP_SERVER (3rd attempt)"
52
53 DIFF=$( echo "$OFFSET > 1" | bc )
54 [ "$DIFF" == "1" ] && ts_failed "offset is $OFFSET"
55
56 ts_ok "offset is $OFFSET"
57