Download MetaTrader 5

GMT from external Source

To add comments, please log in or register
Do you have questions on how to turn your idea into reality? Ask them on the Forum!
Superion
130
Superion 2007.12.19 14:29 

How does one pull in GMT from a site such as timeanddate.com via html? All the doco's seem to be on using text/csv files.

I wish to use GMT time as the basis for a trade time filtering system. At the moment you must set manually via a extern variable.

A bonus would be the ability to pull in city times and GMT offsets...

Regards,

~S

Slawa
Moderator
6839
Slawa 2007.12.19 16:08  

Following code was here on this forum. May be it can help you

//+------------------------------------------------------------------+
//|                                                    LocalTime.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
#import "kernel32.dll"
void GetLocalTime(int& TimeArray[]);
void GetSystemTime(int& TimeArray[]);
int  GetTimeZoneInformation(int& TZInfoArray[]);
#import
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int    TimeArray[4];
   int    TZInfoArray[43];
   int    nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
   string sMilliSec;
//----
   GetSystemTime(TimeArray);
//---- parse date and time from array
   nYear=TimeArray[0]&0x0000FFFF;
   nMonth=TimeArray[0]>>16;
   nDay=TimeArray[1]>>16;
   nHour=TimeArray[2]&0x0000FFFF;
   nMin=TimeArray[2]>>16;
   nSec=TimeArray[3]&0x0000FFFF;
//---- format date and time items
   string time_string=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
   Print("System time is: ",time_string);
//----
   Print("TimeLocal function returns ",TimeToStr(TimeLocal()));
//----
   GetLocalTime(TimeArray);
//---- parse date and time from array
   nYear=TimeArray[0]&0x0000FFFF;
   nMonth=TimeArray[0]>>16;
   nDay=TimeArray[1]>>16;
   nHour=TimeArray[2]&0x0000FFFF;
   nMin=TimeArray[2]>>16;
   nSec=TimeArray[3]&0x0000FFFF;
   nMilliSec=TimeArray[3]>>16;
//---- format date and time items
   sMilliSec=1000+nMilliSec;
   sMilliSec=StringSubstr(sMilliSec,1);
   time_string=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
   Print("Local time is: ",time_string,":",sMilliSec);
//---- shift with daylight savings
   int gmt_shift=0;
   int ret=GetTimeZoneInformation(TZInfoArray);
   if(ret!=0) gmt_shift=TZInfoArray[0];
   Print("Difference between your local time and GMT is: ",gmt_shift," minutes");
   if(ret==2) gmt_shift+=TZInfoArray[42];
   Print("Current difference between your local time and GMT is: ",gmt_shift," minutes");
//---- GMT
   datetime local_time=StrToTime(time_string);
   Print("Greenwich mean time is: ",TimeToStr(local_time+gmt_shift*60,TIME_DATE|TIME_SECONDS));
//---- winter time (nYear remains the current)
   nYear=TimeArray[17]&0x0000FFFF;
   nMonth=TZInfoArray[17]>>16;
   nDay=TZInfoArray[18]>>16;
   nHour=TZInfoArray[19]&0x0000FFFF;
   nMin=TZInfoArray[19]>>16;
   nSec=TZInfoArray[20]&0x0000FFFF;
   time_string=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
   Print("Standard time is: ",time_string);
//---- summer time (nYear remains the current)
   nYear=TimeArray[38]&0x0000FFFF;
   nMonth=TZInfoArray[38]>>16;
   nDay=TZInfoArray[39]>>16;
   nHour=TZInfoArray[40]&0x0000FFFF;
   nMin=TZInfoArray[40]>>16;
   nSec=TZInfoArray[41]&0x0000FFFF;
   time_string=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
   Print("Daylight savings time is: ",time_string);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string FormatDateTime(int nYear,int nMonth,int nDay,int nHour,int nMin,int nSec)
  {
   string sMonth,sDay,sHour,sMin,sSec;
//----
   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);
