WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Small enhancement of 'getTime.sh'  (Read 1827 times)

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Small enhancement of 'getTime.sh'
« on: April 22, 2010, 07:49:54 PM »
It might be worth to consider a small enhancement of 'getTime.sh': Like the '-p' option of 'hwclock' it would just print the UTC value (in ISO 8601 format) obtained from an external source, without changing the time. As the BusyBox 'date' applet accepts TIME also in ISO 8601 format the whole formatting of the update value could be simplified.

I've taken the liberty draft such an enhancement:
Code: [Select]
tc@box:~$ diff `which getTime.sh ` getTime.sh
--- /usr/bin/getTime.sh Fri Mar  5 10:35:05 2010
+++ getTime.sh  Fri Apr 23 09:40:52 2010
@@ -1,11 +1,12 @@
 #! /bin/sh
-version="1.00"
+version="1.01"
 scriptName="getTime.sh"
 #
 # This script gets time information from time.nist.gov in
-# universal time format. It then rearranges it into
-# the format that the date command uses MMDDhhmmCCYY.ss
-# and issues the date command as super user.
+# universal time format. It then rearranges it into ISO 8601
+# format that the busybox date command accepts (i.e. CCYY-MM-DD hh:mm:ss)
+# and issues the date command as super user, unless when just required to
+# print the time information
 #
 # An internet connection must be available
 #
@@ -13,11 +14,15 @@
 #54144 07-02-13 16:14:43 00 0 0  95.3 UTC(NIST) *
 #12345678901234567890123456789012345678901234567890
 #
-NIST="$1";[ -z "$NIST" ] && NIST="time-nw.nist.gov"
+[ "x$1" = "x-p" ] && shift && PRINT=1
+NIST="$1" ; [ -z "$NIST" ] && NIST="time-nw.nist.gov"
 R=$(nc "$NIST" 13 | grep "UTC(NIST)")
-if [ ! -z "$R" ]; then
-  S=$(echo "$R"|cut -c10-11,13-14,16-17,19-20)20$(echo "$R"|cut -c7-8)
-  S=$S.$(echo "$R"|cut -c22-23)
-  sudo date -u "$S"
+if [ -n "$R" ] ; then
+  S=20$( echo "$R" | cut -c 7-23 )
+  if [ -n "$PRINT" ] ; then
+    echo "${S}$( echo $R | cut -c 37-46 )"
+  else
+    sudo busybox date -u "$S"
+  fi
 fi
 date

Offline roberts

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: Small enhancement of 'getTime.sh'
« Reply #1 on: April 23, 2010, 12:46:47 PM »
OK. Accepted into base. Thanks!
10+ Years Contributing to Linux Open Source Projects.