Time equation questions
From an earlier thread I have applied some knowledge from Mike, Phil & Jonny Mac.
I am playing around with time equations. I recieve time of day from a RTC object in a string buffer. I was looking at a way of converting that string buffer back into a number and then being able to do some calculations with it like subtracting one time from another to figure the difference and be able to display it.
Here's my playground of code:
I take 2 different time buffers that were stored as ASCII strings, convert them to decimal number, break them apart (parse?) to get the hour, minutes and seconds. Then convert the hours and minutes to seconds. Then add all the seconds together. I do this for both strings and then find the difference in seconds then go and convert the difference in seconds back into hours, minutes and seconds.
Seems like I took the long way around to get to the answer.
My question- is there a simpler / easier way to take 2 ASCII time strings and find the difference in hours, minute & seconds?
Thanks.
Don
I am playing around with time equations. I recieve time of day from a RTC object in a string buffer. I was looking at a way of converting that string buffer back into a number and then being able to do some calculations with it like subtracting one time from another to figure the difference and be able to display it.
Here's my playground of code:
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
MS_001 = 80_000_000 / 1_000
VAR
long time, seconds, TimeBuffer1, TimeBuffer2
byte TimeString1[10], TimeString2[10]
OBJ
term : "fullduplexserial"
PUB main | hours, minutes, secs, seconds1, seconds2, totalsecs1, totalsecs2, diffseconds
term.start(31, 30, %0000, 115_200)
pause(1000)
seconds := 122
time := time + seconds
minutes := time / 60
secs := time // 60
term.dec(time)
term.str(string(" Seconds = "))
term.dec(minutes)
term.str(string(" Minutes and "))
term.dec(secs)
term.str(string(" Seconds",13, 13))
TimeString1[0] := "1" ' Assign ASCII string numbers to buffer
TimeString1[1] := "2"
TimeString1[2] := ":"
TimeString1[3] := "0"
TimeString1[4] := "5"
TimeString1[5] := ":"
TimeString1[6] := "3"
TimeString1[7] := "0"
TimeString1[8] := 0
TimeString2[0] := "1"
TimeString2[1] := "1"
TimeString2[2] := ":"
TimeString2[3] := "0"
TimeString2[4] := "8"
TimeString2[5] := ":"
TimeString2[6] := "5"
TimeString2[7] := "5"
TimeString2[8] := 0
term.str(@TimeString1) ' Display ASCII strings
term.tx(32)
term.str(@TimeString2)
term.tx(13)
TimeBuffer1 := (StrToDec(@TimeString1)) ' Convert ASCII number strings to decimal number
TimeBuffer2 := (StrToDec(@TimeString2))
term.dec(StrToDec(@TimeString1)) ' Display decimal number
term.tx(32)
term.dec(StrToDec(@TimeString2))
term.tx(13)
term.dec(TimeBuffer1 // 10) ' Play wih Modulus function to see what it does
term.tx(" ")
term.dec(TimeBuffer1 // 100)
term.tx(" ")
term.dec(TimeBuffer1 // 1000)
term.tx(" ")
term.dec(TimeBuffer1 // 10000)
term.tx(" ")
term.dec(TimeBuffer1 // 100000)
term.tx(" ")
term.dec(TimeBuffer1 // 1000000)
term.tx(" ")
term.dec(TimeBuffer1 // 10000000)
term.tx(13)
term.dec(TimeBuffer1 / 10000) ' Divide number by 10,000 to get hours
term.tx(13)
seconds1 := (TimeBuffer1 // 100) ' Seconds
seconds1 := seconds1 + ((((TimeBuffer1 // 10000) - seconds1) / 100) * 60) ' Minutes converted to seconds
seconds1 := seconds1 + ((TimeBuffer1 / 10000) * 3600) ' Hours converted to seconds
seconds2 := (TimeBuffer2 // 100) ' Seconds
seconds2 := seconds2 + ((((TimeBuffer2 // 10000) - seconds2) / 100) * 60) ' Minutes converted to seconds
seconds2 := seconds2 + ((TimeBuffer2 / 10000) * 3600) ' Hours converted to seconds
term.dec(seconds1)
term.tx(32)
term.dec(seconds2)
term.tx(13)
diffseconds := seconds1 - seconds2 ' Figure time difference in seconds
term.dec(diffseconds)
term.tx(13)
hours := diffseconds / 3600 ' Convert seconds difference into Hours, Minutes and Seconds
minutes := diffseconds / 60
secs := diffseconds // 60
term.dec(hours)
term.str(string(" Hours, "))
term.dec(minutes)
term.str(string(" Minutes and "))
term.dec(secs)
term.str(string(" Seconds",13, 13))
PUB StrToDec(stringptr) : value | char, index, multiply
'' Converts a zero terminated string representation of a decimal number to a value
value := index := 0
repeat until ((char := byte[stringptr][index++]) == 0)
if char => "0" and char =< "9"
value := value * 10 + (char - "0")
if byte[stringptr] == "-"
value := - value
pub pause(ms) | t
t := cnt
repeat ms
waitcnt(t += MS_001)
I take 2 different time buffers that were stored as ASCII strings, convert them to decimal number, break them apart (parse?) to get the hour, minutes and seconds. Then convert the hours and minutes to seconds. Then add all the seconds together. I do this for both strings and then find the difference in seconds then go and convert the difference in seconds back into hours, minutes and seconds.
Seems like I took the long way around to get to the answer.
My question- is there a simpler / easier way to take 2 ASCII time strings and find the difference in hours, minute & seconds?
Thanks.
Don

Comments
Here's another method for converting seconds to a string -- it doesn't add a terminator so you can insert the formatted time anywhere you'd like.
Thanks again.
Don