It all comes down to determining the dates of DST changes (for CET this is the last Sunday of March and the last Sunday of October). Obviously, the last Sunday will be between 25th and 31st of those months. So, for an arbitrary date and day of the week, calculating the date of previous Sunday will determine the DST flag. If we (conveniently) mark the days of the week like Sunday=0, Monday=1,..., Saturday=6, then our calculation of the previous Sunday becomes:
Code: Select all
PrevSunday = Day - DayOfTheWeek;
Code: Select all
short IsDST(short day, short month, short dow) {
if (month < 3 || month > 10) return 0;
if (month > 3 && month < 10) return 1;
if (month == 3) return day - dow >= 25;
return day - dow < 25;
}
Also, most RTC chips have DayOfTheWeek register, but you can also calculate it from the date (day, month and year):
Code: Select all
int dow(int y, int m, int d) {
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
Code: Select all
int dow(int y, int m, int d) {
return (d += m<3 ? y-- : y-2, 23*m/9 + d + 4 + y/4 - y/100 + y/400) % 7;
}
Regards