Download MetaTrader 5

Get GMT offset automatically

To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
Olumide Samson
91
Olumide Samson 2015.11.30 06:57 

Hi,

I want to get GMT offset automatically in my expert, i tried using this code but i ain't sure its working like it should be...

double ServerGmtOffsetHours=(TimeCurrent()-TimeGMT())/3600;
  Print(DoubleToStr(ServerGmtOffsetHours));
  

 I don't think this is correct, please how do i get the correct GMT offset?

Carl Schreiber
6586
Carl Schreiber 2015.11.30 07:56  
Why? What are your times what do you expect and what do you get?
whroeder1
14480
whroeder1 2015.11.30 12:46  
noobshow I don't think this is correct, please how do i get the correct GMT offset?
double ServerGmtOffsetHours=(TimeCurrent()-TimeGMT())/3600;
double ServerGmtOffsetHours=(TimeCurrent()-TimeGMT() +1800 )/3600;
Round it to the nearest hour. (Won't work in fractional timezones like India.)
Olumide Samson
91
Olumide Samson 2015.12.01 06:47  
WHRoeder:
Round it to the nearest hour. (Won't work in fractional timezones like India.)

I figured out i could do it like this, but can someone else test this if it should be working in all Timezones.

int GetTimeZone()
  {
   int in_ho=TimeHour(TimeCurrent());
   int st[4];
   int GMT = TimeHour(TimeGMT()) & 0xFFFF;
   int res = in_ho - GMT;
   if(res<0){in_ho+=24;}
   return(in_ho-GMT);
  }
  

 This will return the GMT Offset, it does that correctly on my PC using MetaQuotes-Demo, but i wanna be sure if it will be cool in all timezones...

Carl Schreiber
6586
Carl Schreiber 2015.12.01 07:57  

Definitely it is not! You forget to consider that your pc clock might be wrong by a couple of minutes and that TimeCurrent() due to the latency caused by the internet is always a bit behind the correct time!

The int-cast - if I am right - always returns the lowest integer number not the nearest! WHRoeder's solution covers all that and its a lot simpler!

If you need India's fractions and you are sure the worst inaccuracy of both clocks is less than 7 minutes you can change his formula to :

double ServerGmtOffsetHours=(TimeCurrent()-TimeGMT() + 900 )/1800;
Olumide Samson
91
Olumide Samson 2015.12.01 20:34  
gooly:

Definitely it is not! You forget to consider that your pc clock might be wrong by a couple of minutes and that TimeCurrent() due to the latency caused by the internet is always a bit behind the correct time!

The int-cast - if I am right - always returns the lowest integer number not the nearest! WHRoeder's solution covers all that and its a lot simpler!

If you need India's fractions and you are sure the worst inaccuracy of both clocks is less than 7 minutes you can change his formula to :

Will this work in Backtest or my solution?
Carl Schreiber
6586
Carl Schreiber 2015.12.01 20:38  
Nope! TimeGMT() is taken 'right now'(!) from your PC while TimeCurrent() is the 'modeld' time of the Strat. Tester.
To add comments, please log in or register