8-Bit Software Online Conversion

Time and Date functions ======================= By J.G.Harston (W79) I present here a few handy time and date handling functions that are listed in my HADFS Reference Manual but are useful enough by themselves to be made available to a wider audience. The functions are in the program Times which also gives a brief demonstration of their usage. The Master computers have a real-time-clock (RTC) readable with TIME$ which calls Osword 14. Some BBC B computers also have a RTC available, but as Basic II doesn't have TIME$, it cannot be used, and some 6502 second processors lose the terminating byte. The function FNtime returns the RTC time string by calling Osword 14 directly and is usable on any machine. It returns a null string if no clock is available. REM Return Real-Time-Clock string, or null if no clock available DEFFNtime:LOCAL A%,X%,Y%:X%=ctrl%:Y%=X%DIV256:A%=14:!X%=0:CALL &FFF1 IF?X%:X%?24=13:=$X% ELSE ="" The function FNdate returns the date as a four-byte number as &yyymmdd where yyyy, mm and dd are the year, month and date. Osword 14 returns these values in Binary Coded Decimal and FNdate converts these to more easily usable integers. If the returned year if 50 or more, it is returned as 1950 to 1999, otherwise it is returned as 2000 to 2049, so allowing years to span the end of the century. REM Return Real-Time-Clock data, or zero if no clock available DEFFNdate:LOCAL A%,X%,Y%:X%=ctrl%:Y%=X%DIV256:A%=14:!X%=1:CALL &FFF1 =VALSTR$~X%?3+256*VALSTR$~X%?2+65536*(1900+VALSTR$~?X%-100*(?X%<&50)) The following construction will get the date, month and year into separate variables d%, m% and y%: d%=FNdate:y%=(d%AND&FFFF0000)DIV65536 m%=(d%AND&FF00)DIV256:d%=d%AND&FF The function FNDayOfWeek calculates the day of the week that the supplied date falls on. REM Return day of week, 1=Sunday, 7=Saturday DEFFNDayOfWeek(d%,m%,y%):IFy%<100:y%=y%+1900 y%=y%MOD400 =(y%*365.25+m%*30+d%+VALMID$("120112234455",m%,1)+((y%MOD4)=0)-((y%-1)DIV100)-(m %>2AND((y%MOD4)=0AND(y%MOD100)<>0ORy%=0))+3)MOD7+1 To convert the day to a three-character string, the following construct is very useful: day%=FNDayOfWeek(???, ???, ???) day$=MID$("SunMonTueWedThuFriSat",day%*3-2,3) This also works for the months of the year: month$=MID$("JanFebMarAprMayJunJulAugSepOctNovDec",m%*3-2,3) The procedure PROCConvDate converts a given date and time to the centi-second count since midnight on 1/1/1900 that Acorn uses for measuring time. REM Convert time and date to 5-byte centi-second count since 1st Jan 1900 DEFPROCConvDate(mem%,d%,m%,y%,hr%,mn%,sc%,cs%):IFy%<100:y%=y%+1900 y%=y%MOD400 d%=y%*365.25+m%*30+d%+VALMID$("120112234455",m%,1)+((y%MOD4)=0)-((y%-1)DIV100)-( m%>2AND((y%MOD4)=0AND(y%MOD100)<>0ORy%=0))+36493 IFd%>146096:d%=d%-146097 d%=d%*&41EB:mem%!1=d%+d%:d%=((hr%*60+mn%)*60+sc%)*100+cs% ?mem%=d%:mem%!1=mem%!1+d%DIV256:ENDPROC The function FNDaysSince uses PROCConvDate to calculate the number of days between two events. After writing this I discovered that I had just missed the 10,000th daily anniversary of my birth by a couple of months! Oh, well, just another 28 or so years to day number 20,000. It is called with today's (or any other day's) date in td%, tm%, ty% and the past date in pd%, pm% and py%. REM Return number of days since a past date DEFFNDaysSince(td%,tm%,ty%,pd%,pm%,py%):LOCAL past% PROCConvDate(ctrl%,pd%,pm%,py%,0,0,0,0):past%=ctrl%!1 PROCConvDate(ctrl%,td%,tm%,ty%,0,0,0,0):=(ctrl%!1-past%)DIV&83D6 : :