/// <summary> /// Parses the TimeValue part to a list of hour and minute pairs. /// The TimeValue can be of the form [HH:mm tt;HH:mm tt;.......;HH:mm tt] /// </summary> /// <returns>a list of hour and minute pairs.</returns> /// <exception cref="RuleInvalidDataException"> /// If the TimeValue format is invalid or /// if the parsed hours and minutes are invalid (greater than 23 or 59 respectively) /// </exception> private IList <KeyValuePair <int, int> > ParseTimeValue() { IList <KeyValuePair <int, int> > hourMinPairs = new List <KeyValuePair <int, int> >(); if (TimeValue.Equals(String.Empty)) { //return a single time with hour and min set to zero hourMinPairs.Add(new KeyValuePair <int, int>(0, 0)); return(hourMinPairs); } //Parse out the times specified (delimited by ;) string[] timeValues = TimeValue.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < timeValues.Length; i++) { int hour = 0, min = 0; try { //Here we get the "6:00" part of "6:00 PM" and extract the hour and min parts string onlyTimePart = timeValues[i].Split(' ')[0]; hour += int.Parse(onlyTimePart.Split(':')[0]); min += int.Parse(onlyTimePart.Split(':')[1]); //12:35 AM is actually 00:35 //12:35 PM is not 24:35 if (timeValues[i].Contains("PM") && hour < 12) { hour += 12; } if (timeValues[i].Contains("AM") && hour == 12) { hour -= 12; } } catch (Exception e) { throw new RuleInvalidDataException( timeValues[i] + " must be in format: '[hour]:[minute] [AM/PM]'.", e); } if (hour > 23 || min > 59) { throw new RuleInvalidDataException(timeValues[i] + " is an invalid time."); } //Add each time parsed to the list. hourMinPairs.Add(new KeyValuePair <int, int>(hour, min)); } return(hourMinPairs); }
public void TestTimeValue_Equal() { TimeValue time1 = new TimeValue(1, 2, 3, 4); TimeValue time2 = new TimeValue(1, 2, 3, 4); TimeValue time3 = new TimeValue(4, 2, 2, 1); Assert.AreEqual(true, time1 == time2, "TimeValue == is incorrect."); Assert.AreEqual(true, time1 != time3, "TimeValue != is incorrect."); Assert.AreEqual(true, time1.Equals(time2), "TimeValue Equals is incorrect."); Assert.AreEqual(false, time1.Equals(time3), "TimeValue Equals false is incorrect."); }