//+------------------------------------------------------------------+ //| TimeGMT.mqh | //| Paul Hampton-Smith | //| paul1000@pobox.com | //+------------------------------------------------------------------+ /* HOW TO USE THE TIMEUTILS LIBRARY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Place this TimeGMT.mqh file in your experts\include folder and do not compile Add a line to your EA: #include Enable dll calls in MT4 using Tools->Options->Expert Advisers>Allow DLL imports Functions now available in your EA will be: datetime TimeGMT() returns GMT at last tick double TimeZoneLocal() returns local timezone in hours, adjusting for DST double TimeZoneServer() returns server timezone in hours, adjusting for DST */ ///////////////////////////////////////////////////////////////////////////// #import "kernel32.dll" int GetTimeZoneInformation(int& TZInfoArray[]); #import #define TIME_ZONE_ID_UNKNOWN 0 #define TIME_ZONE_ID_STANDARD 1 #define TIME_ZONE_ID_DAYLIGHT 2 // Local timezone in hours, adjusting for daylight saving double TimeZoneLocal() { int TZInfoArray[43]; switch(GetTimeZoneInformation(TZInfoArray)) { case TIME_ZONE_ID_UNKNOWN: Print("Error obtaining PC timezone from GetTimeZoneInformation in kernel32.dll. Returning 0"); return(0); case TIME_ZONE_ID_STANDARD: return(TZInfoArray[0]/(-60.0)); case TIME_ZONE_ID_DAYLIGHT: return((TZInfoArray[0]+TZInfoArray[42])/(-60.0)); default: Print("Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning 0"); return(0); } } // Server timezone in hours double TimeZoneServer() { int ServerToLocalDiffMinutes = (TimeCurrent()-TimeLocal())/60; // round to nearest 30 minutes to allow for inaccurate PC clock int nHalfHourDiff = MathRound(ServerToLocalDiffMinutes/30.0); ServerToLocalDiffMinutes = nHalfHourDiff*30; return(TimeZoneLocal() + ServerToLocalDiffMinutes/60.0); } // Uses local PC time, local PC timezone, and server time to calculate GMT time at arrival of last tick datetime TimeGMT() { // two ways of calculating // 1. From PC time, which may not be accurate // 2. From server time. Most accurate except when server is down on weekend datetime dtGmtFromLocal = TimeLocal() - TimeZoneLocal()*3600; datetime dtGmtFromServer = TimeCurrent() - TimeZoneServer()*3600; // return local-derived value if server value is out by more than 5 minutes, eg during weekend if (dtGmtFromLocal > dtGmtFromServer + 300) { return(dtGmtFromLocal); } else { return(dtGmtFromServer); } }