Comparing Time Values

 
What's the best way to compare time values?

For instance, assume I have a string var of "12:35". I want to place trades anytime after 12:35. So, I might have something like this:
datetime var = StrToTime("12:35");
datetime now = TimeLocal();
if(TimeHour(now) >= TimeHour(var))
dosomething();



How do I deal with the minutes?

Thanks.

stockwet

 
By the way, this is how I handled it, but, I was hoping there was a more elegant solution. Also, what if I want to have a constraint that falls between 23:00 and 5:00? Anyone with experience on this?

Thanks.

stockwet

bool checkTime()
{
   int sm, sh, em, eh, nowm, nowh;
   bool start, end, ok;

   sm = TimeMinute(ss); //Start Signal minute
   sh = TimeHour(ss); // Start Signal hour
   em = TimeMinute(es); //End Signal minute
   eh = TimeHour(es); // End Signal hour
   nowm = TimeMinute(now); // Current minute
   nowh = TimeHour(now); // Current Hour
   if((nowh == sh && nowm >= sm) || (nowh > sh)) start = true;
   else start = false;
   if((nowh == eh && nowm <= em) || (nowh < eh)) end = true;
   else end = false;
      
      if (start == true && end == true) ok = true;
      else ok = false;
   return(ok);
}
 
Hello Stockwet!

I use StrToTime to convert string into a 32 bit integer (in the format used by c/c++,perl).

extern string var = "12:35:10";
int _var = StrToTime(var);

if (LocalTime() >= _var) {
    dosomething();
}



It will have the following problems:
1. LocalTime(), for some reason for me, is not the same *intended* time for different computers I have tested. On my 4 machines (where my computer is set for GMT+8), LocalTime() is always GMT+0; yet on other people's machine, LocalTime() is exactly the local time of their machines as it should be! WIERD!

2. Converting a "12:35:10" string into an integer includes the "date" as well. So, it will remain true afterwards. Perhaps you can extract the "time only part of the datetime integer" by MathMod?.

Hope this helps.

 
Thanks, BK.

Actually, it didn't even dawn on my to compare my var against Local time after converting it to time. That's pretty nice. I'll try it out.

I've never had a problem with local time. Actually, one thing to check. LocalTime is deprecated now. As of build 201, I think, you need to use TimeLocal. So, maybe that's impacting your tests. Probably not if it works on some machines, but not others. Worth a try, though.