I have been reading the posts on date and time and I am looking for an example or advice as to the best way to do this.
I what to have two time ranges - no days but it can span a day so Starttime2 could be 16 (hour) 15 (min) and Endtime2 could be 00 (hour) and 15 (min). I am not interested in seconds or days of the week
Starttime1
Endtime1
Starttime2
Endtime2
Script A would run from the first time range and Script 2 from the second time range.
I have seen various examples in both VBScript and just using MS. What would be the best way to go? One thought I had was to convert everything to minutes and then use that to caculate it.
Any and all advice would be appreicated.
Run Compiled Script from time range
Moderators: JRL, Dorian (MJT support)
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Do you mean the macro should START at the start of the range and keep looping until the END of the range? Or just that it should only run ONCE sometime with the range ... or?
If the former you can do something like:
If the former you can do something like:
Code: Select all
Let>rangeStart=1100
Let>rangeEnd=1600
Label>checktime
Hour>hh
Min>mm
Let>now=%hh%%mm%
If>{(%now%>=%rangeStart%) and (%now%<=%rangeEnd%)}
//we can run
.. your code here
//now loop back - if out of range it won't repeat again
Goto>checktime
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Thanks
You make it look so simple. I was so confused after reading all the posts I was going to complie it with a command line variable for which function (set of rules), a script to shut it down and use Windows Task Scheduler to run it.
However this example doesn't work for your requirement that the range end time can be after midnight of the next day.
Start = 16:15
End = 00:15
This is one way to (hopefully) take care of all situations:
[code]
VBSTART
VBEND
Let>rangeStart=16:15:00
Let>rangeEnd=00:15:00
Let>timeConstant=1/1/1970 12:00:00
Let>FirstTime=True
Let>MSG_HEIGHT=200
Let>MSG_WIDTH=400
Label>checktime
Year>yyyy
Month>mm
Day>dd
Hour>hh
Min>min
//Recalc once per day
If>FirstTime=True
Gosub>CalcOnce
Let>FirstTime=False
Endif
If>{(%hh%=23) and (%min%=59)}
Gosub>CalcOnce
Endif
//Calc now as UTC
VBEval>datediff("s","%timeConstant%","%mm%/%dd%/%yyyy% %hh%:%min%:00"),CurrentUTC
Message>Current=%mm%/%dd%/%yyyy% %hh%:%min%:00 UTC=%CurrentUTC% %CRLF%%CRLF% Start=%myStartNow% UTC=%StartUTC% %CRLF% End=%myEndNow% UTC=%EndUTC%
If>{(%CurrentUTC% >= %StartUTC%) and (%CurrentUTC% Run Now!
Wait>59
Endif
Wait>0.5
Goto>checktime
SRT>CalcOnce
//Calc start time as UTC
Let>myStartNow=%mm%/%dd%/%yyyy% %rangeStart%
VBEval>datediff("s","%timeConstant%","%myStartNow%"),StartUTC
//Calc End time as UTC
If>rangeEndDateAdd("d",1,"%yyyy%-%mm%-%dd%"),result
Let>myEndNow=%result% %rangeEnd%
VBEval>datediff("s","%timeConstant%","%myEndNow%"),EndUTC
Else
Let>mynow=%mm%/%dd%/%yyyy% %rangeEnd%
VBEval>datediff("s","%timeConstant%","%myEndNow%"),EndUTC
Endif
END>CalcOnce
[/code]
Start = 16:15
End = 00:15
This is one way to (hopefully) take care of all situations:
[code]
VBSTART
VBEND
Let>rangeStart=16:15:00
Let>rangeEnd=00:15:00
Let>timeConstant=1/1/1970 12:00:00
Let>FirstTime=True
Let>MSG_HEIGHT=200
Let>MSG_WIDTH=400
Label>checktime
Year>yyyy
Month>mm
Day>dd
Hour>hh
Min>min
//Recalc once per day
If>FirstTime=True
Gosub>CalcOnce
Let>FirstTime=False
Endif
If>{(%hh%=23) and (%min%=59)}
Gosub>CalcOnce
Endif
//Calc now as UTC
VBEval>datediff("s","%timeConstant%","%mm%/%dd%/%yyyy% %hh%:%min%:00"),CurrentUTC
Message>Current=%mm%/%dd%/%yyyy% %hh%:%min%:00 UTC=%CurrentUTC% %CRLF%%CRLF% Start=%myStartNow% UTC=%StartUTC% %CRLF% End=%myEndNow% UTC=%EndUTC%
If>{(%CurrentUTC% >= %StartUTC%) and (%CurrentUTC% Run Now!
Wait>59
Endif
Wait>0.5
Goto>checktime
SRT>CalcOnce
//Calc start time as UTC
Let>myStartNow=%mm%/%dd%/%yyyy% %rangeStart%
VBEval>datediff("s","%timeConstant%","%myStartNow%"),StartUTC
//Calc End time as UTC
If>rangeEndDateAdd("d",1,"%yyyy%-%mm%-%dd%"),result
Let>myEndNow=%result% %rangeEnd%
VBEval>datediff("s","%timeConstant%","%myEndNow%"),EndUTC
Else
Let>mynow=%mm%/%dd%/%yyyy% %rangeEnd%
VBEval>datediff("s","%timeConstant%","%myEndNow%"),EndUTC
Endif
END>CalcOnce
[/code]
analog timer algorithm using array
This is another way that I did mostly for fun. But it does have the advantage that ranges can be set or changed "on the fly". There can be as many ranges as you want providing there is at least 1 minute separation between ranges (1 minute was the chosen resolution in the problem description.)
The following code is intentionally left verbose and not optimized for clarity.
It uses an array structure which represents every minute of a 24 hour day. If the array element is 0 it means that the process should not run, and if it is set to 1 then the target process should run. Ranges spanning a period starting before midnight and ending after midnight should be no problem with this method. (i.e. start range 16:15 end range 00:15) I got the inspiration for this solution by thinking about those cheap analog timers sold in hardware stores which have a time dial, and little pegs along the outside rim of the time wheel which can either be set up or down to indicate an "on" or "off condition at that particular minute.
[code]
Let>hh=00
Let>mm=00
Let>x=%hh%%mm%
Let>count=0
Label>Init_array
Let>Time[%x%]=0
//Message>Time[%x%]=0
Add>count,1
Add>mm,1
If>mm=60
Let>mm=00
Add>hh,1
Endif
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>x=%hh%%mm%
If>x=2359
Let>Time[%x%]=0
Add>count,1
Goto>Init_array_x
Else
Goto>Init_array
Endif
Label>Init_array_x
//MessageModal>Done %count%
//Set range
Let>rangeStarthh=09
Let>rangeStartmm=28
Let>rangeEndhh=00
Let>rangeEndmm=15
Let>count=0
Let>hh=rangeStarthh
Let>mm=rangeStartmm
Let>x=%hh%%mm%
Label>Set_Range
Let>Time[%x%]=1
//Message>Time[%x%]=1
Add>count,1
Add>mm,1
If>mm=60
Let>mm=00
Add>hh,1
Endif
If>hh=24
Let>hh=00
Let>mm=00
Endif
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>x=%hh%%mm%
If>x=%rangeEndhh%%rangeEndmm%
Let>Time[%x%]=1
Add>count,1
Goto>Set_Range_x
Else
Goto>Set_Range
Endif
Label>Set_Range_x
//MessageModal>Done %count%
//Main Loop
Label>checktime
Hour>hh
Min>mm
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>now=%hh%%mm%
If>Time[%now%]=1
MessageModal>Run Now!
Wait>0.5
Goto>checktime
Else
//MessageModal>Not time to run yet
Wait>0.5
Goto>checktime
Endif
[/code]
The following code is intentionally left verbose and not optimized for clarity.
It uses an array structure which represents every minute of a 24 hour day. If the array element is 0 it means that the process should not run, and if it is set to 1 then the target process should run. Ranges spanning a period starting before midnight and ending after midnight should be no problem with this method. (i.e. start range 16:15 end range 00:15) I got the inspiration for this solution by thinking about those cheap analog timers sold in hardware stores which have a time dial, and little pegs along the outside rim of the time wheel which can either be set up or down to indicate an "on" or "off condition at that particular minute.
[code]
Let>hh=00
Let>mm=00
Let>x=%hh%%mm%
Let>count=0
Label>Init_array
Let>Time[%x%]=0
//Message>Time[%x%]=0
Add>count,1
Add>mm,1
If>mm=60
Let>mm=00
Add>hh,1
Endif
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>x=%hh%%mm%
If>x=2359
Let>Time[%x%]=0
Add>count,1
Goto>Init_array_x
Else
Goto>Init_array
Endif
Label>Init_array_x
//MessageModal>Done %count%
//Set range
Let>rangeStarthh=09
Let>rangeStartmm=28
Let>rangeEndhh=00
Let>rangeEndmm=15
Let>count=0
Let>hh=rangeStarthh
Let>mm=rangeStartmm
Let>x=%hh%%mm%
Label>Set_Range
Let>Time[%x%]=1
//Message>Time[%x%]=1
Add>count,1
Add>mm,1
If>mm=60
Let>mm=00
Add>hh,1
Endif
If>hh=24
Let>hh=00
Let>mm=00
Endif
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>x=%hh%%mm%
If>x=%rangeEndhh%%rangeEndmm%
Let>Time[%x%]=1
Add>count,1
Goto>Set_Range_x
Else
Goto>Set_Range
Endif
Label>Set_Range_x
//MessageModal>Done %count%
//Main Loop
Label>checktime
Hour>hh
Min>mm
Length>mm,result
If>result=1
Let>mm=0%mm%
Endif
Length>hh,result
If>result=1
Let>hh=0%hh%
Endif
Let>now=%hh%%mm%
If>Time[%now%]=1
MessageModal>Run Now!
Wait>0.5
Goto>checktime
Else
//MessageModal>Not time to run yet
Wait>0.5
Goto>checktime
Endif
[/code]