//----
   return(StringConcatenate(nYear,".",sMonth,".",sDay," ",sHour,":",sMin,":",sSec));
  }
//+------------------------------------------------------------------+
Superion
130
Superion 2007.12.20 01:35  
Very helpful, thank you!
FREDERICK WILSON
57
FREDERICK WILSON 2008.02.20 02:08  
Does the code provided above work to translate all MT4 time references (like time references at bottoms of charts) to the time in the user's timezone?
brewmanz
486
brewmanz 2010.11.29 12:04  
#import "kernel32.dll"
int SystemTimeToFileTime(int& TimeArray[], int& FileTimeArray[]);
int FileTimeToLocalFileTime(int& FileTimeArray[], int& LocalFileTimeArray[]);
void GetSystemTime(int& TimeArray[]);
#import

datetime GetWinLocalDateTime()
{
   double hundrednSecPerSec = 10.0 * 1000000.0;
   double bit32to64 = 65536.0 * 65536.0;
   double secondsBetween1601And1970 = 11644473600.0;
   int    TimeArray[4];
   int    FileTimeArray[2];   // 100nSec since 1601/01/01 UTC
   int    LocalFileTimeArray[2];   // 100nSec since 1601/01/01 Local

   GetSystemTime(TimeArray);
   SystemTimeToFileTime(TimeArray, FileTimeArray);
   FileTimeToLocalFileTime(FileTimeArray, LocalFileTimeArray);
   
   double lfLo32 = LocalFileTimeArray[0];
   if(lfLo32 < 0)
      lfLo32 = bit32to64 + lfLo32;
   double ticksSince1601 = LocalFileTimeArray[1] * bit32to64 + lfLo32;
   double secondsSince1601 = ticksSince1601 / hundrednSecPerSec;
   double secondsSince1970 = secondsSince1601 - secondsBetween1601And1970;
   return (secondsSince1970);
}

datetime GetWinUtcDateTime()
{
   double hundrednSecPerSec = 10.0 * 1000000.0;
   double bit32to64 = 65536.0 * 65536.0;
   double secondsBetween1601And1970 = 11644473600.0;
   int    TimeArray[4];
   int    FileTimeArray[2];   // 100nSec since 1601/01/01 UTC
   GetSystemTime(TimeArray);
   SystemTimeToFileTime(TimeArray, FileTimeArray);
   
   double lfLo32 = FileTimeArray[0];
   if(lfLo32 < 0)
      lfLo32 = bit32to64 + lfLo32;
   double ticksSince1601 = FileTimeArray[1] * bit32to64 + lfLo32;
   double secondsSince1601 = ticksSince1601 / hundrednSecPerSec;
   double secondsSince1970 = secondsSince1601 - secondsBetween1601And1970;
   return (secondsSince1970);
}

This is code that I use to get either Local or UTC(GMT) time from Windows. It has the advantage of directly returning datetime variables.

Apart from the one-line conversion from UTC to Local (and changes to variable names to handle that), the routines are the same.

George
148
George 2011.04.06 11:39  
brewmanz:

This is code that I use to get either Local or UTC(GMT) time from Windows. It has the advantage of directly returning datetime variables.

Apart from the one-line conversion from UTC to Local (and changes to variable names to handle that), the routines are the same.

Hello brewmanz,

Once again, thanks for the tip.

First I'd like to say that I'm not a MQL4 wizard at all and not familiar with all its functionality.

So, I ask for your patience........

The first program, I compiled as a custom indicator. It compiles well. However, when attaching it to the chart, there is no action whatsoever.

Your code I copied into the start section (as custom indicator), moved the #import part up underneath the #property link.

It would not compile. The code you published looks to me as incomplete code or only a fraction of a program.

I also use Windows 7. It is possible that the kernel32.dll doesn't deliver the correct result.

If you have an idea, your help is appreciated.

brewmanz
486
brewmanz 2011.04.06 12:30  

