Wrong value on calculating difference in hours between TimeCurrent() and TimeLocal()

 

Hi guys,

I am trying to do some calculations with datetimes, and one of them is to detect the difference in hours between the Computer time [ TimeLocal() ] and the Broker Time on Metatrader [ TimeCurrent() ]:

datetime ComputerTime = TimeLocal();
datetime BrokerTime = TimeCurrent();

int TimeOffset = (int) ( BrokerTime - ComputerTime )/3600; 

Print("Computer Time: ", TimeToStr(ComputerTime , TIME_MINUTES), " - Broker Time: ", TimeToStr(BrokerTime, TIME_MINUTES), " - Current Offset: ", TimeOffset);

 

I know that the difference between my computer and metatrader time is 6 hours... but in some times, the code results in only 5 hours..

 

The expected result is something like this: 

Computer Time: 09:32 - Broker Time: 15:32 - Current Offset: 6

Works fine, but sometimes, the result is :

Computer Time: 09:32 - Broker Time: 15:32 - Current Offset: 5 

 

What is wrong ???

 
wemersonrv:

[...] What is wrong ???

If one of the clocks - probably your local clock - is marginally wrong, then the issue ends up being a round-down of an integer division.

For example, local time is 09:32:01 and broker time is 15:32:00. Difference in seconds is 21599. An integer division in MQL4 of 21599 by 3600 produces the result 5; the value of 5.99972 is rounded down in an integer division, not rounded to nearest.

 
Using an int, even if the offset is 5.9999999999999999999999999999 it will be printed as 5
 

Understand... then i change it to 

datetime ComputerTime = TimeLocal();
datetime BrokerTime = TimeCurrent();

double TimeOffset = ( BrokerTime - ComputerTime )/3600; 

Print("Computer Time: ", TimeToStr(local , TIME_SECONDS), " - Broker Time: ", TimeToStr(now, TIME_SECONDS), " - Current Offset: ", OffSet);

 

And still happens... somethimes the result is 5 other times is 6... see some results

2015.10.06 10:22:48.885 LongTermEA - v1.08 - Unlocked EURUSD,M15: Computer Time: 10:22:48 - Broker Time: 16:22:48 - Current Offset: 6

2015.10.06 10:22:47.239 LongTermEA - v1.08 - Unlocked EURUSD,M15: Computer Time: 10:22:47 - Broker Time: 16:22:46 - Current Offset: 5

2015.10.06 10:22:40.138 LongTermEA - v1.08 - Unlocked EURUSD,M15: Computer Time: 10:22:40 - Broker Time: 16:22:39 - Current Offset: 5

2015.10.06 10:22:36.980 LongTermEA - v1.08 - Unlocked EURUSD,M15: Computer Time: 10:22:36 - Broker Time: 16:22:36 - Current Offset: 6


What can i do to get it right ? I just need to get the difference in hours between the two times...

 
wemersonrv:

What can i do to get it right ? I just need to get the difference in hours between the two times...

You need to do something like the following. Specifying the denominator as 3600.0 rather than 3600 forces MQL4 to treat it as a division of double values instead of a division of int values:

int TimeOffset = MathRound( (BrokerTime - ComputerTime) / 3600.0); 

The following also work as ways of instructing MQL4 to treat the calculation as a division of doubles: 

int TimeOffset = MathRound( (BrokerTime - ComputerTime) / (double)3600); 
int TimeOffset = MathRound( (double)(BrokerTime - ComputerTime) / 3600); 
int TimeOffset = MathRound( (double)(BrokerTime - ComputerTime) / (double)3600); 

 

The following does not work, because the calculation is still a division of int values, and simply then puts the rounded-down int value into the double variable:

double TimeOffset = ( BrokerTime - ComputerTime )/3600;
 
jjc:

You need to do something like the following. Specifying the denominator as 3600.0 rather than 3600 forces MQL4 to treat it as a division of double values instead of a division of int values:

The following also work as ways of instructing MQL4 to treat the calculation as a division of doubles: 

 

The following does not work, because the calculation is still a division of int values, and simply then puts the rounded-down int value into the double variable:

 

Understand now... 

 

well, as i have other calculations with the 3600 value, just created a global variable double hoursecs = 3600; and replace it on the code and after a couple of hours of tests, still working...

int OffSet = (int) MathCeil( ( TimeCurrent() - TimeCurrent() ) / hoursecs ); // Hoursecs is double
 

Now you have a problem on the other side. You get 6 or 7.

You have to calculate the Offset as Integer and check the remainder.

If you get 5 and lets say 3000 or higher as remainder, add 1 to the Offset of 5.

 
wemersonrv: What can i do to get it right ? I just need to get the difference in hours between the two times...
int TimeOffset = ( BrokerTime - ComputerTime +1800 )/3600; // Round it integer divide integer result
 
eddie:

Now you have a problem on the other side. You get 6 or 7.

You have to calculate the Offset as Integer and check the remainder.

If you get 5 and lets say 3000 or higher as remainder, add 1 to the Offset of 5.

This is what i need!

Just calculate the results roundint it to above... then this is the right for me... if gets me 5.???? will be round to 6 and that is what i need.

But your suggestion is good... it's more accurate than my way. Thanks!
 
WHRoeder:

WHRoeder, i don't get the point with the 1800 added to the equation.
 
wemersonrv:
WHRoeder, i don't get the point with the 1800 added to the equation.

WHRoeder's way is the more elegant way.

Change my 3000 to 1800 and both ways act the same.

Reason: