2

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:

  1. An activity can only occur once on any given day. I.e., the Morning Meeting cannot occur twice on Tuesday.
  2. 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.
  3. 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.
  4. 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?

4

2 回答 2

2

时间段库可以帮助您解决这个问题

于 2013-04-11T14:22:07.847 回答
1

我认为如果你每天维护一个类,在你的类中有一个数组,并使用索引器访问它们,你会发现你的代码更容易使用:

public class ActivityDay
{
    public bool Active { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
}

public class DailyActivityTime
{
    public int WeeklyActivityTimeId { get; set; }
    public bool Biweekly { get; set; }
    public string DisplayText { get; set; }
    private ActivityDay[] _days = new ActivityDay[7];

    public ActivityDay this[int day]
    {
        get { return _days[day]; }
        set { _days[day] = value; }
    }
}

So Sundaybecome foo[0], Mondayisfoo[1]等。想想看,您可以使用DayOfWeek 枚举作为索引。

您可以创建ActivityDay一个结构,但随后您会遇到相当不方便的值语义。

于 2013-04-11T14:20:10.713 回答