Detect automatically summer/winter time switch period between America and Europe?

 

Hi all.

Is it possible to detect automatically (script) the switch period between winter/summer time in America when i'm located in Europe?

In the United States, Summer Time begins on the second Sunday in March and reverts to standard time on the first Sunday in November.

In the European Union, Summer Time begins the last Sunday in March and ends the last Sunday in October.

Between these periods, there are 2/3 weeks in March and October that there is a time difference of 1 hour between Europe and America. My EA needs to detect this time change to adjust some trading time variables that are used for the event calendar (US: Jobless claims, PPI, CPI etc.).

Many thanks.

 
snelle_moda:

Hi all.

Is it possible to detect automatically (script) the switch period between winter/summer time in America when i'm located in Europe?

In the United States, Summer Time begins on the second Sunday in March and reverts to standard time on the first Sunday in November.

In the European Union, Summer Time begins the last Sunday in March and ends the last Sunday in October.

Between these periods, there are 2/3 weeks in March and October that there is a time difference of 1 hour between Europe and America. My EA needs to detect this time change to adjust some trading time variables that are used for the event calendar (US: Jobless claims, PPI, CPI etc.).

Many thanks.

Interesting to have this, but I don't know how. Have you ever done a search on the site (forum, codebase ...) ?
 

I could not find a solution on the site or the forum but I have figured out a way to do this.

This code is not yet finished but it gives an example how to detect, if the current date is between the 2e sunday of March and the last sunday March.

You need the day number and week number to solve this problem.

 

      
      MqlDateTime TimePeriod;        
      TimeGMT(TimePeriod);

      int nDayOfYear = TimePeriod.day_of_year;
      int nDayOfWeek = TimePeriod.day_of_week;

// month detection
//---------------------
// january     = 31  31   
// february    = 28  59
// march       = 31  90 
// april       = 30 120  
// may         = 31 151
// june        = 30 181
// july        = 31 212
// august      = 31 243
// september   = 30 273 
// october     = 31 304
// november    = 30 334
// december    = 31 365   

// Find the current month.
      int nMonthNumber = 0;
      int nMonthStartNumber = 0;
      int nMonthEndNumber = 0;
      if(nDayOfYear > 0 && nDayOfYear <= 31) 
            {
            nMonthNumber = 1; // january
            nMonthStartNumber = 0;
            nMonthEndNumber = 31;
            }
            else if(nDayOfYear > 31 && nDayOfYear <= 59)
            {
            nMonthNumber = 2; // february
            nMonthStartNumber = 31;
            nMonthEndNumber = 59;
            }
            else if(nDayOfYear > 59 && nDayOfYear <= 90)
            {
            nMonthNumber = 3; // march
            nMonthStartNumber = 59;
            nMonthEndNumber = 90;
            }      
            else if(nDayOfYear > 90 && nDayOfYear <= 120)
            {
            nMonthNumber = 4; // april
            nMonthStartNumber = 90;
            nMonthEndNumber = 120;
            }         
            else if(nDayOfYear > 120 && nDayOfYear <= 151)
            {
            nMonthNumber = 5; // may
            nMonthStartNumber = 120;
            nMonthEndNumber = 151;
            }      
            else if(nDayOfYear > 151 && nDayOfYear <= 181)
            {
            nMonthNumber = 6; // juny
            nMonthStartNumber = 151;
            nMonthEndNumber = 181;
            }        
            else if(nDayOfYear > 181 && nDayOfYear <= 212)
            {
            nMonthNumber = 7; // july
            nMonthStartNumber = 181;
            nMonthEndNumber = 212;
            }       
            else if(nDayOfYear > 212 && nDayOfYear <= 243)
            {
            nMonthNumber = 8; // august
            nMonthStartNumber = 212;
            nMonthEndNumber = 243;
            } 
            else if(nDayOfYear > 243 && nDayOfYear <= 273)
            {
            nMonthNumber = 9; // september
            nMonthStartNumber = 243;
            nMonthEndNumber = 273;
            } 
            else if(nDayOfYear > 273 && nDayOfYear <= 304)
            {
            nMonthNumber = 10; // october
            nMonthStartNumber = 273;
            nMonthEndNumber = 304;
            }     
            else if(nDayOfYear > 304 && nDayOfYear <= 334)
            {
            nMonthNumber = 11; // november
            nMonthStartNumber = 304;
            nMonthEndNumber = 334;
            }      
            else if(nDayOfYear > 334 && nDayOfYear <= 365)
            {
            nMonthNumber = 12; // december
            nMonthStartNumber = 334;
            nMonthEndNumber = 365;
            }  

