Comparing time

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Comparing time

Post by Kwhiz » Fri Nov 18, 2005 2:36 pm

The following macro doesn't work properly before 10:00am

gettime>time
if>time>10:00,message
goto>end
label>message
msg>time is > 10:00
label>end

MacroScheduler (before 10:00am) believes that 9:00am>10:00am. Hopefully they can fix this bug in a future release. In the meantime, does somebody have a good workaround suggestion?

Thanks,
Kwhiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Nov 18, 2005 3:00 pm

That is not a bug. You're comparing strings there, because they have colons in. And 9 is larger than 1. The string 9:00 comes after string 10:00.

If you remove the colon it will work because then it will be seen as a number and 1000 is larger than 900. So you could just do:

StringReplace>time,:,,time
If>time>1000
.. etc
Endif

Or you can compare time with VBScript:

VBSTART
VBEND
VBEval>Time>TimeSerial(10,0,0),AfterTen
If>AfterTen=True
..whatever
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?

Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Post by Kwhiz » Fri Nov 18, 2005 4:05 pm

StringReplace doesn't work because MacroScheduler still considers the result a string. For example, just as it thinks 9:00>10:00, so does it think 900>1000 because it is still thinking in strings. How do you make MacroScheduler realize that 900 is a value and not a string?

Thanks,
Kwhiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Nov 18, 2005 5:21 pm

Sorry, but it DOES work. Macro Scheduler will interchange between numbers and strings automatically. If a string has a numerical representation it will work as a number. 09:00 is NOT a number, 0900 IS a number. Did you try it!?

This works:

GetTime>time
StringReplace>time,:,,time
If>time>1000
..whatever
Endif

To prove it:

Let>time=900
StringReplace>time,:,,time
If>time>1000
..whatever
Endif

This also works:

GetTime>time
StringReplace>time,:,,time
If>{%time%>1000}
..whatever
Endif

And this works:

VBSTART
VBEND
VBEval>Time>TimeSerial(10,0,0),AfterTen
If>AfterTen=True
..whatever
EndIf

There you go - three ways to do the same thing. Not bad eh?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Post by Kwhiz » Fri Nov 18, 2005 7:13 pm

Well, I guess it didn't work for me because I was still getting AM and PM in my time. When I ask for time, it returns something like 1:15:44 PM.

So if I use stringreplace to get rid of the colons, as well as the AM and PM, then I can finally work with the numbers. And then finally 1000>900. Now I just have to work with PM numbers, since Macroscheduler is now telling me that 1200>100, when in reality 1200 noontime
stringreplace>time,:,,time
stringreplace>time, AM,time
stringreplace>time, PM,time
if>timenextstep
label>lownumber
add>time,120000
label>nextstep

Now I can finally work with time after these 9 steps. It seems like working with time should be something more basic with MacroScheduler, sort of like breathing. It's something we've done our whole life, from learning to read clocks as kids, to punching timeclocks and meeting deadlines as adults. Seems just a little bit silly to me that I have to enter these 9 steps into every macro where I want to compare relative time. Not that it's a really big deal most of the time, but I'm currently working on a macro where I will be running a continuous loop throughout the day, getting numerous pixelcolors and getting the time (and doing relative timechecks) with each loop. I'm trying to keep each loop to less than three seconds, unless a trigger is triggered. And now with each loop I have to perform these 9 extra steps. Ughh!! But at least it's still doable.

Thanks for your help,
Kwhiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Nov 18, 2005 7:51 pm

Well why not use the VBScript solution. Much easier and it uses the Time functions for the purposes they were designed for. Just put the following at the top of your script:

VBSTART
VBEND

And then whereever you need to compare time just do this:

VBEval>Time>TimeSerial(10,0,0),AfterTen
If>AfterTen=True
..whatever
EndIf

Only one line when you need it. Two if you count the If, which you'd need anyway. It can't be much simpler really.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Post by Kwhiz » Fri Nov 18, 2005 9:48 pm

Unfortunately, I don't know or understand VBScript. Maybe if I took a quick lesson and learned a few basics, perhaps I could understand your script better. But I don't know where to do that, without spending a few hours searching the web and figuring it all out myself.

As it is, I don't understand what it is that your script does exactly. AfterTen appears to be a user-defined variable that you created. TimeSerial doesn't appear in the MacroScheduler Help index, so I'm guessing that you created that too, although I don't really understand what it is.

And judging from MS Help, it appears that VBStart and VBEND usually have a block of VBScript code in between them. Your post has no block in between VBStart and VBEND. I'm taking a guess here and I'm thinking that "time" is something that doesn't need to be defined in a VBScript code block, although it can be called with a VBScript command, such as VBEval. Am I getting close? lol.

