public static OpeningHours FromString(string valueStr) { if (String.IsNullOrWhiteSpace(valueStr)) return null; var parts = valueStr.Split(new[] { ' ', ':', '-' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 5) return null; var newObject = new OpeningHours(); try { newObject.OpeningHour = Convert.ToInt32(parts[1]); newObject.OpeningMinute = Convert.ToInt32(parts[2]); newObject.ClosingHour = Convert.ToInt32(parts[3]); newObject.ClosingMinute = Convert.ToInt32(parts[4]); switch (parts[0]) { case "Su": newObject.DayOfWeek = Day.Sunday; break; case "Mo": newObject.DayOfWeek = Day.Monday; break; case "Tu": newObject.DayOfWeek = Day.Tuesday; break; case "We": newObject.DayOfWeek = Day.Wednesday; break; case "Th": newObject.DayOfWeek = Day.Thursday; break; case "Fr": newObject.DayOfWeek = Day.Friday; break; case "Sa": newObject.DayOfWeek = Day.Saturday; break; default: return null; } } catch { return null; } return newObject; }
public void OpeningHoursBasicTest() { var time = new OpeningHours { DayOfWeek = Day.Wednesday, OpeningHour = 15, OpeningMinute = 45, ClosingHour = 23, ClosingMinute = 30 }; var str = time.ToString(); var newTime = OpeningHours.FromString(str); Assert.IsNotNull(newTime); Assert.AreEqual(Day.Wednesday, newTime.DayOfWeek); Assert.AreEqual(15, newTime.OpeningHour); Assert.AreEqual(45, newTime.OpeningMinute); Assert.AreEqual(23, newTime.ClosingHour); Assert.AreEqual(30, newTime.ClosingMinute); }