// Confirmation boolean for the switch time period difference between USA en Europe.
// Current date >= 2e Sunday march && Current date < last Sunday march 
      static bool bSwitchTimePeriod = false; 
   
// Check if the current date is in the time switch period.    
      if(nMonthNumber == 3 || nMonthNumber == 10 || nMonthNumber == 11) // In this example only march is now used.
            {

// Set number of the first day of the year 01-01-20XX.      
            int nFirstDayOfYear = 0;
            int nDayNumberInThePast = nDayOfWeek;
            

            int nLastSundayCounter = 0; 
            static bool bLastSunDayDetection = false;
            for(int i = nDayOfYear + 28; i > 0; i--) // four addition weeks are added to start the check from the end of the month.
                  {

// Reset week.            
                  if(nDayNumberInThePast <= 0) // Reset day counter after one week has passed.
                        {
                        nDayNumberInThePast = 6;                       
                        }
                        else
                        {
                        nDayNumberInThePast--;
                        }                 
            
// calculate if current date is before the last sunday of the month           
                  if(i >= nDayOfYear && i <= nMonthEndNumber && i > nMonthStartNumber)
                        {
                      
// Check if day in the past is equal to sunday.                        
                        if(nDayNumberInThePast == 0) 
                              {
                              nLastSundayCounter++; 
                              }
                              
// Activate boolean for the last Sunday of the month detection                         
                        if(nLastSundayCounter >= 1)
                              {
                              bLastSunDayDetection = true;                                                         
                              }
                        }     
              
                  }

// Check the date has passed the 2e Sunday of the month.
            int nSecondSundayCounter = 0;  
            static bool bStartSunDayDetected = false;
            for(int i = 0; i < nDayOfYear; i++)
                  {


// Reset week.            
                  if(nDayNumberInThePast >= 6) // Reset day number after one week.
                        {
                        nDayNumberInThePast = 0;
                        }
                        else
                        {
                        nDayNumberInThePast++;
                        }  

// instellen van de week.
                  if(i <= nDayOfYear && i > nMonthStartNumber && i <= nMonthEndNumber)
                        {
                        
// Check if the day number in the past is equal to sunday.                        
                        if(nDayNumberInThePast == 0) 
                              {
                              nSecondSundayCounter++;
                              }

// Activate boolean for the 2e Sunday of the month detection                   
                        if(nSecondSundayCounter == 2)
                              {
                              bStartSunDayDetected = true;
                              Alert("kom ik hier? zondag gevonden", nDayNumberInThePast, " ", nSecondSundayCounter);

                              }
                        }                      
                  }    

// Check if both conditions for the time periods are valid.            
            if(bStartSunDayDetected == true && bLastSunDayDetection == true)
                  {
                  bSwitchTimePeriod = true; // ADJUST THE TIME VARIABLES!!!!
                  }
            
            }
 
snelle_moda:

Hi all.

Is it possible to detect automatically (script) the switch period between winter/summer time in America when i'm located in Europe?

In the United States, Summer Time begins on the second Sunday in March and reverts to standard time on the first Sunday in November.

In the European Union, Summer Time begins the last Sunday in March and ends the last Sunday in October.

Why not do something like this . . . 

#define SECONDSINADAY  86400


