WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Script to sync your computers time to Google  (Read 2663 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11702
Script to sync your computers time to Google
« on: November 11, 2020, 12:53:45 AM »
This script was initially inspired by this thread started by halma:
http://forum.tinycorelinux.net/index.php/topic,24319.0.html
I decided to switch from the cable modem to Google for a time source because it was much more stable.

The script script gets GMT time from Google, converts it to EST time, and compares it to local time. If the local time
is off by 3 seconds or more, it gets corrected. The time is checked once per hour and the results are optionally logged
to a file:
Code: [Select]
GoogleTime=Nov 06 04:24:47 2020   SystemTime=Nov 05 23:24:46 2020   0.006   The system clock is 1 second slow.
GoogleTime=Nov 06 05:00:21 2020   SystemTime=Nov 06 00:00:20 2020   0.017   The system clock is 1 second slow.
GoogleTime=Nov 06 06:00:20 2020   SystemTime=Nov 06 01:00:20 2020   0.019   The system clock is accurate.
GoogleTime=Nov 06 07:00:20 2020   SystemTime=Nov 06 02:00:20 2020   0.017   The system clock is accurate.
GoogleTime=Nov 06 08:00:20 2020   SystemTime=Nov 06 03:00:20 2020   0.014   The system clock is accurate.
GoogleTime=Nov 06 09:00:20 2020   SystemTime=Nov 06 04:00:20 2020   0.014   The system clock is accurate.
GoogleTime=Nov 06 10:00:20 2020   SystemTime=Nov 06 05:00:20 2020   0.007   The system clock is accurate.
GoogleTime=Nov 06 11:00:20 2020   SystemTime=Nov 06 06:00:20 2020   0.008   The system clock is accurate.
GoogleTime=Nov 06 12:00:20 2020   SystemTime=Nov 06 07:00:20 2020   0.015   The system clock is accurate.
GoogleTime=Nov 06 13:00:20 2020   SystemTime=Nov 06 08:00:20 2020   0.011   The system clock is accurate.
GoogleTime=Nov 06 14:00:20 2020   SystemTime=Nov 06 09:00:20 2020   0.012   The system clock is accurate.
GoogleTime=Nov 06 15:00:20 2020   SystemTime=Nov 06 10:00:20 2020   0.011   The system clock is accurate.
GoogleTime=Nov 06 16:00:20 2020   SystemTime=Nov 06 11:00:20 2020   0.010   The system clock is accurate.
GoogleTime=Nov 06 17:00:20 2020   SystemTime=Nov 06 12:00:20 2020   0.025   The system clock is accurate.
GoogleTime=Nov 06 18:00:20 2020   SystemTime=Nov 06 13:00:20 2020   0.010   The system clock is accurate.
GoogleTime=Nov 06 19:00:20 2020   SystemTime=Nov 06 14:00:20 2020   0.009   The system clock is accurate.
GoogleTime=Nov 06 20:00:20 2020   SystemTime=Nov 06 15:00:20 2020   0.012   The system clock is accurate.
GoogleTime=Nov 06 21:00:19 2020   SystemTime=Nov 06 16:00:20 2020   0.008   The system clock is 1 second fast.
GoogleTime=Nov 06 22:00:19 2020   SystemTime=Nov 06 17:00:20 2020   0.010   The system clock is 1 second fast.
GoogleTime=Nov 06 23:00:19 2020   SystemTime=Nov 06 18:00:20 2020   0.009   The system clock is 1 second fast.
GoogleTime=Nov 07 00:00:19 2020   SystemTime=Nov 06 19:00:20 2020   0.009   The system clock is 1 second fast.
GoogleTime=Nov 07 01:00:19 2020   SystemTime=Nov 06 20:00:20 2020   0.016   The system clock is 1 second fast.
GoogleTime=Nov 07 02:00:19 2020   SystemTime=Nov 06 21:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 07 03:00:19 2020   SystemTime=Nov 06 22:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 07 04:00:19 2020   SystemTime=Nov 06 23:00:20 2020   0.014   The system clock is 1 second fast.
GoogleTime=Nov 07 05:00:19 2020   SystemTime=Nov 07 00:00:20 2020   0.040   The system clock is 1 second fast.
GoogleTime=Nov 07 06:00:19 2020   SystemTime=Nov 07 01:00:20 2020   0.008   The system clock is 1 second fast.
GoogleTime=Nov 07 07:00:19 2020   SystemTime=Nov 07 02:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 07 08:00:19 2020   SystemTime=Nov 07 03:00:20 2020   0.014   The system clock is 1 second fast.
GoogleTime=Nov 07 09:00:19 2020   SystemTime=Nov 07 04:00:20 2020   0.015   The system clock is 1 second fast.
GoogleTime=Nov 07 10:00:19 2020   SystemTime=Nov 07 05:00:20 2020   0.008   The system clock is 1 second fast.
GoogleTime=Nov 07 11:00:18 2020   SystemTime=Nov 07 06:00:20 2020   0.014   The system clock is 2 seconds fast.
GoogleTime=Nov 07 12:00:18 2020   SystemTime=Nov 07 07:00:20 2020   0.012   The system clock is 2 seconds fast.
GoogleTime=Nov 07 13:00:18 2020   SystemTime=Nov 07 08:00:20 2020   0.007   The system clock is 2 seconds fast.
GoogleTime=Nov 07 14:00:18 2020   SystemTime=Nov 07 09:00:20 2020   0.037   The system clock is 2 seconds fast.
GoogleTime=Nov 07 15:00:18 2020   SystemTime=Nov 07 10:00:20 2020   0.008   The system clock is 2 seconds fast.
GoogleTime=Nov 07 16:00:18 2020   SystemTime=Nov 07 11:00:20 2020   0.015   The system clock is 2 seconds fast.
GoogleTime=Nov 07 17:00:18 2020   SystemTime=Nov 07 12:00:20 2020   0.007   The system clock is 2 seconds fast.
GoogleTime=Nov 07 18:00:18 2020   SystemTime=Nov 07 13:00:20 2020   0.006   The system clock is 2 seconds fast.
GoogleTime=Nov 07 19:00:18 2020   SystemTime=Nov 07 14:00:20 2020   0.015   The system clock is 2 seconds fast.
GoogleTime=Nov 07 20:00:18 2020   SystemTime=Nov 07 15:00:20 2020   0.015   The system clock is 2 seconds fast.
GoogleTime=Nov 07 21:00:18 2020   SystemTime=Nov 07 16:00:20 2020   0.015   The system clock is 2 seconds fast.
GoogleTime=Nov 07 22:00:18 2020   SystemTime=Nov 07 17:00:20 2020   0.014   The system clock is 2 seconds fast.
GoogleTime=Nov 07 23:00:18 2020   SystemTime=Nov 07 18:00:20 2020   0.011   The system clock is 2 seconds fast.
GoogleTime=Nov 08 00:00:18 2020   SystemTime=Nov 07 19:00:20 2020   0.011   The system clock is 2 seconds fast.
GoogleTime=Nov 08 01:00:17 2020   SystemTime=Nov 07 20:00:20 2020   0.013   The system clock is 3 seconds fast,  corrected.
GoogleTime=Nov 08 02:00:20 2020   SystemTime=Nov 07 21:00:20 2020   0.009   The system clock is accurate.
GoogleTime=Nov 08 03:00:20 2020   SystemTime=Nov 07 22:00:20 2020   0.040   The system clock is accurate.
GoogleTime=Nov 08 04:00:20 2020   SystemTime=Nov 07 23:00:20 2020   0.009   The system clock is accurate.
GoogleTime=Nov 08 05:00:19 2020   SystemTime=Nov 08 00:00:20 2020   0.014   The system clock is 1 second fast.
GoogleTime=Nov 08 06:00:19 2020   SystemTime=Nov 08 01:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 08 07:00:19 2020   SystemTime=Nov 08 02:00:20 2020   0.009   The system clock is 1 second fast.
GoogleTime=Nov 08 08:00:19 2020   SystemTime=Nov 08 03:00:20 2020   0.008   The system clock is 1 second fast.
GoogleTime=Nov 08 09:00:19 2020   SystemTime=Nov 08 04:00:20 2020   0.017   The system clock is 1 second fast.
GoogleTime=Nov 08 10:00:19 2020   SystemTime=Nov 08 05:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 08 11:00:19 2020   SystemTime=Nov 08 06:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 08 12:00:19 2020   SystemTime=Nov 08 07:00:20 2020   0.009   The system clock is 1 second fast.
GoogleTime=Nov 08 13:00:19 2020   SystemTime=Nov 08 08:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 08 14:00:19 2020   SystemTime=Nov 08 09:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 08 15:00:19 2020   SystemTime=Nov 08 10:00:20 2020   0.025   The system clock is 1 second fast.
GoogleTime=Nov 08 16:00:19 2020   SystemTime=Nov 08 11:00:20 2020   0.015   The system clock is 1 second fast.
GoogleTime=Nov 08 17:00:19 2020   SystemTime=Nov 08 12:00:20 2020   0.011   The system clock is 1 second fast.
GoogleTime=Nov 08 18:00:19 2020   SystemTime=Nov 08 13:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 08 19:00:18 2020   SystemTime=Nov 08 14:00:20 2020   0.007   The system clock is 2 seconds fast.
GoogleTime=Nov 08 20:00:18 2020   SystemTime=Nov 08 15:00:20 2020   0.011   The system clock is 2 seconds fast.
GoogleTime=Nov 08 21:00:18 2020   SystemTime=Nov 08 16:00:20 2020   0.010   The system clock is 2 seconds fast.
GoogleTime=Nov 08 22:00:18 2020   SystemTime=Nov 08 17:00:20 2020   0.016   The system clock is 2 seconds fast.
GoogleTime=Nov 08 23:00:18 2020   SystemTime=Nov 08 18:00:20 2020   0.015   The system clock is 2 seconds fast.
GoogleTime=Nov 09 00:00:18 2020   SystemTime=Nov 08 19:00:20 2020   0.013   The system clock is 2 seconds fast.
GoogleTime=Nov 09 01:00:18 2020   SystemTime=Nov 08 20:00:20 2020   0.011   The system clock is 2 seconds fast.
GoogleTime=Nov 09 02:00:18 2020   SystemTime=Nov 08 21:00:20 2020   0.025   The system clock is 2 seconds fast.
GoogleTime=Nov 09 03:00:18 2020   SystemTime=Nov 08 22:00:20 2020   0.008   The system clock is 2 seconds fast.
GoogleTime=Nov 09 04:00:18 2020   SystemTime=Nov 08 23:00:20 2020   0.014   The system clock is 2 seconds fast.
GoogleTime=Nov 09 05:00:18 2020   SystemTime=Nov 09 00:00:20 2020   0.008   The system clock is 2 seconds fast.
GoogleTime=Nov 09 06:00:18 2020   SystemTime=Nov 09 01:00:20 2020   0.007   The system clock is 2 seconds fast.
GoogleTime=Nov 09 07:00:18 2020   SystemTime=Nov 09 02:00:20 2020   0.010   The system clock is 2 seconds fast.
GoogleTime=Nov 09 08:00:18 2020   SystemTime=Nov 09 03:00:20 2020   0.007   The system clock is 2 seconds fast.
GoogleTime=Nov 09 09:00:17 2020   SystemTime=Nov 09 04:00:20 2020   0.016   The system clock is 3 seconds fast,  corrected.
GoogleTime=Nov 09 10:00:20 2020   SystemTime=Nov 09 05:00:20 2020   0.009   The system clock is accurate.
GoogleTime=Nov 09 11:00:20 2020   SystemTime=Nov 09 06:00:20 2020   0.014   The system clock is accurate.
GoogleTime=Nov 09 12:00:19 2020   SystemTime=Nov 09 07:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 09 13:00:19 2020   SystemTime=Nov 09 08:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 09 14:00:19 2020   SystemTime=Nov 09 09:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 09 15:00:19 2020   SystemTime=Nov 09 10:00:20 2020   0.052   The system clock is 1 second fast.
GoogleTime=Nov 09 16:00:19 2020   SystemTime=Nov 09 11:00:20 2020   0.041   The system clock is 1 second fast.
GoogleTime=Nov 09 17:00:19 2020   SystemTime=Nov 09 12:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 09 18:00:19 2020   SystemTime=Nov 09 13:00:20 2020   0.012   The system clock is 1 second fast.
GoogleTime=Nov 09 19:00:19 2020   SystemTime=Nov 09 14:00:20 2020   0.007   The system clock is 1 second fast.
GoogleTime=Nov 09 20:00:19 2020   SystemTime=Nov 09 15:00:20 2020   0.017   The system clock is 1 second fast.
GoogleTime=Nov 09 21:00:19 2020   SystemTime=Nov 09 16:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 09 22:00:19 2020   SystemTime=Nov 09 17:00:20 2020   0.010   The system clock is 1 second fast.
GoogleTime=Nov 09 23:00:19 2020   SystemTime=Nov 09 18:00:20 2020   0.014   The system clock is 1 second fast.
GoogleTime=Nov 10 00:00:19 2020   SystemTime=Nov 09 19:00:20 2020   0.009   The system clock is 1 second fast.
GoogleTime=Nov 10 01:00:19 2020   SystemTime=Nov 09 20:00:20 2020   0.013   The system clock is 1 second fast.
GoogleTime=Nov 10 02:00:18 2020   SystemTime=Nov 09 21:00:20 2020   0.011   The system clock is 2 seconds fast.
GoogleTime=Nov 10 03:00:18 2020   SystemTime=Nov 09 22:00:20 2020   0.010   The system clock is 2 seconds fast.
GoogleTime=Nov 10 04:00:18 2020   SystemTime=Nov 09 23:00:20 2020   0.016   The system clock is 2 seconds fast.
GoogleTime=Nov 10 05:00:18 2020   SystemTime=Nov 10 00:00:20 2020   0.012   The system clock is 2 seconds fast.
The column of numbers with the decimal point are the milliseconds part of SystemTime. The high level of repeatability
is achieved by using the GNU date command from coreutils.tcz which provides sub-second resolution coupled with a
~1 hour sleep command.

Aside from accurate sleep times, the script also shows how to pass variables by reference to a function, i.e.:
Code: [Select]
function "Result" "$value"so  function  does something with  value  and modifies the contents of  Result  directly.

The script is fairly well documented and easily customized. It assumes the clock is set to local time and converts
GMT to local time by faking the TZ code.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11702
Re: Script to sync your computers time to Google
« Reply #1 on: November 11, 2020, 12:30:49 PM »
A couple of other points I forgot to mention:

The modulo operator (%) is supported by  busybox ash (integer only)  and  busybox awk (floating point).
The script includes a nice one liner that limits the log file to the most recent 100 entries.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Script to sync your computers time to Google
« Reply #2 on: November 11, 2020, 04:28:29 PM »
Isn't this what NTP is for?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11702
Re: Script to sync your computers time to Google
« Reply #3 on: November 11, 2020, 04:42:20 PM »
Hi andyj
Sure, but this allowed me to learn a few things, brought me some enjoyment, and works well for my needs.