I want to write some sort of dictionary that would accept current time
and return what user-define time period it is. As time-preiod I mean user-defined names for time period that are read from file. For example
8:00-9:00 – breakfast time
9:00-12:00 – work time
12:00-13:00 – lunch time
etc…
Currently I have a function is base on if-else if statements
// C# syntax omitted for simplicity
TimePeriod GetCurrentTimePeriod(DateTime t)
{
if(t < 8.00)
{
return TimePeriod.Sleep;
}
else if(t < 9.00)
{
...
}
}
I am not happy with this straight solution. I would prefer use some
sort of “dictionary” instead.
PS. Changed the word TimeZone to TimePeriod to avoid confusion with System.TimeZone class.
If you need to make those “periods of time” dynamically adjustable, I suggest building up a simple List of a record containing start time, end time, and description and simply query that list with LINQ. Since the number of items in that list is probably likely to be very small, linear search does the job and it isn’t worth it to bother using binary search or more advanced data structures like segment trees: