Amateur radio, programming, electronics and other musings

Adding Eastern Standard Time to the Thunderbolt Monitor

Adding Eastern Standard Time to the Thunderbolt Monitor

I received an email from someone in the US who had just built one of my Thunderbolt Monitors. He casually asked if there was a way of showing EST rather than UTC on the display. Whilst on a computer this would be easy, it is a little harder on NETMF due to the fact it doesn’t understand timezones. It isn’t just a case of subtracting 5 hours from the UTC time as we also need to consider daylight savings time.

After some research, I put together a code change which appears to work for him and should handle the daylight savings rules too. Before I share the code I must warn you that this is a little rough but solves the problem. I will not be releasing this in the main branch but you should be able to hack it in if you really want it.

Add the following to the end of the class of program.cs.

public static bool IsDaylightSaving(DateTime dt)
{
    var dow = (int)(dt.DayOfWeek);

    // January, February, and December are out.
    if (dt.Month < 3 || dt.Month > 11) { return false; }

    // April to October are in
    if (dt.Month > 3 && dt.Month < 11) { return true; }

    var previousSunday = dt.Day - dow;

    if (dt.Month == 3 && dt.Day >= 8 && dt.Day <= 14 && dow == 0)
    {
        return dt.Hour >= 2;
    }

    // In March, we are DST if our previous sunday was on or after the 8th.
    if (dt.Month == 3) { return previousSunday >= 8; }

    if (dt.Month == 11 && dt.Day >= 1 && dt.Day <= 7 && dow == 0)
    {
        return dt.Hour >= 2;
    }

    // In November we must be before the first Sunday to be DST.
    // That means the previous Sunday must be before the 1st.
    return previousSunday <= 0;

}
 
public static DateTime ConvertToEasternTime(DateTime dt)
{
    var utcOffsetHours = -5;
    if (IsDaylightSaving(dt))
    {
        utcOffsetHours += 1;
    }
    return dt.AddHours(utcOffsetHours);
}

Change references of :

string mode = _thunderbolt.TimingMode == TimingModes.UTC ? "U" : "G";
_lcdshield.WriteLine(0, DateTime.UtcNow.ToString(@"dd-MMM-yy " + mode + " HH:mm:ss"));

to

string mode = "E";
_lcdshield.WriteLine(0, ConvertToEasternTime(DateTime.UtcNow).ToString(@"dd-MMM-yy " + mode + " HH:mm:ss"));

I changed the U/G to an E to signify Eastern time but feel free to ignore that or even replace it with just a space instead.