// Returns the datetime for Midnight on the last Sunday in March for this current year
datetime LastSundayMarch()
   {
   datetime Midnight, StartOfNewYear;
   int DaysInMarch = 31, DayIndex, StartOfMarch = 59, EndOfMarch = 90;  // start and end of March do not need to be accurate
   
   MqlDateTime TimeNow, TimeDayInMarch;
   TimeToStruct(TimeCurrent(), TimeNow);

   Midnight = TimeCurrent() - ( TimeCurrent()%SECONDSINADAY );  // midnight today as a datetime
   
   StartOfNewYear = Midnight - ( TimeNow.day_of_year * SECONDSINADAY);  // midnight 1st Jan this year
   
   DayIndex = StartOfMarch;
   while(DayIndex < EndOfMarch)
      { 
      DayInMarch = StartOfNewYear + ( DayIndex * SECONDSINADAY);  //datetime of Midnight on a day in March
      TimeToStruct(DayInMarch, TimeDayInMarch);
      if(TimeDayInMarch.mon == 3 && TimeDayInMarch.day_of_week == 0 && TimeDayInMarch.day > 25 )  // must be March, a Sunday and after the 25th
         return(DayInMarch); 
      
      DayIndex++;
      }
   }

 and similar for SecondSundayInMarch()  FirstSundayInNovember()  and LastSundayInOctober().  Then you can simply call the functions and have the relevant datetimes returned.

 

Please note:  Not compiled or tested,  there may be bugs. 

 

Sorry, since I don't observe DST, I have some Q and suggestion :

1. Is time in PC in DST countries automatically adjusted to DST or we have to manually adjust the time ? I think its automatically, because my Vista also shows current time in Sidney AU, and London UK, and they automatically adjusted to their own DST zone.

2. If PC time is automatically adjusted to DST, all snelle_moda need is current time of US city, like New York (NY) time for example. Since this NY time is automatically adjusted to DST, then there's should be no need to manually adjust time in EA.

Am I correct here ? 

 
phi.nuts:

Sorry, since I don't observe DST, I have some Q and suggestion :

1. Is time in PC in DST countries automatically adjusted to DST or we have to manually adjust the time ? I think its automatically, because my Vista also shows current time in Sidney AU, and London UK, and they automatically adjusted to their own DST zone.

2. If PC time is automatically adjusted to DST, all snelle_moda need is current time of US city, like New York (NY) time for example. Since this NY time is automatically adjusted to DST, then there's should be no need to manually adjust time in EA.

Am I correct here ? 

1.  Yes,  the PC would usually automatically adjust for DST unless the user has disabled that option.

2.  Not sure.  Maybe the OP will answer this one.  

 
RaptorUK:

1.  Yes,  the PC would usually automatically adjust for DST unless the user has disabled that option.

2.  Not sure.  Maybe the OP will answer this one.  

What is OP ?
 
angevoyageur:
What is OP ?
Original Poster. snelle_moda  in this case.
 
RaptorUK:

1.  Yes,  the PC would usually automatically adjust for DST unless the user has disabled that option.

2.  Not sure.  Maybe the OP will answer this one.  

Aha, thank you for the explanation.

So far the code I could find in code base is this but I don't check the code. 

 
phi.nuts:

Sorry, since I don't observe DST, I have some Q and suggestion :

1. Is time in PC in DST countries automatically adjusted to DST or we have to manually adjust the time ? I think its automatically, because my Vista also shows current time in Sidney AU, and London UK, and they automatically adjusted to their own DST zone.

2. If PC time is automatically adjusted to DST, all snelle_moda need is current time of US city, like New York (NY) time for example. Since this NY time is automatically adjusted to DST, then there's should be no need to manually adjust time in EA.

Am I correct here ? 

 

Hi 

When I would live in the USA, you're correct with your statement and my pc time would automatically change to the new summer time at (10-03-2013). BUT!!! I live the the Netherlands. In Europe, the summer time will be adjusted on the last Sunday on March 31-03-2013, thats 2.5 weeks from now. So between 10-03-2013 till 31-03-2013 there is a 1 hour time shift. All events in the USA are one hour earlier in Europe. My model needs to check this.     

 