My code is actually copied from a library I've written for myself, but you can include it 'somewhere' (beginning? end?) in your own code.

Then call GetWinLocalDateTime()as you would an MQL4 function

George
148
George 2011.04.08 10:24  
brewmanz:

My code is actually copied from a library I've written for myself, but you can include it 'somewhere' (beginning? end?) in your own code.

Then call GetWinLocalDateTime()as you would an MQL4 function

Thanks, I'll give it a go.

Have a nice day.

onewithzachy
2271
onewithzachy 2012.03.20 15:27  
stringo:

Following code was here on this forum. May be it can help you

Hi stringo,

Thanks for that. My time zone doesn't observe DST so these codes

//---- shift with daylight savings
   int gmt_shift=0;
   int ret=GetTimeZoneInformation(TZInfoArray);
   if(ret!=0) gmt_shift=TZInfoArray[0];
   Print("Difference between your local time and GMT is: ",gmt_shift," minutes");
   if(ret==2) gmt_shift+=TZInfoArray[42];
   Print("Current difference between your local time and GMT is: ",gmt_shift," minutes");

should be like this

//---- shift with daylight savings
   int gmt_shift=0;
   int ret=GetTimeZoneInformation(TZInfoArray);
   //if(ret!=0)                           <<== Just change this. zero mean that daylight saving time is not observed
   gmt_shift=TZInfoArray[0];
   Print("Difference between your local time and GMT is: ",gmt_shift," minutes");
   if(ret==2) gmt_shift+=TZInfoArray[42];
   Print("Current difference between your local time and GMT is: ",gmt_shift," minutes");

And that's return my GMT distance correctly. Here at MSDN GetTimeZoneInformation.

Haven't check brewmanz's GetWinUtcDateTime() codes, but thanks any way.

Man, this topic is old, but not old school.

BTW, that was typedef struct out, any sample for typedef struct in ?

onewithzachy
2271
onewithzachy 2012.03.20 15:52  
onewithzachy:

Haven't check brewmanz's GetWinUtcDateTime() codes, but thanks any way.

Man, this topic is old, but not old school.

BTW, that was typedef struct out, any sample for typedef struct in ?

LOL.

Dumb Q.

brewmanz's GetWinUtcDateTime() codes is using typedef struct in :)

AutoAbacus
25
AutoAbacus 2014.06.26 23:54  

A demo script follows for accessing GetSystemTime(), GetLocalTime(), and GetTimeZoneInformation() in MT4 Build 600+ where having structs available makes things much simpler and clearer than was the case in the old MT4 as shown by the previous posts in this thread.

Its output for me today in Eastern Australia was  TimeCallsScript Output

Here is the code, which is more lines of comments than actual code:

/* TimeCallsScript.mq4
   Script to demonstrate making kernel32.dll time related calls in MT4 Build 600+

GetSystemTime() and GetLocalTime()
-------------       ------------
From http://msdn.microsoft.com/en-us/library/ms724390%28v=vs.85%29.aspx
void WINAPI GetSystemTime(
  _Out_  LPSYSTEMTIME lpSystemTime
);

From http://msdn.microsoft.com/en-us/library/ms724338%28v=vs.85%29.aspx
void WINAPI GetLocalTime(
  _Out_  LPSYSTEMTIME lpSystemTime
);

Windows structure for a GetSystemTime() or GetLocalTime() call from http://msdn.microsoft.com/en-us/library/ms724950%28v=vs.85%29.aspx
typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wDay;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
*/

// MT4 equivalent struct:
struct SYSTEMTIME {
  ushort wYear;         // 2014 etc
  ushort wMonth;        // 1 - 12
  ushort wDayOfWeek;    // 0 - 6 with 0 = Sunday
  ushort wDay;          // 1 - 31
  ushort wHour;         // 0 - 23
  ushort wMinute;       // 0 - 59
  ushort wSecond;       // 0 - 59
  ushort wMilliseconds; // 0 - 999
};

