GMT to Server - page 6

 

I'm not sure if this has been addressed already but this rounding logic has inconsistencies.

   datetime gt           = D'2017.10.08 01:00:00';//TimeGMT();
   datetime st           = D'2017.10.08 03:00:00';//TimeCurrent();
   int      sTZ          = int((st-gt + 1800)/3600);
            // output =  GMT+2
            gt           = D'2017.10.08 03:00:00';
            st           = D'2017.10.08 01:00:00';
            sTZ          = int((st-gt + 1800)/3600);
            // output =  GMT-1 ...should be GMT-2


Here is a potential fix. 

   datetime gt       = D'2017.10.08 00:13:00';//TimeGMT();
   datetime st       = D'2017.10.07 22:55:46';//TimeCurrent();
   
   int      delta    = int((st-gt)%3600);
            st       = delta >= 1800 ? st+(3600-delta): delta<-1800 ? st+(-3600-delta) : st-delta;
   int      sTZ      = int((st - gt)/3600);
 
nicholishen: but this rounding logic has inconsistencies.

Oops, your right, int((st-gt + 1800)/3600) truncates toward zero not rounds. So use (int)MathRound( (st-gt)/3600.)
          Exactly what I used in Indicators: Zero Lag Timer - Indices - Articles, Library comments - MQL5 programming forum

Alternative, still using integer arithmetic:

template<typename T> T sign(const& T t){ return T(t<0? -1 : +1); }

int((st-gt +sign(st-gt)*1800)/3600)
 
nicholishen:

I'm not sure if this has been addressed already but this rounding logic has inconsistencies [...]

On a non-technical note, as partially covered by PierroNeto above, if the Indian market de-regulated and opened up, then you could get brokers running their servers at IST of GMT+05:30. I'd have ruled out this scenario until last week, when I saw Chinese brokers running their servers at local time of GMT+8...
 
The best way is a knowing Server UTC offset, or Server UTC time, better to have DST info as well, I have started a poll for suggested required functions: TimeTradeServerGMTOffset() and/or GetTradeServerTimeZoneInfo().https://www.mql5.com/en/forum/217145
MQL4/5 required functions: TimeTradeServerGMTOffset() or GetTradeServerTimeZoneInfo()
MQL4/5 required functions: TimeTradeServerGMTOffset() or GetTradeServerTimeZoneInfo()
  • 2017.10.10
  • www.mql5.com
Expert Advisors and Automated Trading: MQL4/5 required functions: TimeTradeServerGMTOffset() or GetTradeServerTimeZoneInfo()
Reason: