Calculate UTC time from time server?

 

Hello all,

I trying to build indicator relax with sessions open and close. Input time setting is UTC, so i need to convert each time[i] (broker time) to UTC time. My ideal is 

hourshift=(int)(TimeCurrent()-TimeGMT())/3600; Print("hourshift: ",hourshift);
or
MqlDateTime tc,tgmt;
TimeCurrent(tc); TimeGMT(tgmt); 
hourshift=tc.hour-tgmt.hour;

this will calculate shift hour from broker time to UTC and this value is static, in loop of indicator need ajust this hourshift.

BUT i found strange: hourshift some time =2, some time=3.  I ask my broker (perperstone) and their timezone is GMT+3 so i thing my hourshift value always 3.  

Can you please clean for me? Or help me better way to calculate hourshift beetween broker time and UTC time?

Thank you!

 
convert to unix time and use it to calculate the time shift.
1 Hour = 60 x 60 seconds

good luck.
 
Soewono Effendi #:
convert to unix time and use it to calculate the time shift.
1 Hour = 60 x 60 seconds

good luck.

I tried

hourshift=(int)((long)TimeCurrent()-(long)TimeGMT())/3600; Print("hourshift: ",hourshift);

but same result

 
Minh Truong Pham:
+3 so i thing my hourshift value always 3.  

I think you should go for minutes shift instead of hour shift. This is safer.

 
Dealing with time (2) functions
Dealing with time (2) functions
  • www.mql5.com
Calculate DST for USA, EUR, AUD and RUB and the offset time of the broker automatically from the 70's until 2030 - even in the Strategy Tester of MQ.
 
Carl Schreiber #:
Try this: https://www.mql5.com/en/code/45287

Thank  Carl Schreiber ! This work!

 
Minh Truong Pham #:

Thank  Carl Schreiber ! This work!

:)

Glad that it helped!