g3dfxpro 发表于 2018-7-8 19:53:33

计算GMT偏差时间

#include <WinUser32.mqh>

#import "Kernel32.dll"
int GetTimeZoneInformation(int &TIME_ZONE_INFORMATION[]);
void GetSystemTime(int &SYSTEMTIME[]);
#import

//------ Get GMT Offset by GetTimeZoneInformation() - modified from CrapperQuotes source ------//
int GMT_Off_GetTimeZoneInformation()//20
{
   int a,offlocmin,n;

   n=TimeCurrent()-TimeLocal(); // server time - local time, in seconds

   switch(GetTimeZoneInformation(a))
   {
      case 0: // no daylight saving in local time zone, or unkown
         offlocmin=a;
         break;
      case 1: // local system is operating in standard time (no daylight saving time)
         offlocmin=a;
         break;
      case 2: // local system is operating in daylight saving time
         offlocmin=a+a;
         break;
   }

   return(MathRound(n/3600.0-offlocmin/60.0));
}
//------ Get GMT Offset by GetSystemTime() - unknown source ------//
int GMT_Off_GetSystemTime()//10
{
   double GMT_Diff;
   int SYSTEMTIME;
   int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
   string sMonth,sDay,sHour,sMin,sSec;
   string sUTC_Time;

   GetSystemTime(SYSTEMTIME);//API
   nYear   = SYSTEMTIME&0x0000FFFF;
   nMonth    = SYSTEMTIME>>16;
   nDay      = SYSTEMTIME>>16;
   nHour   = SYSTEMTIME&0x0000FFFF;
   nMin      = SYSTEMTIME>>16;
   nSec      = SYSTEMTIME&0x0000FFFF;
   nMilliSec = SYSTEMTIME>>16;

   sMonth = 100 + nMonth;
   sMonth = StringSubstr(sMonth, 1);
   sDay   = 100 + nDay;
   sDay   = StringSubstr(sDay, 1);
   sHour= 100 + nHour;
   sHour= StringSubstr(sHour, 1);
   sMin   = 100 + nMin;
   sMin   = StringSubstr(sMin, 1);
   sSec   = 100 + nSec;
   sSec   = StringSubstr(sSec, 1);

   sUTC_Time=StringConcatenate(nYear,".",sMonth,".",sDay," ",sHour,":",sMin,":",sSec);

   GMT_Diff=TimeCurrent()-StrToTime(sUTC_Time);

   return(MathRound(GMT_Diff / 3600.0));
}

// Get GMT Offset in several possible ways
int GetGMT_Offset()
{
   bool IsPlausible1=false,IsPlausible2=false,IsIdentical=false;
   int GMT_Offset1,GMT_Offset2;

   GMT_Offset1 = GMT_Off_GetSystemTime();                        //10
   GMT_Offset2 = GMT_Off_GetTimeZoneInformation();               //20

   if(-12<=GMT_Offset1 && GMT_Offset1<=12)
      IsPlausible1=true;
   if(-12<=GMT_Offset2 && GMT_Offset2<=12)
      IsPlausible2=true;
   if(GMT_Offset1==GMT_Offset2)
      IsIdentical=true;

   if(IsIdentical && IsPlausible1)
   {
      ERR_GMT_Offset=0;
      return(GMT_Offset1);
   }

   ERR_GMT_Offset++; // if not identical or not plausible, then increase error counter and return most suitable value

   if(IsPlausible1 && !IsPlausible2)
      return(GMT_Offset1);
   else if(!IsPlausible1 && IsPlausible2)
      return(GMT_Offset2);
   else // leave unchanged
   return(GMT_Offset);
}

悄悄的 发表于 2018-9-14 20:28:20

好帖,来顶下
页: [1]
查看完整版本: 计算GMT偏差时间