I have a table that stores daily activities, e.g., "Morning Meeting." These activities take place on single or multiple days of every week at varying times during the day. I need an algorithm for producing a "nice" description of an activity time. Currently, I'm working with this data structure:
public class DailyActivityTime
{
public int WeeklyActivityTimeId { get; set; }
public bool Sunday { get; set; }
public string SundayStartTime { get; set; }
public string SundayEndTime { get; set; }
public bool Monday { get; set; }
public string MondayStartTime { get; set; }
public string MondayEndTime { get; set; }
public bool Tuesday { get; set; }
public string TuesdayStartTime { get; set; }
public string TuesdayEndTime { get; set; }
public bool Wednesday { get; set; }
public string WednesdayStartTime { get; set; }
public string WednesdayEndTime { get; set; }
public bool Thursday { get; set; }
public string ThursdayStartTime { get; set; }
public string ThursdayEndTime { get; set; }
public bool Friday { get; set; }
public string FridayStartTime { get; set; }
public string FridayEndTime { get; set; }
public bool Saturday { get; set; }
public string SaturdayStartTime { get; set; }
public string SaturdayEndTime { get; set; }
public bool Biweekly { get; set; }
public string DisplayText { get; set; }
}
The Start
and End
times are persisted to SQL Server (2008 R2) as TIME
data type (hence coming into the C# code as TimeSpan
), and the bool
properties are, of course, BIT
. I want to write the conversion-to-description code in C# in the DailyActivityTime
class itself, either in the constructor or in the DisplayText
property getter.
So, for example, say the Morning Meeting activity occurs as follows:
Monday: 10:00-10:30AM
Tuesday: 10:00-10:30AM
Wednesday: 10:00-10:30AM
Thursday: 10:00-10:30AM
Friday: 10:00-10:30AM
Saturday: 5:00-6:00PM
Sunday: 5:00-6:00PM
I need this example to be displayed as:
Mon-Fri 10:00-10:30 AM, Sat & Sun 5:00-6:00 PM
Some basic facts:
- An activity can only occur once on any given day. I.e., the Morning Meeting cannot occur twice on Tuesday.
- Activities that occur on more than two days in a week that are not in sequence can be displayed in a comma-separated list, e.g.,
Mon, Wed, Fri 1:00-1:30 PM
. - Activities that are marked
Biweekly
would appear as "Every other..." and do not occur more than one day per week, e.g.,Every other Thurs 3:00-3:45 PM
. - Though I've created the table and the related class for this entity already, I'm totally open to storing this data in a different format if there's a better solution.
I'm pretty sure I can figure this out by assigning an integer sequence to each day of the week and then comparing times, but it seems like there might be a better algorithm for this, possibly utilizing the TimeSpan
structure. Anyone have any ideas?