I have a new version of the script with some improvements from the code of RaptorUK (thanks).

The script needs to detect if the current date is between the 2e sunday in march till the last sunday in march. 10-03-2013 t/m 31-03-2013  (the other months will be added later).

      MqlDateTime TimePeriod;        
      TimeGMT(TimePeriod);

// Confirmation boolean for the switch time period difference between USA en Europe.
// Current date >= 2e Sunday march && Current date < last Sunday march 
      static bool bSwitchTimePeriod = false; 
   
// Check if the current date is in the time switch period.    
      if(TimePeriod.mon == 3 || TimePeriod.mon == 10 || TimePeriod.mon == 11) // In this example only march is now used.
            {

// Day of the year since 01-01-20XX, first day is 0.  
            int nDayOfYear    = TimePeriod.day_of_year;  

            int nMonthStartNumber = 0; // Start day of the month.
            int nMonthEndNumber = 0;   // End day of the month.
            
// Find the current month.            
            if(TimePeriod.mon == 3) // March
                  {
                  nMonthStartNumber = 59;
                  nMonthEndNumber = 90;
                  } 
                  else if(TimePeriod.mon == 10) // October
                  {
                  nMonthStartNumber = 273;
                  nMonthEndNumber = 304;                  
                  }     
                  else if(TimePeriod.mon == 11) // November
                  {
                  nMonthStartNumber = 304;
                  nMonthEndNumber = 334;                  
                  }

// Set number of the current day.      
            int nDayNumberInThePast = TimePeriod.day_of_week;
            
            int nLastSundayCounter = 0; 
            static bool bLastSunDayDetection = false;
            
// Go back to the past (01-01-20XX + four weeks) and check how many sunday's have passed since the end of the month (march)
// and check on which day 01-01-20XX has started. For 2013, this is on Tuesday.             
            for(int i = nDayOfYear + 28; i > 0; i--) // four addition weeks are added, to start the loop check beyond the end of the month.
                  {

// Reset day number after one week has passed.           
                  if(nDayNumberInThePast <= 0) 
                        {
                        nDayNumberInThePast = 6;                       
                        }
                        else
                        {
                        nDayNumberInThePast--;
                        }                 
            
// Check this month since the current day.           
                  if(i >= nDayOfYear && i <= nMonthEndNumber && i > nMonthStartNumber)
                        {
                      
// Check if the day number in the past is equal to sunday.                        
                        if(nDayNumberInThePast == 0) 
                              {
                              nLastSundayCounter++; 
                              }
                              
// Activate the boolean if the last Sunday of the month is detected.                          
                        if(nLastSundayCounter >= 1)
                              {
                              bLastSunDayDetection = true;                                                         
                              }
                        }                  
                  }

// Check the date has passed the 2e Sunday of the month.
            int nSecondSundayCounter = 0;  
            static bool bStartSunDayDetected = false;
            
// Go back to the present and check how many sunday's have passed since the start of the month.            
            for(int i = 0; i < nDayOfYear; i++)
                  {

// Reset day number after one week has passed.           
                  if(nDayNumberInThePast >= 6) 
                        {
                        nDayNumberInThePast = 0;
                        }
                        else
                        {
                        nDayNumberInThePast++;
                        }  

// Check this month till the current day. 
                  if(i <= nDayOfYear && i > nMonthStartNumber && i <= nMonthEndNumber)
                        {
                        
// Check if the day number in the past is equal to sunday.                        
                        if(nDayNumberInThePast == 0) 
                              {
                              nSecondSundayCounter++;
                              }

// Activate the boolean if the 2e Sunday of the month is detected.                   
                        if(nSecondSundayCounter == 2)
                              {
                              bStartSunDayDetected = true;
                              }
                        }                      
                  }    

// Check if both conditions for the time periods are valid.            
            if(bStartSunDayDetected == true && bLastSunDayDetection == true)
                  {
                  bSwitchTimePeriod = true; // ADJUST THE TIME VARIABLES!!!!
                  }
            }