#!/bin/ksh # @(#) Adjust time by minutes #..# systems: #..# sites: tag=${0##*/} tmp=/tmp/${tag}$$ rm -f $tmp usage="Usage: $tag {+|-}{m|s}" # set `getopt -- vxh $*` while [ $# -gt 0 ]; do case "$1" in -v|-x) set "$1" ;; -vi) vi $0; exit ;; -about) echo "$tag was written by David Ledger" echo "dledger@ivdcs.demon.co.uk" exit ;; -h) echo "$usage" echo "" echo "$tag adjusts the system time by minutes or seconds." echo "" echo "If the time adjustment is by more then 5 mins, the new" echo "date/time is calculated and set." echo "If the adjustment is specified in minutes for less than" echo "5 min, $tag sleeps till the next minute end before" echo "applying the setting in order to maintain the seconds." echo "" echo "If the adjustment is in seconds, this cannot be done" echo "directly, and so $tag sleeps until a time when zeroing" echo "the seconds will have the desired effect. The minutes" echo "are adjusted accordingly." echo "" echo "Requests to perform seconds adjustments by more than 59" echo "seconds are rejected." echo "" exit ;; [+-][1-9]*m) adj=`expr "$1" : '\([+-][1-9][0-9]*\)m'` || { echo "$usage" echo "?? $1 ??" exit; } mode=mins;; [+-][1-9]*s) adj=`expr "$1" : '\([+-][1-9][0-9]*\)s'` || { echo "$usage" echo "?? $1 ??" exit; } [ $adj -gt 59 ] && { print -u2 "$tag [+|-]m for = 60 secs" exit; } mode=secs;; *) ;; esac shift done [ ${adj-0} -eq 0 ] && { echo "$usage"; exit; } set -A mn X Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec set -A dt `date -u | tr "[:]" "[ ]"` # Wed Dec 3 09 53 11 GMT 1997 print -u2 ${dt[*]} # mmddhhmm[yy] case "${dt[1]}" in Jan)mn=1;; Feb)mn=2;; Mar)mn=3;; Apr)mn=4;; May)mn=5;; Jun)mn=6;; Jul)mn=7;; Aug)mn=8;; Sep)mn=9;; Oct)mn=10;; Nov)mn=11;; Dec)mn=12;; *)exit;; esac normalise() { while [ ${dt[4]} -gt 59 ]; do dt[4]=$((dt[4] - 60)) dt[3]=$((dt[3] + 1)) while [ ${dt[3]} -gt 23 ]; do dt[3]=$((dt[3] - 24)) dt[2]=$((dt[2] + 1)) [ ${dt[2]} -gt 28 ] && { print -u2 "Too near midnight near a possible month end." print -u2 "Try again later." exit 1 } done done while [ ${dt[4]} -lt 0 ]; do dt[4]=$((dt[4] + 60)) dt[3]=$((dt[3] - 1)) while [ ${dt[3]} -lt 0 ]; do dt[3]=$((dt[3] + 24)) dt[2]=$((dt[2] - 1)) [ ${dt[2]} -lt 1 ] && { print -u2 "Too near midnight near a month start." print -u2 "Try again later." exit 1 } done done echo xxx $mn ${dt[2]} ${dt[3]} ${dt[4]} } fmt() { mn=$((mn + 0)) [ $mn -le 9 ] && mn="0$mn" dt[2]=$((dt[2] + 0)) [ ${dt[2]} -le 9 ] && dt[2]="0${dt[2]}" dt[3]=$((dt[3] + 0)) [ ${dt[3]} -le 9 ] && dt[3]="0${dt[3]}" dt[4]=$((dt[4] + 0)) [ ${dt[4]} -le 9 ] && dt[4]="0${dt[4]}" echo xxx $mn ${dt[2]} ${dt[3]} ${dt[4]} } if [ "$mode" = mins ]; then dt[4]=$((dt[4] + adj)) if [ $adj -lt 5 -a $adj -gt -5 ]; then secsleft=$((60 - dt[5])) print -u2 "Sleeping $secsleft secs to maintain seconds" sleep $secsleft # Regenerate dt array in case we slipped into next minute set -A dt `date -u | tr "[:]" "[ ]"` dt[4]=$((dt[4] + adj)) fi elif [ "$mode" = secs ]; then print -u2 "Seconds can only be set to zero, so this will sleep until" print -u2 "zeroing seconds can have the requested effect." if [ $adj -gt 0 ]; then secsleft=$((60 - dt[5])) if [ $secsleft -gt $adj ]; then print -u2 "Sleeping $((secsleft -adj))" sleep $((secsleft - adj)) dt[4]=$((dt[4] + 1)) else print -u2 "Sleeping $((60 + secsleft -adj))" sleep $((60 - adj + secsleft)) dt[4]=$((dt[4] + 2)) fi set -A dt `normalise` || exit set -A dt `fmt` print -u2 "Setting time to:" print -u2 "xxx ${mn[$mn]} ${dt[2]} ${dt[3]} ${dt[4]} 00 UTC ${dt[7]}" print -u2 "With command:" print -u2 "date -u ${mn}${dt[2]}${dt[3]}${dt[4]}" print -u2 "at (by old time):" date date -u ${mn}${dt[2]}${dt[3]}${dt[4]} else adj=$((- adj)) if [ ${dt[5]} -le $adj ]; then print -u2 "Sleeping $((adj - dt[5]))" sleep $((adj - dt[5])) # Regenerate dt array as we moved into next minute set -A dt `date -u | tr "[:]" "[ ]"` else print -u2 "Sleeping $((60 + adj - dt[5]))" sleep $((60 + adj - dt[5])) # Regenerate dt array as we moved into next minute set -A dt `date -u | tr "[:]" "[ ]"` fi set -A dt `normalise` || exit set -A dt `fmt` print -u2 "Setting time to:" print -u2 "xxx ${mn[$mn]} ${dt[2]} ${dt[3]} ${dt[4]} 00 UTC ${dt[7]}" print -u2 "With command:" print -u2 "date -u ${mn}${dt[2]}${dt[3]}${dt[4]}" print -u2 "at (by old time):" date echo "yes" | date -u ${mn}${dt[2]}${dt[3]}${dt[4]} fi fi print -u2 "Time is now:" date | |