/*  TimeSaver.rexx  $VER: 1.00 (17-Jan-1998) 
    © 1998 Ralf Schönfeldt                   
    eMail: rschoenfeldt@t-online.de          
    WWW: http://privat.schlund.de/RalfsWorld/
                                             
    An ARexx-script to auto-adjust bad or    
    inaccurate battery clocks                
                                             
    requires (on 1st run only):              
    ReqTools and RexxReqTools-libraries    */

OPTIONS Results
OPTIONS FailAt 10000

PrefsFile='TimeSaver.prefs'	/* name of prefs-file */
IF ~EXISTS('ENV:'PrefsFile)	/* check for prefs-file */
	THEN DO	/* generate new prefs-file */
		IF ~SHOW('L','rexxreqtools.library') THEN IF ~AddLib('rexxreqtools.library',0,-30) THEN EXIT
		T_off=RTGetLong(,'Please enter daily offset in seconds' '0A'x '(e.g.2 if clock runs 2 seconds too slow' '0A'x ' or -3.5 if clock runs 3.5 seconds too fast)','TimeSaver.rexx',,'rtgl_min=-10 rtgl_max=10',ReqRes)
		IF ReqRes=0 THEN EXIT
		T_prf=T_off',0'
		CALL SavePrefs
		EXIT	/* nothing left to do */
	END

OPEN('Prefs','ENV:'PrefsFile,'R')	/* read prefs-file */
T_old=READLN('Prefs')	/* last saved date/time */
T_prf=READLN('Prefs')	/* offset/correction data */
CLOSE('Prefs')

TD_old=SUBSTR(T_old,1,3)	/* get old data */
TT_old=SUBSTR(T_old,4)
T_off=SUBSTR(T_prf,1,POS(',',T_prf)-1)
T_rem=SUBSTR(T_prf,POS(',',T_prf)+1)
TD=DATE('D')	/* read current date */
TT=TIME('S')	/* read current time */
TD_dif=TD-TD_old	/* calculate differences */
TT_dif=TT-TT_old
T_dif=TT_dif+TD_dif*86400	/* calculate seconds between last invoke */
off=T_rem+T_dif/86400*T_off	/* 86400=seconds per day */
IF ABS(off)<1 THEN EXIT	/* nothing to do, because timer is less than a second off */
DO FOREVER
	T=TIME()	/* read current time in standard format */
	T_H=SUBSTR(T,1,2)	/* hours only */
	T_M=SUBSTR(T,4,2)	/* minutes only */
	T_S=SUBSTR(T,7)+TRUNC(off)	/* seconds only plus calculated offset */
	IF T_S>59	/* second overflow */
		THEN DO
			T_M=T_M+1
			T_S=T_S-60
			IF T_M>60	/* minute overflow */
				THEN DO
					T_H=T_H+1
					T_M=0
					IF T_H=25 THEN CALL Error	/* hour overflow */
				END
		END
	IF T_S<0	/* second undercut */
		THEN DO
			T_M=T_M-1
			T_S=T_S+60
			IF T_M<0	/* minute undercut */
				THEN DO
					T_H=T_H-1
					T_M=0
					IF T_H=-1 THEN CALL Error	/* hour undercut */
				END
		END
	NewT=Fill(T_H,2)':'Fill(T_M,2)':'Fill(T_S,2)	/* new time in standard format */
	IF TIME()=T THEN LEAVE	/* did the correction take longer than 1 sec? */
END
ADDRESS COMMAND 'Date 'NewT	/* set new system time */
ADDRESS COMMAND 'Setclock Save'	/* save system time to battery clock */
T_rem=off-TRUNC(off)	/* calculate new correction data */
T_prf=T_off','T_rem
CALL SavePrefs
EXIT	/* seeya */

SavePrefs:	/* settings-file to ENV: and ENVARC: */
	Time=Fill(DATE('D'),3)Fill(TIME('S'),5)
	OPEN('Prefs','ENVARC:'PrefsFile,'W')	/* save prefs to ENVARC: */
	WRITELN('Prefs',Time)
	WRITELN('Prefs',T_prf)
	CLOSE('Prefs')
	OPEN('Prefs','ENV:'PrefsFile,'W')	/* save prefs to ENV: */
	WRITELN('Prefs',Time)
	WRITELN('Prefs',T_prf)
	CLOSE('Prefs')
RETURN

Error:
	SAY 'Please run this script again.'	/* no date handling implemented */
	EXIT

Fill: PROCEDURE	/* fill strings of length 'Len' with leading '0's */
PARSE ARG String,Len
	String=INSERT(String,'',Len-LENGTH(String),LENGTH(String),'0')
RETURN String
