can provide a new funtion which can judge a datetime is DaylightSavingTime(DST) or not ,like java

 

java:

ZoneId zoneId = ZoneId.of("America/New_York");

LocalDateTime localDateTime = LocalDateTime.of(2023, 05, 30, 15, 16, 17);//datetime in java
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
System.out.println(zonedDateTime);

ZoneRules rules = zoneId.getRules();
boolean result = rules.isDaylightSavings(zonedDateTime.toInstant());
System.out.println("the time is dst or not:"+result);


in mt5 like this will be great.

datetime dt = D'2023.05.30 15:16:17';
bool res = isDaylightSavings("America/New_York",dt);


i already read ariticle about "deal with time" many times, part 2 is so complex for me who is beginer.  

thanks a lot.


Dealing with Time (Part 1): The Basics - MQL5 Articles

Dealing with Time (Part 2): The Functions - MQL5 Articles

Dealing with Time (Part 1): The Basics
Dealing with Time (Part 1): The Basics
  • www.mql5.com
Functions and code snippets that simplify and clarify the handling of time, broker offset, and the changes to summer or winter time. Accurate timing may be a crucial element in trading. At the current hour, is the stock exchange in London or New York already open or not yet open, when does the trading time for Forex trading start and end? For a trader who trades manually and live, this is not a big problem.
 
See this, it might help you
 
chuangmingjijing:

java:


in mt5 like this will be great.


i already read ariticle about "deal with time" many times, part 2 is so complex for me who is beginer.  

thanks a lot.


Dealing with Time (Part 1): The Basics - MQL5 Articles

Dealing with Time (Part 2): The Functions - MQL5 Articles

This is my current method for calculating daylight saving time. Maybe you can refer to it. I hope it will be helpful to you.

input string G_GMTandDSTSettingn = "---------- GMT and DST setting ----------"; // |
input string G_BrokerGMTOptions  = "The time zone of most brokers is GMT+2.";
input int    G_BrokerGMTType     = 2;
input string G_BrokerDSTOptions  = "0=United States, 1=European Union, 2=On, 3=Off.";
input int    G_BrokerDSTType     = 0;

bool AutoDST()
{
   MqlDateTime mqlTime;
   TimeToStruct(TimeCurrent(), mqlTime);
   if (G_BrokerDSTType == 0)
   { 
      MqlDateTime firstDayTimeMarch, firstDayTimeNovember;
      firstDayTimeMarch.year = mqlTime.year;
      firstDayTimeMarch.mon  = 3;
      firstDayTimeMarch.day  = 1;
      TimeToStruct(TimeCurrent(firstDayTimeMarch), firstDayTimeMarch);
      int L_firstSundayOfMarch  = (firstDayTimeMarch.day_of_week == 0) ? 1 : (8 - firstDayTimeMarch.day_of_week + 1);
      int L_secondSundayOfMarch = L_firstSundayOfMarch + 7;
      firstDayTimeNovember.year = mqlTime.year;
      firstDayTimeNovember.mon  = 11;
      firstDayTimeNovember.day  = 1;
      TimeToStruct(TimeCurrent(firstDayTimeNovember), firstDayTimeNovember);
      int L_firstSundayOfNovember = (firstDayTimeNovember.day_of_week == 0) ? 1 : (8 - firstDayTimeNovember.day_of_week + 1);
      bool L_USADSTOpen  = mqlTime.mon >  3 || (mqlTime.mon ==  3 && mqlTime.day >= L_secondSundayOfMarch  );
      bool L_USADSTClose = mqlTime.mon < 11 || (mqlTime.mon == 11 && mqlTime.day <  L_firstSundayOfNovember);
      if (L_USADSTOpen && L_USADSTClose)
      {
         return (true);
      }
      else
      {
         return (false);
      }
   }
   else if (G_BrokerDSTType == 1)
   {
      MqlDateTime specificDayTimeMarch, specificDayTimeOctober;
      int L_lastSundayOfMarch   = 31;
      int L_lastSundayOfOctober = 31;
      specificDayTimeMarch.year   = mqlTime.year;
      specificDayTimeMarch.mon    = 3;
      specificDayTimeOctober.year = mqlTime.year;
      specificDayTimeOctober.mon  = 10;
      for(; L_lastSundayOfMarch > 0; L_lastSundayOfMarch--)
      {
         specificDayTimeMarch.day = L_lastSundayOfMarch;
         TimeToStruct(TimeCurrent(specificDayTimeMarch), specificDayTimeMarch);
         if(specificDayTimeMarch.day_of_week == 0)
         {
            break;
         }
      }
      for(; L_lastSundayOfOctober > 0; L_lastSundayOfOctober--)
      {
         specificDayTimeOctober.day = L_lastSundayOfOctober;
         TimeToStruct(TimeCurrent(specificDayTimeOctober), specificDayTimeOctober);
         if(specificDayTimeOctober.day_of_week == 0)
         {
            break;
         }
      }
      bool L_EUDSTOpen  = mqlTime.mon >  3 || (mqlTime.mon ==  3 && mqlTime.day >= L_lastSundayOfMarch  );
      bool L_EUDSTClose = mqlTime.mon < 10 || (mqlTime.mon == 10 && mqlTime.day <  L_lastSundayOfOctober);
      if (L_EUDSTOpen && L_EUDSTClose)
      {
         return (true);
      }
      else
      {
         return (false);
      }
   }
   else if (G_BrokerDSTType == 2)
   {
      return (true);
   }
   else
   {
      return (false);
   }
}
 
amrali #:
See this, it might help you
thank you so much for your code with clarity of thought!
 
Hong Yi Li #:
helpful

thanks a lot . so helpful for me.

Dealing with this problem is difficult for beginners. 

 
chuangmingjijing #:

thanks a lot . so helpful for me.

Dealing with this problem is difficult for beginners. 

I forgot to attach how to apply the function.

I have seen many EAs on the market that do not use the correct calculation method, which results in inaccurate calculated times.

if ( HourTime(Yes, true, 13, 16, AutoDST()) )
{
        YourOpeningFunction();
}

bool HourTime(string P_Type, bool P_Switch, int P_OpenTime, int P_CloseTime, bool P_DST)
{
   MqlDateTime mqlTime;
   TimeToStruct(TimeCurrent(),mqlTime);
   int L_Hour = mqlTime.hour;
   if (!P_Switch)
   {
      return (true);
   }
   else if (P_DST)
   {
      bool L_OpenTime  = (P_OpenTime  + G_BrokerGMTType - 1 + 24) % 24 <= mqlTime.hour;
      bool L_CloseTime = (P_CloseTime + G_BrokerGMTType - 1 + 24) % 24 >= mqlTime.hour;
      if (P_Type == "Yes")
      {
         if (L_OpenTime && L_CloseTime) return (true); else return (false);
      }
      else if (P_Type == "No") 
      {
         if (L_OpenTime && L_CloseTime) return (false); else return (true);
      }
   }
   else
   {
      bool L_OpenTime  = (P_OpenTime  + G_BrokerGMTType - 0 + 24) % 24 <= mqlTime.hour;
      bool L_CloseTime = (P_CloseTime + G_BrokerGMTType - 0 + 24) % 24 >= mqlTime.hour;
      if (P_Type == "Yes")
      {
         if (L_OpenTime && L_CloseTime) return (true); else return (false);
      }
      else if (P_Type == "No") 
      {
         if (L_OpenTime && L_CloseTime) return (false); else return (true);
      }
   }
   Print ("The time function is not set correctly!");
   return (false);
}