WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Suggestion: Use of a different DAYTIME server  (Read 1719 times)

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Suggestion: Use of a different DAYTIME server
« on: September 20, 2011, 06:38:33 PM »
The recent change (in v3.8 ) of the hard-coded DAYTIME server in '/usr/bin/getTime.sh' does not seem to have been a great improvement. At least considering my own experience of the last few days. Clearly vitex would be right to claim "I told you so".

Whilst I had stated in the same thread that a single user experience might not be applicable to everybody else, I also believe that a single hard coded value is less then ideal. After checking the list again I'd like to suggest the following patch:
Code: (bash) [Select]
--- /usr/bin/getTime.sh
+++ getTime.sh
@@ -17,4 +17,7 @@
 [ "x$1" = "x-p" ] && shift && PRINT=1
-NIST="$1";[ -z "$NIST" ] && NIST="time.nist.gov"
-R=$(nc "$NIST" 13 | grep "UTC(NIST)")
+NIST="$1"
+USTIMING_SERVERS="atl chi sj"   # the whole list: "atl chi la lv nj ny pa sj"
+[ -z "$NIST" ] && NIST="nist1-$( echo "$USTIMING_SERVERS" \
+    | awk '{ srand(); n = int(rand() * NF) + 1; print $n }' ).ustiming.org"
+R=$( nc -w 3 "$NIST" 13 | grep "UTC(NIST)")
 if [ -n "$R" ] ; then
@@ -26,3 +29,6 @@
   fi
+  date
+else
+  echo "no response from $NIST" > /dev/stderr
+  exit 1
 fi
-date

It tries to address the issue by: (1) picking randomly one out of several servers from the US Time Server Foundation, (2) specifying a certain wait time (i.e. 3 seconds), (3) having a defined response (i.e. an error message and an 'exit') when not having received an answer, and (4) only showing the current time stamp when a reply was received.

Please note that I've limited the list of servers to only the three (i.e. Atlanta, Chicago and San Jose) from which I managed to get DAYTIME responses. But if other users are getting a different result I'd suggest to use a fuller list (see the comment in the same line). At least point (3) and (4) would allow the user to now be clear about the success or failure of the script execution.


EDIT: Minor correction by dropping the superfluous parameter in the 'rand()' function.
« Last Edit: September 21, 2011, 05:41:09 PM by maro »

Offline roberts

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: Suggestion: Use of a different DAYTIME server
« Reply #1 on: September 20, 2011, 08:48:47 PM »
The existing script has from the beginning accepted an argument of any desired time server.
Therefore I hardly think it is deserving of a "I toid you so", especially with all the CLI commandos that frequent this forum. Nonetheless, patch accepted. Thanks.
10+ Years Contributing to Linux Open Source Projects.

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: Suggestion: Use of a different DAYTIME server
« Reply #2 on: September 21, 2011, 05:44:43 PM »
As vitex was kind enough in a PM to point out that the 'rand()' function was using a superfluous parameter I've now done that a minor adjustment to the OP. To limit future potential confusion it might be worth applying this ever so slightly modified patch.