Libraries: daylight

 

daylight:

Short description.

Author: amrali

 

Update  2021.04.10

More simplified computation of the daylight saving time changes.

Adopted from the Clock library of takumi

https://www.mql5.com/en/code/34301

clock
clock
  • www.mql5.com
Extension for the datetime with DayLightSavingTime adjustments.
 

In your GetNthSunday() function, you have :

datetime dt=StructToTime(st); // get date of first of month
...
TimeToStruct(dt,st); 
How is that useful to add the 'TimeToStruct' ? as you started from st to get dt, is there any side effect I am missing ?
 
Alain Verleyen #:

In your GetNthSunday() function, you have :

How is that useful to add the 'TimeToStruct' ? as you started from st to get dt, is there any side effect I am missing ?
The call to TimeToStruct() is needed to populate other unintialized fields of the MqlDateTime struct including the "st.day_of_week". After the call, you can get the day of week (i.e., ordinal number) of the first day of month.

For example, if the first day of month happens to be Friday (value of 5), then you must add 2 days after Friday in order to get the first Sunday of that month, that is Sunday = the 3rd day.

The number of days to be added is calculated as: FirstSunday = (7-st.day_of_week)%7
 
amrali #:
The call to TimeToStruct() is needed to populate other unintialized fields of the MqlDateTime struct including the "st.day_of_week". After the call, you can get the day of week (i.e., ordinal number) of the first day of month.

For example, if the first day of month happens to be Friday (value of 5), then you must add 2 days after Friday in order to get the first Sunday of that month, that is Sunday = the 3rd day.

The number of days to be added is calculated as: FirstSunday = (7-st.day_of_week)%7

I see. Missed that, thank you.