Hi,
I made an function to determine whether DST is valid in the European Union for an arbitrary UnixTime code.
It uses the TimeLib.h library and makeTime() function.
/* ===============================================================
2020-06-10 - EvB
Function to determine whether DST is valid in the European Union
for an arbitrary UnixTime UTC code, using TimeLib.h and makeTime()
DST starts on last Sunday in March at 01:00:00 UTC in the EU
DST ends on last Sunday of October at 02:00:00 UTC in the EU
================================================================*/
#include <TimeLib.h>
tmElements_t dst_start; // time elements structure for DST summertime
tmElements_t dst_end; // time elements structure for DST wintertime
time_t ut_dstStart; // unix UTC timestamp when DST starts
time_t ut_dstEnd; // unix UTC timestamp when DST ends
// Function to determine DST status in Europe
byte detEUdstStatus( time_t moment ) {
byte dstStat = 0;
dst_start.Year = year(moment) - 1970; // years since 1970, so deduct 1970
dst_start.Month = 3; // DST starts in March
dst_start.Day = 31; // Last day of March
dst_start.Hour = 1; // DST starts at 01:00:00 UTC
dst_start.Minute = 0;
dst_start.Second = 0;
dst_end.Year = year(moment) - 1970; // years since 1970, so deduct 1970
dst_end.Month = 10; // DST ends in October
dst_end.Day = 31; // Last day of October
dst_end.Hour = 2; // DST ends at 02:00:00 UTC
dst_end.Minute = 0;
dst_end.Second = 0;
// ((weekday()-1)*86400UL) calculates number of secs to subtract to get the last Sunday in the month
ut_dstStart = ( makeTime(dst_start) - ( ( weekday( makeTime(dst_start) ) - 1 ) * 86400UL ) );
ut_dstEnd = ( makeTime(dst_end) - ( ( weekday( makeTime(dst_end) ) - 1 ) * 86400UL ) );
if ( moment >= ut_dstStart && moment < ut_dstEnd ) {
dstStat = 1;
}
return dstStat;
}
void setup(void) {
Serial.begin(57600);
while (!Serial) {
;
}
time_t unixTimeCode[8] = {
1591628148, // 2020-06-08T14:55:48+00:00 (UTC)
1582478299, // 2020-02-23T17:18:19+00:00 (UTC)
1293843661, // 2011-01-01T01:01:01+00:00 (UTC)
1883639349, // 2029-09-09T09:09:09+00:00 (UTC)
1585443599, // 2020-03-29T00:59:59+00:00 (UTC)
1585443600, // 2020-03-29T01:00:00+00:00 (UTC) DST start in 2020
1603591199, // 2020-10-25T01:59:59+00:00 (UTC)
1603591200 // 2020-10-25T02:00:00+00:00 (UTC) DST end in 2020
};
for ( byte i = 0; i < 8; i++ ) {
byte dstStatus = detEUdstStatus( unixTimeCode[i] );
if ( dstStatus == 1 ) {
Serial.print("DST is valid in Europe");
} else {
Serial.print("DST is NOT valid in Europe");
}
Serial.print(" for UnixTime UTC code ");
Serial.println(unixTimeCode[i]);
}
}
void loop() {
}
// Output:
//16:56:10.933 -> DST is valid in Europe for UnixTime UTC code 1591628148
//16:56:10.933 -> DST is NOT valid in Europe for UnixTime UTC code 1582478299
//16:56:10.933 -> DST is NOT valid in Europe for UnixTime UTC code 1293843661
//16:56:10.967 -> DST is valid in Europe for UnixTime UTC code 1883639349
//16:56:10.967 -> DST is NOT valid in Europe for UnixTime UTC code 1585443599
//16:56:10.967 -> DST is valid in Europe for UnixTime UTC code 1585443600
//16:56:11.001 -> DST is valid in Europe for UnixTime UTC code 1603591199
//16:56:11.001 -> DST is NOT valid in Europe for UnixTime UTC code 1603591200
Input is an arbitrary UTC UnixTime code, and output is 1 for DST and 0 for no DST.
It follows the current DST rules for the European Union.
It is working fine for me, but any improvements to the code would be appreciated.
If you want to modify it for another region you probably just have to tweak the time elements structures.
Ed