Tasp
Full Member
Posts: 215
|
Post by Tasp on Dec 26, 2020 13:57:01 GMT -5
I would like to include an up time timer within my program. Which is simple right?! This is extremely frivolous, and is only used for whenever the user looks at the [About] window Well as Carl pointed out in another thread TIME$("ms") will show milliseconds since midnight, which is handy for showing uptime. Issue is, at midnight it "resets" back to 0 Since my program is likely to be running for sometime, this is likely to roll over multiple times, so I'd need to keep track of multiple items, including days gone past as well as tricky maths to calc the difference. So If I just grab the Time and Date of program start I can calc the uptime. But when I code this I'm running into all sorts of issues, including the month rollover dates eg, 28th, 30th, 31st.... Does anyone have any simpler ways/suggestions how to achieve this?
|
|
|
Post by Chris Iverson on Dec 26, 2020 14:14:31 GMT -5
If I remember correctly, date$("days") will give you the number of days since the Unix epoch(I think?)
Save date$("days") and time$("ms") when you begin your program.
When you need to calculate uptime, do both operations again. Compare the days values to get the number of days that have passed, and compare the ms values for time within the day.
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Dec 26, 2020 14:54:21 GMT -5
Cheers Chris,
DATE$("days") I checked the helpfile and it actually starts at 01/01/01 rather than the Unix epoch, but effectively its a known constant which is what we need.
I'm starting with the below, but can you see the issue already!
startdays = DATE$("days") starttime = TIME$("seconds")
timer 1000, [uptime] WAIT
[uptime] currentdays = DATE$("days") currenttime = TIME$("seconds")
daysup = (currentdays - startdays) secondsup = (currenttime - starttime)
minsup = int(secondsup / 60) hoursup = int(minsup / 60) daysup = int(hoursup / 24)
PRINT daysup;" days " ; hoursup ; " hrs " ; minsup ; " mins " ; secondsup ; " secs" WAIT
|
|
|
Post by Chris Iverson on Dec 26, 2020 15:03:25 GMT -5
Yeah, you do have to do math with the timer values to correct for it not being a full day yet.
If the current ms time is less than the saved one, subtract one day, add a day's worth of ms to the current ms measurement, and do the subtraction. I think that should give the correct time elapsed.
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Dec 26, 2020 16:17:17 GMT -5
For further searchers, I believe this is the solution for TIME only. It does not calculate date at all
'Functions courtesy Brandon Parker 'https://libertybasiccom.proboards.com/thread/1122/checking-time-second?page=1
startdays = DATE$("days") starttime = TIME$("ms")
timer 1000, [uptime] WAIT
[uptime] currentdays = DATE$("days") currenttime = TIME$("ms")
daydiff = (currentdays - startdays) msdiff = (currenttime - starttime)
print msToStringTime$(msdiff)
WAIT
FUNCTION stringTimeToMS(sTime$) stringTimeToMS = (Val(Word$(sTime$, 1, ":")) * 3600000) + _ (Val(Word$(sTime$, 2, ":")) * 60000) + _ (Val(Word$(sTime$, 3, ":")) * 1000) 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 day$ = "00" + STR$(INT(msTime / 86400000)) 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)) msToStringTime$ = RIGHT$(day$, 3) + ":" + RIGHT$(hr$, 2) + ":" + RIGHT$(min$, 2) + ":" + RIGHT$(sec$, 2) END FUNCTION
|
|
|
Post by tsh73 on Dec 27, 2020 5:29:52 GMT -5
Tasp, I fail to see how your code accounts for day change. I wonder if it actually doesn't. (if start time is Jun 01 10:10 and current time is Jun 02 10:15 msdiff is positive and daydiff is not used, but whole day is passed)
I have old subs working with dates - I run through it today - rewrote some stuff. It allows adding/subtracting days and show date difference as 5 days 06:01:13 or -3 days 12:45:03
Just note that dateTimeDiff2str$ and dateTime2str$ is different functions.
Have a look.
'kind of date time library 'to keep date and time (to make add|subtract etc) 'we'll keep date as integer part, and time as fractional part 'So time in seconds till midnight will be (fractional part)*wholeDaySec global wholeDaySec wholeDaySec = 24 * 60 * 60
datePart$ = "08/06/07" print "Date without time is stored like days number" curDT = date$(datePart$) print ,curDT 'print ,makeDateTime(datePart$) 'works too, gives same result print ,dateTime2str$(curDT) print
dateTime$ = "08/06/07 01:02:03" print "Date with time is stored like whole days number and fractional time" print " fractional time corresponds to seconds as (fractional part)*wholeDaySec" 'should split into date and time datePart$ = word$(dateTime$, 1) timePart$ = word$(dateTime$, 2) print ,"datePart: >";datePart$;"<" print ,"timePart: >";timePart$;"<" 'different ways to create dateTime 'curDT = date$(datePart$)+minsec2s(timePart$)/wholeDaySec 'print curDT 'curDT=makeDateTime2(datePart$, timePart$) 'print curDT curDT=makeDateTime(dateTime$) print curDT print "all ways make same result and decodes to" print dateTime2str$(curDT) print
'now for some examples print "Current date as date time" print date$(), time$() curDT = makeDateTime2(date$(), time$()) print "our internal representation, integer part - days, fractional - time" print curDT, print " decoded back ", print dateTime2str$(curDT) print
print "Current date time + 5 days 6 hours 01 min 13 sec" curDT2 = curDT + dateTimeSpan(5, "06:01:13") print curDT2, "decodes to ", print dateTime2str$(curDT2) print
print "Now dateDiff between these two" dateDiff =curDT2- curDT print dateDiff, "our internal representation" print dateTimeDiff2str$(dateDiff), "decoded back" print
'curDT3 = curDT - dateTimeSpan(3, "12:45:03") print "Current date time - 3 days 12 hours 45 min 03 sec" curDT3 = curDT - dateTimeSpan(3, "12:45:03") print curDT3, "decodes to ", print dateTime2str$(curDT3)
print "Now dateDiff between these two" dateDiff2 =curDT3- curDT print dateDiff, "our internal representation" print dateTimeDiff2str$(dateDiff2), "decoded back" print
print "We even could add/subtract these dateDiffs" print dateTimeDiff2str$(dateDiff) print "+" print dateTimeDiff2str$(dateDiff2) print "------------------------------" print dateTimeDiff2str$(dateDiff+dateDiff2)
wait
print print "press a key to see rest of this thing" a$=input$(1) print
print "Buildt-in:" print "time now as string '16:21:44'" print time$() print "seconds since midnight as number 33221" print time$("seconds") print "milliseconds since midnight as number 33221342" print time$("ms") print "Now some calculations" print "Now get time now in the form 'hour:min:sec.thousendth of a second'" print time$();".";right$(str$(time$("ms") mod 1000+1000),3)
print "Here is a function that will convert any amount of milliseconds in hh:mm:ss.tho form" print timeMs2str$(1) '1 ms print timeMs2str$(1000) '1 sec print timeMs2str$(60000) '1 min print timeMs2str$(12345678) 'something random print timeMs2str$(time$("ms")) 'current time
print "Now, backwards function:" print minsec2ms("0:0:0.001") print minsec2ms("0:0:1.000"), minsec2ms("0:0:1") print minsec2ms("0:1:0") print minsec2ms("3:25:45.046") end
function dateTime2str$(days) dd = int(days) tt = days - dd tim = tt*wholeDaySec dateTime2str$ = date$(dd);" "; time2str$(tim) end function
function dateTimeDiff2str$(days) 'only change is days 'and it could be negative if days <0 then sign$="-": days = 0-days dd = int(days) tt = days - dd tim = tt*wholeDaySec dateTimeDiff2str$ = sign$;dd;" days "; time2str$(tim) end function
function dateTimeSpan(days, time$) dateTimeSpan = days + minsec2s(time$)/wholeDaySec end function
function makeDateTime2(dt$, tm$) 'obviously need valid date, time strings makeDateTime2=date$(dt$)+minsec2s(tm$)/wholeDaySec end function
function makeDateTime(dtTm$) 'obviously need valid date, time strings dt$ = word$(dtTm$, 1) 'left one tm$ = word$(dtTm$, 2) 'the rest makeDateTime=makeDateTime2(dt$, tm$) end function
function time2str$(sTime) sTime = int(sTime) ss$ = right$(str$(sTime mod 60+100),2) sTime = int(sTime/60) mm$ = right$(str$(sTime mod 60+100),2) sTime = int(sTime/60) 'omitZeroHours=1 if omitZeroHours then '0 hours is omitted, that is 15:23 instead of 00:15:23 hh$ = "" if sTime<>0 then hh$ = str$(sTime);":" else hh$ = right$(str$(sTime+100),2);":" end if time2str$ = hh$;mm$;":";ss$ end function
function timeMs2str$(msTime) thous$ = right$(str$(msTime mod 1000+1000),3) 'trick found in Orient Express listing sTime = int(msTime/1000) timeMs2str$ = time2str$(sTime);".";thous$ end function
function minsec2s(minsecTime$) s = 0 idx = 1 a1$ = word$(minsecTime$,idx,":") while a1$<>"" idx = idx+1 s = s*60+val(a1$) a1$ = word$(minsecTime$,idx,":") wend minsec2s = int(s) end function
function minsec2ms(minsecTime$) s = 0 idx = 1 a1$ = word$(minsecTime$,idx,":") while a1$<>"" idx = idx+1 s = s*60+val(a1$) a1$ = word$(minsecTime$,idx,":") wend minsec2ms = s*1000 end function
|
|
|
Post by honkytonk on Dec 27, 2020 7:20:25 GMT -5
This calculates the date with the time, which you just have to consult whenever you want.
mois$ = "Jan Fév Mar Avr Mai Jun Jui Aou Sep Oct Nov Déc" today$ = date$("mm/dd/yyyy") j = date$(today$) jsem$ = word$("Mar Mer Jeu x Ven Sam x Dim Lun",int((j/7-int(j/7))*10)+1) date$ = jsem$+" "+mid$(today$,4,2)+" "+word$(mois$, val(today$))+" "+left$(time$(),5) print date$
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Dec 27, 2020 9:21:32 GMT -5
tsh73, you are entirely correct. Essentially my code is flawed when it comes to days, although I collect the days information, you are totally correct, in that I do nothing with it. I was relying on the ms code adding days, this will never happen.
While I am extremely grateful for your code, I am unable to work out the portion of it that I require. I've tried breaking it down into blocks, but I simply cannot get my head around it at the minute.
I have purely opted, for the simplest, probably incorrect solution, of just checking program started on DATE$("days") then checking the amount of days again when the uptime window is called. This is hugely flawed again, as if the program is started at 23:59, at 00:00, it will show it's been up for 1 day.
I vaguely understand what your code is doing, in that it takes the integer of both the time and date and calculates the difference, then puts it back into human readable text. I just cannot fathom how exactly.
HonkyTonk Thanks but I'm looking to calculate the difference between 2 dates and times, start of program and current.
|
|
|
Post by Chris Iverson on Dec 27, 2020 14:26:37 GMT -5
In your first example, Tasp, you forgot to remove the time that got bumped up from seconds from the seconds value itself, and the same going to hours from minutes.
That is, if 90 seconds have passed, that means 1 minute 30 seconds have passed. Your code would spit out 1 minute 90 seconds.
Also, you calculate the days value from here, instead of using the already calculated days value.
Here's your example adjusted to take care of all that, and I believe it should also take the day boundary into effect successfully. As I said before, if the current time in seconds is LESS than the saved time in seconds, subtract one day from the current day, and add a full day's worth of seconds to the current second time, and do all the calculations. Looks like it comes out correctly.
minute = 60 hour = 60 * 60 fullDaySeconds = 24 * hour startdays = DATE$("days") starttime = TIME$("seconds")
'This is an adjustment to test for day rollover. 'It adjusts the day back one day, and adjusts the time forward one minute. ' 'This should start the timer at 0d23h59m0s having elapsed. startdays = startdays - 1 starttime = starttime + (minute)
timer 1000, [uptime] WAIT
[uptime] currentdays = DATE$("days") currenttime = TIME$("seconds")
if currenttime < starttime then currentdays = currentdays - 1 currenttime = currenttime + fullDaySeconds end if
daysup = (currentdays - startdays) secondsup = (currenttime - starttime)
minsup = int(secondsup / 60) secondsup = secondsup mod 60
hoursup = int(minsup / 60) minsup = minsup mod 60
PRINT daysup;" days " ; hoursup ; " hrs " ; minsup ; " mins " ; secondsup ; " secs" WAIT
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Dec 29, 2020 4:20:48 GMT -5
Thanks Chris, that's seems so easy now I see it. I was faffing around completely in the wrong direction, thanks for the simple solution
|
|