3

I have a dual booted PC, and in order to have Windows and Linux agree on the time, I have set the hardware clock to localtime in linux as described here.

Now I want to make a script to check if my Linux setting is as I want it to be - for this I need to check if the hardware clock is saved in localtime or UTC. But the hwclock just give me the time in localtime, even if the hardware clock is in UTC.

So, how do I know if the hardware clock in linux is set to localtime or UTC ?

(Linux is Crunchbang 11 - Debian family, and Windows is 8.1)

0

2 Answers 2

1

You can use hwclock to show the results in localtime or utc.

Just use the corresponding flag for each case:

jim@g6:~$ sudo hwclock --localtime
lun 11 ago 2014 03:12:18 CLT  -0.547401 seconds

jim@g6:~$ sudo hwclock --utc
dom 10 ago 2014 23:12:22 CLT  -0.672848 seconds

Then run date and check which of the above results match the output from date.

1
  • I am now confused about how hwclock works. The man page states "The time shown is always in local time, even if you keep your Hardware Clock in Coordinated Universal Time." But when I tested it on my machine, hwclock matches hwclock --utc if I do hwclock --systohc --utc before, and the same happens replacing "utc" by "localtime". Aug 13, 2014 at 19:36
0

You can try the script below:

#!/bin/bash

curl time.nist.gov:13 2>&1|tee time_nist_gov_13
while cat time_nist_gov_13|grep 'Empty\|denied\|unreachable'
do
 sleep 0.1
 curl time.nist.gov:13 2>&1|tee time_nist_gov_13
done

str=$(cat time_nist_gov_13 | sed '1,3d'|awk  '{print $3}')

hh=$(echo $str|awk -F':' '{print $1}')
mm=$(echo $str|awk -F':' '{print $2}')

a=$(date -u | awk '{print $4 }'| awk -F':' '{print $1}') 
b=$(date -u | awk '{print $4 }'| awk -F':' '{print $2}')

mindiff=$(echo "$b-$mm"|bc)

if [ $mindiff == "-1" ] ; then $mindiff = "1"; 
fi

if [ "$hh" == "$a" ] && ([ "$mm" == "$b" ] || [ "$mindiff" == "1" ])   ; then echo "Your Local time is assigned to UTC";
 else echo "Your Local time is NOT assigned to UTC";
fi

An explanation as follows: the script gets UTC+0 time and compares it to date -u local time subtracted by time zone difference with reference to UTC+0, and if it is the same, that means your local time is correct for your time zone.

It will not always perfectly work because of the server can be unreachable sometimes and script will be waiting, and the script processes only tree kind of errors filtering by 'Empty\|denied\|unreachable'

So, once you got "Your Local time is tied to UTC" that's it, the local time is tied to UTC with the local time zone offset.

but anyway I wrote this script to give and idea of how it could be, and if you need it more serious, please include further details. Then anyone can see your requirements to the functionality. Hope it helps.

2
  • Actually, I need the script to check the timezone of the hardware clock, not the system clock (the one used by date). The given script checks the system clock against the NTP server. In just a side note, reading /dev/tcp/time.nist.gov/13 did not work on my machine, I replaced it with curl time.nist.gov:13 Aug 13, 2014 at 19:14
  • I a little bit improved the script, you can find it in the answer of course, but I didn't involve hwclock so far, not sure I'll be doing this. Aug 14, 2014 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .