Tasp
Full Member
Posts: 215
|
Post by Tasp on Jun 8, 2020 13:57:08 GMT -5
Sorry, I'm back again needing assistance!
I have a logfile that changes daily, so I need to create a new log at midnight. Easy right?!
statictext #win.ms, "TIME", 10,10,200,18 open "win" for window as #win TIMER 50, [check]
WAIT
[check] #win.ms, time$("ms") if LEFT$(TIME$(), 2) + MID$(TIME$(), 4, 2) + RIGHT$(TIME$(), 2) = "000000" THEN TIMER 0 : PRINT "Create file!" : END WAIT
WRONG! When tested, because this will call the sub multiple times during that clock cycle of 00 00 00, I end up with 10-15 events while the time is at 00 00 00. I cannot change the TIMER to 1000 as other parts of the program need to fire at 50.
So I changed to using ms instead
statictext #win.ms, "TIME", 10,10,200,18 open "win" for window as #win
TIMER 50, [check]
WAIT
[check] #win.ms, time$("ms") if time$("ms") = 86399999 THEN TIMER 0 : PRINT "Create file!" : END
WaIT
Again, this doesn't work as the time needs to be on exactly that number of ms. So.......I can use > 86399999, however the clock could tick over to the next day and the ms resets to zero, missing the day change and not firing the event.
Any suggestions?
|
|
|
Post by Chris Iverson on Jun 8, 2020 14:07:55 GMT -5
Keep a variable that always stores the last reading you got from time$("ms").
Before updating the variable, compare it to the new value, and if the new value is lower, the clock ticked over. Perform your new day code there, and keep going.
Alternatively, use the first solution you wrote there, but add a flag that checked whether or not you've already processed the new day event. Turn the IF statement that checks if the time is 00:00:00 into an IF-ELSE. Inside the IF TRUE, check the flag, if flag isn't set, create the new file and set the flag. In the ELSE statement for the time check, reset the new day flag.
|
|
|
Post by tsh73 on Jun 8, 2020 16:48:36 GMT -5
that's sure pointer to a need to use a flag
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Jun 9, 2020 11:22:19 GMT -5
After pondering sometime how to achieve this, this is what I come up with. I'm sure it could be done simpliar and more refined! But this is the best I could do. I couln't quite work out how to do it in an IF CASE type so gave up!
Flag = 0 TIMER 50, [check]
WAIT
[check] IF LEFT$(TIME$(), 2) + MID$(TIME$(), 4, 2) + RIGHT$(TIME$(), 2) = "171800" THEN [Create_File] IF LEFT$(TIME$(), 2) + MID$(TIME$(), 4, 2) + RIGHT$(TIME$(), 2) = "171801" THEN Flag = 0
WaIT
[Create_File] IF Flag = 0 THEN PRINT "Create File" Flag = 1 WAIT
|
|
|
Post by Brandon Parker on Jun 11, 2020 20:58:14 GMT -5
Here is how I have done it for years ...
As far as the huge load of functions included, you only need the checkForMidnight() (... obviously ...) and stringTimeToMS() functions, but they are handy time/date functions to have. Date/Time stuff is a huge pain in the butt half the time!!!
Global False : False = 0 Global True : True = 1
Struct midNightTracker, midNightActivated As boolean, _ currentDate As long
AndonLightSetupLong.CurrentDate.struct = Date$("Days") AndonLightSetupBool.midNightActivated.struct = False
[Loop] Scan If checkForMidnight() Then Print "Hello from the Midnight function!!" GoTo [Loop]
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
Function checkForMidnight() If Not(midNightTracker.midNightActivated.struct) Then If (Date$("Days") > midNightTracker.CurrentDate.struct) Then checkForMidnight = True midNightTracker.midNightActivated.struct = True midNightTracker.CurrentDate.struct = Date$("Days") End If Else If Time$("ms") >= stringTimeToMS("00:00:10") Then midNightTracker.midNightActivated.struct = False End If End Function
'____________________________________________________________________________________________________________________________________ '____________________________________________________________________________________________________________________________________
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)) msToStringTime$ = Right$(hr$, 2) + ":" + Right$(min$, 2) + ":" + Right$(sec$, 2) End Function
{:0)
Brandon Parker
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Jun 12, 2020 13:14:21 GMT -5
Some handy functions in there. Adding to snippet library!
|
|