Notes on Implementing a Timer

I’m currently working on a project at work that includes a timer that will turn relays on during certain time periods and off outside them. The timer wasn’t nearly as easy as I thought it would be so I decided to prototype the code on the PC as it’s a lot easier to debug than on a tiny little PIC18F. All of the code in this post will be in C# which I what I used to prototype with.

The weird thing about this timer is that the day is assumed change at 3am and not midnight. So a day starts at 3am and ends at 2:59am, this is apparently so that the relay can be on across midnight. This caused massive confusion and I struggled to implement it.

The idea is that the timer will store and work with an internal representation of time and then convert to hours/minutes only when displaying settings to the user and when getting the time from the RTC. This internal representation is a single integer number of minutes from midnight offset in such a way that times less than 03:00am are offset by a day. This means that 03:00am is 180 minutes as you would expect but 02:55 is actually 1615 minutes. Using this representation means that comparing time is really easy and that makes the whole timer and user interface much easier to implement. Below are some constants

const int MinutesPerDay = 1440;
const int MinInternalTime = 180;
const int MaxInternalTime = 1619;

Below is a function that converts hours and minutes to the internal representation. If the time is less than the minimum internal time then it’ll add a whole day to the time.

int TimeToInternal(int hrs, int min)
{
    int time = hrs*60 + min;
    if( time < MinInternalTime) {
        time += MinutesPerDay;
    }
    return time;
}

Next I needed a function to convert the internal representation to actual time, in hours and minutes. Here the time is returned as a tuple of two integers, in the C version I used two pointer parameters instead.

Tuple<int, int> InternalToTime(int time)
{
    if( time > MinutesPerDay ) {
        time -= MinutesPerDay;
    }
    return new Tuple<int, int>(time/60, time%60);
}

I have also implemented a similar timer elsewhere in the project, but it was much simpler and the day changed at midnight as you would expect. This is the code I used to implement the timer that turns on a feature during a time period, then turns off. This period can span across midnight. The time is really simple, just multiply hours by 100 then add the minutes so 11:32 is 1132.

bool IsInPeriod(int time, Period period)
{
    if( period.Start < period.Stop ) {
        return (time >= period.Start) && (time <= period.Stop);
    } else if ( period.Start > period.Stop ) {
        return (time >= period.Start) || (time <= period.Stop);
    } else {
        return false;
    }
}

Here is the Period class as I implemented it, nothing special or interesting here

public class Period {
    public Period() {
    }
     
    public Period(int start, int stop) {
        this.Start = start;
        this.Stop = stop;
    }
     
    public static int HrsMinToInt(int hrs, int min) {
        return hrs*100 + min;
    }
     
    public int Start { get; set; }
    public int Stop { get; set; }
}

Sorry if none of this make any sense, this post has mostly just been a brain dump so that if in the future I work on this again, or something similar I can remind myself of all this.