Get GMT offset automatically

Back to topics list To post a new topic, please log in or register
avatar
40
noobshow 2015.11.30 07: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?

avatar
2017
gooly 2015.11.30 08:56 #
 
Why? What are your times what do you expect and what do you get?
avatar
12985
WHRoeder 2015.11.30 13: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.)
avatar
40
noobshow 2015.12.01 07: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...

 

avatar
2017
gooly 2015.12.01 08: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;
avatar
40
noobshow 2015.12.01 21: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?
avatar
2017
gooly 2015.12.01 21:38 #
 
Nope! TimeGMT() is taken 'right now'(!) from your PC while TimeCurrent() is the 'modeld' time of the Strat. Tester.
Back to topics list  

To add comments, please log in or register