Tasp
Full Member
Posts: 215
|
Post by Tasp on Nov 15, 2020 11:50:43 GMT -5
I'm trying to to print time as HH:MM:SS.mm
Obviously PRINT TIME$() does HH:MM:SS
So PRINT TIME$("milliseconds"), only gives milliseconds since midnight, which I've always found strange but it does what it does for a reason no doubt.
My initial solution was;for a = 1 to 10 ms = TIME$("ms") print ((ms *1000) / 60) / 60 next
Which I think is giving me ms after the decimal point? I would prefer 2 decimal places. But am I going about this all wrong?
I will add Maths equations to the ever growing list of BASIC that I don't understand!
|
|
|
Post by tsh73 on Nov 15, 2020 13:10:46 GMT -5
Creative misuse of strings (and yes it truncates milliseconds string to 2 digits after dot, not rounds it to 1/100) print time$();":";right$(str$(1000+time$("ms")mod 1000),3) print time$();":";mid$(str$(1000+time$("ms")mod 1000),2,2)
t0=time$("ms") timer 16, [tick] wait
[tick] if time$("ms")-t0>1000 then timer 0: end print time$();":";right$(str$(1000+time$("ms")mod 1000),3) print time$();":";mid$(str$(1000+time$("ms")mod 1000),2,2) wait
|
|
|
Post by Carl Gundel on Nov 15, 2020 14:50:16 GMT -5
I'm trying to to print time as HH:MM:SS.mm Obviously PRINT TIME$() does HH:MM:SS So PRINT TIME$("milliseconds"), only gives milliseconds since midnight, which I've always found strange but it does what it does for a reason no doubt. My initial solution was; for a = 1 to 10 ms = TIME$("ms") print ((ms *1000) / 60) / 60 next
Which I think is giving me ms after the decimal point? I would prefer 2 decimal places. But am I going about this all wrong? I will add Maths equations to the ever growing list of BASIC that I don't understand! Milliseconds since midnight is very useful. You can use it to measure the passage of time in your programs. You do have to test for overflow at midnight if it matters to your application. Here is an example of hh:mm:ss.ms that computes from a single reading of the clock so that you will not have the seemingly random illogical results at the boundaries: timer 100, [loop] initialMS = time$("ms") wait
[loop] timeMS = time$("ms") timeSecs = int(timeMS/1000) hrs = int(timeSecs/3600) mins = int((timeSecs - hrs * 3600) / 60) secs = timeSecs - hrs * 3600 - mins * 60 ms = timeMS - timeSecs * 1000
time$ = rjust$(hrs)+":"+rjust$(mins)+":"+rjust$(secs)+".";ms print time$ if timeMS - initialMS > 5000 then timer 0 : print "done" wait
function rjust$(num) rjust$ = right$("0"+str$(num), 2) end function
|
|
|
Post by tsh73 on Nov 15, 2020 15:06:29 GMT -5
Carl,
+".";ms is no good. See?
23:04:32.961 23:04:33.71 23:04:33.180
71 comes before 180, but .71 - after .180
|
|
|
Post by Brandon Parker on Nov 15, 2020 15:28:56 GMT -5
Here are some handy functions that I have been using for a while if you want some premade functions for easy use.
'Created by Brandon R. Parker Print msToStringTime$(Time$("ms")) Print msToStringTime$(Time$("ms")) End
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function stringTimeToMS(sTime$) stringTimeToMS = (Val(Word$(sTime$, 1, ":")) * 3600000) + _ (Val(Word$(sTime$, 2, ":")) * 60000) + _ (Val(Word$(sTime$, 3, ":")) * 1000) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function Year() Year = Val(Word$(Date$("yyyy/mm/dd") , 1, "/")) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function Month() Month = Val(Word$(Date$("yyyy/mm/dd") , 2, "/")) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function Month$(month) Month$ = Word$("JAN.FEB.MAR.APR.MAY.JUN.JUL.AUG.SEP.OCT.NOV.DEC", month, ".") End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function Day() Day = Val(Word$(Date$("yyyy/mm/dd") , 3, "/")) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function DayofWeek(day) DayofWeek = ((day MOD 7) + 3) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function DayofWeek$(day) DayofWeek$ = Word$("Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday", ((day MOD 7) + 3), ".") End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function isQuarter(month) 'Quarterly starting in January 'Returns False if not a Quarterly month and the 'Quarter if it is a Quarterly Month isQuarter = (Abs(Not((month + 2) Mod 3)) * ((month + 2)/3)) End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function msToStringTime$(msTime) If msTime > stringTimeToMS("24:00:00") Then msTime = (msTime MOD stringTimeToMS("24:00:00")) If msTime < 0 Then If Abs(msTime) > stringTimeToMS("24:00:00") Then msTime = (stringTimeToMS("24:00:00") - (Abs(msTime) MOD stringTimeToMS("24:00:00"))) Else msTime = (stringTimeToMS("24:00:00") + msTime) End If End If hr$ = "00" + str$(Int(msTime/ 3600000)) msTime = (msTime Mod 3600000) min$ = "00" + str$(Int(msTime/ 60000)) msTime = (msTime Mod 60000) sec$ = "00" + str$(Int(msTime/ 1000)) msTime = (msTime Mod 1000) ms$ = "000" + str$(msTime) msToStringTime$ = Right$(hr$, 2) + ":" + Right$(min$, 2) + ":" + Right$(sec$, 2) + "." + Right$(ms$, 3) End Function
{:0)
Brandon Parker
|
|
|
Post by Carl Gundel on Nov 15, 2020 16:26:06 GMT -5
Carl, +".";ms is no good. See? 23:04:32.961 23:04:33.71 23:04:33.180
71 comes before 180, but .71 - after .180 If you’re sorting it, then yes I agree. If you’re just reading it then 71 milliseconds comes before 180. ;-) Thanks. Edit: Besides that, I thought that he wanted this format, but I guess not? Here's a correction. timer 100, [loop] initialMS = time$("ms") wait
[loop] timeMS = time$("ms") timeSecs = int(timeMS/1000) hrs = int(timeSecs/3600) mins = int((timeSecs - hrs * 3600) / 60) secs = timeSecs - hrs * 3600 - mins * 60 ms = timeMS - timeSecs * 1000
time$ = rjust$(hrs,2)+":"+rjust$(mins,2)+":"+rjust$(secs,2)+"."+rjust$(ms,3) print time$ if timeMS - initialMS > 5000 then timer 0 : print "done" wait
function rjust$(num, size) rjust$ = right$("00"+str$(num), size) end function
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Nov 16, 2020 13:48:14 GMT -5
Thanks all for the replies.
This is just to timestamp a logfile of events, so sorting won't be an issue. I have a Timer firing every 100 ticks and the code is doing alot in between when an event occurs, so I'm having to start thinking about the most efficient way to do this without much jumping around into subs/functions.
Excellent suggestions, I'm sure one will definitely suffice.
|
|