Hello,
I'm trying to write a piece of code to look at the current time set using the Time.h library, and determine whether or not this point in time is a point of time where daylight savings time is in effect.
For this, I'm not getting fancy with multiple states or countries, I'm just looking at US daylight savings time, in the areas that observe it. (Starts second Sunday in March at 2AM and ends first Sunday in November at 2AM.)
I wrote this code that I think would properly return a 0 or a 1 to indicate if DST is in effect at the currently set time. Does this look right? First, I'm saying if the month is anywhere between March and November, DST should be 1.
Then I check to see if the month is March. If the day is greater than 14, meaning the second Sunday must have already passed, DST is 1. If it's less than 8, meaning the second Sunday couldn't have happened yet, DST is 0. If the day is between those two, then it runs through if statements for each day and checks if the day of the week is Sunday and if the hour is greater than 1. If both of those are the case, it sets DST to 1. Otherwise, it leaves it at 0. (Actually, I guess one of those must be Sunday, so I could probably get rid of the last else.)
Then, it kind of does the inverse if the month is November to determine if the day is within the range that might be the first Sunday in November, and if the day is indeed Sunday, and if the hour is greater than 1, meaning DST should go back to 0.
I'm sure there is a much cleaner way to do this, and I'm probably this code will probably hurt most peoples' eyes. But, at my current skill level, a bunch of nested if statements is all I could come up with to try to test the date to see if it's within the DST time of the year or not. Mainly I just want to see if there is some flaw in my logic here.
Thanks.
if (month() > 3 && month() < 11)
{
DST = 1;
}
else if (month() == 3)
{
if (day() > 14)
{
DST = 1;
}
else if (day() < 8)
{
DST = 0;
}
else if (day() == 8)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 9)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 10)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 11)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 12)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 13)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else if (day() == 14)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 1;
}
}
}
else
{
DST = 0;
}
}
else if (month() == 11)
{
if (day() > 7)
{
DST = 0;
}
else if (day() == 1)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 2)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 3)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 4)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 5)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 6)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else if (day() == 7)
{
if (weekday() == 1)
{
if (hour() > 1)
{
DST = 0;
}
}
}
else
{
DST = 1;
}
}