And the three numbers in the parentheses I'm clueless about. Assuming the 10 is referring to 10:00, how would I change that to 10:30? Would it be 10.3? 10.30? 10:30? 10.5? Sorry, I'm a bit clueless.

Well, thanks again for your help.
Kwhiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Nov 18, 2005 10:17 pm

Ok. I'll try to explain.

First if you look in the help file under any VBScript commands you will see a link to the VBScript documentation on our website. This is a link to the official Microsoft documentation.

VBSTART and VBEND are needed to tell the script that we want to use VBScript. We would put VBScript functions etc between them, but we don't need to put any code between them because we will use built in VBScript functions that are already defined.

So we will use two VBScript functions that are in the VBScript documentation linked to in the help file:

1. Time - this returns the value of the current time.
2. TimeSerial takes the following parameters:
TimeSerial(HOUR, MINUTE, SECOND) and returns a time value.

Please see the VBScript documentation for more detail.

So we have Time which returns the value of NOW and TimeSerial which will return the value of any given time. To return the time value of 10:00am we would pass it 10 for the hour, 0 for the minutes and 0 for the seconds. Hence: TimeValue(10,0,0).

So if we evaluate the expression Time>TimeSerial(10,0,0) we will get Trule if the current time is after 10:00am and False if it is not.

So we put the result of that expression in a variable called AfterTen.

Next we use If to check if AfterTen is True or False.

So, we end up with

//Tell script we'd like to use VBScript
VBSTART
VBEND

//See if Now is after Ten AM
Eval>Time>TimeSerial(10,0,0),IsItAfterTenDoYouThink

//Now we can do a simple boolean evaluation:
If>IsItAfterTenDoYouThink=True
//Yes it is
..do whatever
Else
//No it isn't
..do whatever else
Endif

The VBScript documentation is linked on our website and the help file links to it also, so it's there for the perusing.

I hope I am making sense now.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Post by Kwhiz » Fri Nov 18, 2005 11:10 pm

Yes, I understand better now. Thanks. The MSHelp file says that VBEval returns a variable, but in your example, it returns either TRUE or FALSE. I'm guessing that has something to do with your use of TimeSeries.

Thanks as usual for your help,
Kwhiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Nov 19, 2005 12:18 am

No, VBEval returns a variable. The variable is user defined. You call it what you like. That variable holds the result of the VBEval statement. Which in this case is a boolean value - true or false.

Perhaps you'll understand it better if we show it all in long form to simplify the VBEval line. We could do this:

VBSTART
Function IsItLaterThanTen
dim TenAM
TenAM = TimeValue(10,0,0)
if Time > TenAM then
IsItLaterThanTen = True
else
IsItLaterThanTen = False
End if
End Function
VBEND

VBEval>IsItLaterThanTen,MyVeryOwnVariable

//MyVeryOwnVariable now holds the result of the IsItLaterThanTen function, which will be either True or False.

Here VBEval calls the IsItLaterThanTen function which we have created and which we put in the VBSTART/VBEND block. We ask VBEval to put the result of that function in our local MyVeryOwnVariable variable.

In this case it's a boolean. Below it's an integer:

VBSTART
Function AddTwoVals(x,y)
AddTwoVals = x + y
End Function
VBEND

VBEval>AddTwoVals(5,4),answer
//answer here will now hold the value 9 if I am not mistaken

Here the result of the VBEval call is a variable called answer which holds an integer value.

Welcome to the wonderful world of programming :-)
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kwhiz
Junior Coder
Posts: 35
Joined: Wed Jan 12, 2005 6:19 pm

Post by Kwhiz » Sat Nov 19, 2005 2:09 am

Oh yes, I reread and studied your posts until a light bulb went off in my head. I think I now understand perfectly. Thanks!

One mistake I was making early on was not understanding the purpose of the second ">" in the following command line (from your post:)

VBEval>Time>TimeSerial(10,0,0),AfterTen

I thought the second ">" was just another pointer, like the first one. I wasn't viewing it as a "greater than" symbol, so I therefore was not looking at the command as an "if/then" statement. Understand? I first looked at the statement as if it was akin to something like "C:\ProgFiles\MSched\stuff.htm" so I didn't really understand what the whole command line was all about. I didn't understand that a mathematical equation was occcuring in that sentence. But now I see!

And after a small experiment, I discovered that TimeSerial operates on 24-hour "Army time." So now everything is making perfect (usable!) sense.

Thank you so much, and have a good weekend,
Kwhiz

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts