计算GMT偏差时间

楼主  收藏   举报   帖子创建时间:  2019-05-05 10:45 回复:0 关注量:546
  1. #include <WinUser32.mqh>

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

  6. //------ Get GMT Offset by GetTimeZoneInformation() - modified from CrapperQuotes source ------//
  7. int GMT_Off_GetTimeZoneInformation()//20
  8.   {
  9.    int a[43],offlocmin,n;

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

  11.    switch(GetTimeZoneInformation(a))
  12.      {
  13.       case 0: // no daylight saving in local time zone, or unkown
  14.          offlocmin=a[0];
  15.          break;
  16.       case 1: // local system is operating in standard time (no daylight saving time)
  17.          offlocmin=a[0];
  18.          break;
  19.       case 2: // local system is operating in daylight saving time
  20.          offlocmin=a[0]+a[42];
  21.          break;
  22.      }

  23.    return(MathRound(n/3600.0-offlocmin/60.0));
  24.   }
  25. //------ Get GMT Offset by GetSystemTime() - unknown source ------//
  26. int GMT_Off_GetSystemTime()//10
  27.   {
  28.    double GMT_Diff;
  29.    int SYSTEMTIME[4];
  30.    int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
  31.    string sMonth,sDay,sHour,sMin,sSec;
  32.    string sUTC_Time;

  33.    GetSystemTime(SYSTEMTIME);//API
  34.    nYear     = SYSTEMTIME[0]&0x0000FFFF;
  35.    nMonth    = SYSTEMTIME[0]>>16;
  36.    nDay      = SYSTEMTIME[1]>>16;
  37.    nHour     = SYSTEMTIME[2]&0x0000FFFF;
  38.    nMin      = SYSTEMTIME[2]>>16;
  39.    nSec      = SYSTEMTIME[3]&0x0000FFFF;
  40.    nMilliSec = SYSTEMTIME[3]>>16;

  41.    sMonth = 100 + nMonth;
  42.    sMonth = StringSubstr(sMonth, 1);
  43.    sDay   = 100 + nDay;
  44.    sDay   = StringSubstr(sDay, 1);
  45.    sHour  = 100 + nHour;
  46.    sHour  = StringSubstr(sHour, 1);
  47.    sMin   = 100 + nMin;
  48.    sMin   = StringSubstr(sMin, 1);
  49.    sSec   = 100 + nSec;
  50.    sSec   = StringSubstr(sSec, 1);

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

  52.    GMT_Diff=TimeCurrent()-StrToTime(sUTC_Time);

  53.    return(MathRound(GMT_Diff / 3600.0));
  54.   }
  55.   
  56. // Get GMT Offset in several possible ways
  57. int GetGMT_Offset()
  58.   {
  59.    bool IsPlausible1=false,IsPlausible2=false,IsIdentical=false;
  60.    int GMT_Offset1,GMT_Offset2;

  61.    GMT_Offset1 = GMT_Off_GetSystemTime();                        //10
  62.    GMT_Offset2 = GMT_Off_GetTimeZoneInformation();               //20

  63.    if(-12<=GMT_Offset1 && GMT_Offset1<=12)
  64.       IsPlausible1=true;
  65.    if(-12<=GMT_Offset2 && GMT_Offset2<=12)
  66.       IsPlausible2=true;
  67.    if(GMT_Offset1==GMT_Offset2)
  68.       IsIdentical=true;

  69.    if(IsIdentical && IsPlausible1)
  70.      {
  71.       ERR_GMT_Offset=0;
  72.       return(GMT_Offset1);
  73.      }

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

  75.    if(IsPlausible1 && !IsPlausible2)
  76.       return(GMT_Offset1);
  77.    else if(!IsPlausible1 && IsPlausible2)
  78.       return(GMT_Offset2);
  79.    else // leave unchanged
  80.    return(GMT_Offset);
  81.   }
打赏