This forum is in read-only mode now. You may discuss your questions on forums of MQL4.community and MQL5.community

Using DLL to determine Daylight Savings Time


Back to topics list
avatar
Trader 2006.08.08 03:54 
Hello all,

Does anyone have a code example to determine daylight savings time?

Thank you,
-charliev
avatar
Trader 2006.08.08 10:21 
see GetTimeZoneInformation function placed in the kernel32.dll

if it returns TIME_ZONE_ID_DAYLIGHT (2), then time should be shifted to summer time
avatar
Trader 2006.08.08 16:33 
see GetTimeZoneInformation function placed in the kernel32.dll

if it returns TIME_ZONE_ID_DAYLIGHT (2), then time should be shifted to summer time


Thanks Slawa, do you have a code example?
-charliev
avatar
Trader 2006.08.08 17:10 
local time sample in c++

   TIME_ZONE_INFORMATION tzinfo={0};
   int rettz=::GetTimeZoneInformation(&tzinfo);
   int tmzone=0;
   //---- standard or daylight both
   if(rettz!=TIME_ZONE_ID_UNKNOWN)  tmzone=tzinfo.Bias;
   //---- if daylight then modify
   if(rettz==TIME_ZONE_ID_DAYLIGHT) tmzone+=tzinfo.DaylightBias;
   time_t local_time=time(NULL)-tmzone*60;

avatar
Trader 2006.08.08 18:33 
local time sample in c++

   TIME_ZONE_INFORMATION tzinfo={0};
   int rettz=::GetTimeZoneInformation(&tzinfo);
   int tmzone=0;
   //---- standard or daylight both
   if(rettz!=TIME_ZONE_ID_UNKNOWN)  tmzone=tzinfo.Bias;
   //---- if daylight then modify
   if(rettz==TIME_ZONE_ID_DAYLIGHT) tmzone+=tzinfo.DaylightBias;
   time_t local_time=time(NULL)-tmzone*60;




Thank you Slawa, but I guess I need help parsing tzinfo to get DaylightBias under MT4.
-charliev
avatar
Trader 2006.08.09 06:25 
Slawa, although this was a complete solution you provided me in my "Millisecond Resolution" post... anything that could help me in this direction would be much appreciated.

Thanks,
-charliev

//+------------------------------------------------------------------+
//|                                                    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[]);
#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int TimeArray[4];
   int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
//----
   GetLocalTime(TimeArray);
   Print(GetLastError());
   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;
   Print(nYear,".",nMonth,".",nDay," ",nHour,":",nMin,":",nSec,":",nMilliSec);
//----
   return(0);
  }
//+------------------------------------------------------------------+

 

avatar
Trader 2006.08.09 12:58 

//+------------------------------------------------------------------+
//|                                                    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[]);
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;
//----
   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);
   string 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)+nSec;
   Print("Greenwich mean time is: ",TimeToStr(local_time+gmt_shift*60,TIME_DATE|TIME_SECONDS));
//---- winter time (nYear remains the current)
   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)
   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));
  }
//+------------------------------------------------------------------+


avatar
Trader 2006.08.09 20:39 
Slawa, WOW! Excellent... THANKS!
-charliev
Back to topics list