/*
GetTimeZoneInformation()
----------------------
From http://msdn.microsoft.com/en-us/library/ms724421%28v=vs.85%29.aspx

DWORD WINAPI GetTimeZoneInformation(
  _Out_  LPTIME_ZONE_INFORMATION lpTimeZoneInformation
);

Windows struct for a GetTimeZoneInformation() call from http://msdn.microsoft.com/en-us/library/ms725481%28v=vs.85%29.aspx
typedef struct _TIME_ZONE_INFORMATION {
  LONG       Bias;
  WCHAR      StandardName[32];
  SYSTEMTIME StandardDate;
  LONG       StandardBias;
  WCHAR      DaylightName[32];
  SYSTEMTIME DaylightDate;
  LONG       DaylightBias;
} TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION;
*/

// MT4 equivalent struct
struct TIME_ZONE_INFORMATION {
  int        Bias;
  ushort     StandardName[32];
  SYSTEMTIME StandardDate;
  int        StandardBias;
  ushort     DaylightName[32];
  SYSTEMTIME DaylightDate;
  int        DaylightBias;
};

#import "kernel32.dll"
void GetSystemTime(SYSTEMTIME &SystemTimeStruct);
void  GetLocalTime(SYSTEMTIME &LocalTimeStruct);
int  GetTimeZoneInformation(TIME_ZONE_INFORMATION &TimeZoneInformationStruct);
#import

#define TIME_ZONE_ID_UNKNOWN   0
#define TIME_ZONE_ID_STANDARD  1
#define TIME_ZONE_ID_DAYLIGHT  2

void OnStart() {
  string s, retS;
  SYSTEMTIME st, lt;
  TIME_ZONE_INFORMATION tzi = {0}; // initialised to 0 in case the call fails
  PrintFormat("Size of SYSTEMTIME=%d", sizeof(SYSTEMTIME));                       // Gives  16 which is correct
  PrintFormat("Size of TIME_ZONE_INFORMATION=%d", sizeof(TIME_ZONE_INFORMATION)); // Gives 172 which is correct
  GetSystemTime(st);
   GetLocalTime(lt);
  switch (GetTimeZoneInformation(tzi)) {
    case TIME_ZONE_ID_UNKNOWN:  retS = "TIME_ZONE_ID_UNKNOWN";  break;
    case TIME_ZONE_ID_STANDARD: retS = "TIME_ZONE_ID_STANDARD"; break;
    case TIME_ZONE_ID_DAYLIGHT: retS = "TIME_ZONE_ID_DAYLIGHT"; break;
    default: retS = "GetTimeZoneInformation() call failed"; break;
  }
  s  = StringFormat("System Time\nYear: %d\nMonth: %d\nDay of week: %d\nDay of month: %d\nHour: %d\nMinute: %d\nSecond: %d\nMillisecond: %d",
                   st.wYear, st.wMonth, st.wDayOfWeek, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  s += StringFormat("\n\nLocal Time\nYear: %d\nMonth: %d\nDay of week: %d\nDay of month: %d\nHour: %d\nMinute: %d\nSecond: %d\nMillisecond: %d",
                   lt.wYear, lt.wMonth, lt.wDayOfWeek, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
  s += StringFormat("\n\nTime Zone Information\nReturn: %s\nBias: %d\nStandard Name: %s\nStandardDate Month: %d\nStandard Bias: %d\nDaylight Name: %s\nDaylightDate Month: %d\nDaylight Bias: %d",
                   retS, tzi.Bias, ShortArrayToString(tzi.StandardName), tzi.StandardDate.wMonth, tzi.DaylightBias, ShortArrayToString(tzi.DaylightName), tzi.DaylightDate.wMonth, tzi.DaylightBias);
  MessageBox(s, "Time Information");
}

For full information on what the various struct members mean see the Windows documentation links in the comments in the code, or it can be found easily by searching.

12
To add comments, please log in or register