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)

2 Answers2

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.

jimm-cl
  • 1,929
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.