private static ScheduleRule ConvertHourTokenToRule(RuleDayOfWeek days, ScheduleToken hourToken) { switch (hourToken.Type) { case ScheduleTokenType.AllDay: case ScheduleTokenType.HourRange: byte startHour = hourToken.StartHour.Value; if (hourToken.StartMeridiem != HourMeridiem.None) { if (hourToken.StartMeridiem == HourMeridiem.AM) { if (startHour == 12) { startHour = 0; } } else if (startHour > 0 && startHour < 12) { startHour += 12; } } else if (startHour == 24) { startHour = 0; } short startMinute = (short)((startHour * 60) + (hourToken.StartMinute ?? 0)); byte endHour = hourToken.EndHour.Value; if (hourToken.EndMeridiem != HourMeridiem.None) { if (hourToken.EndMeridiem == HourMeridiem.AM) { if (endHour == 12) { endHour = 0; } } else if (endHour > 0 && endHour < 12) { endHour += 12; } } else if (endHour == 24) { endHour = 0; } short endMinute = (short)((endHour * 60) + (hourToken.EndMinute ?? 0)); if (hourToken.EndMeridiem == HourMeridiem.PM && hourToken.StartMeridiem == HourMeridiem.None && (startMinute + (12 * 60)) < endMinute && startMinute < 1440) { startMinute += (12 * 60); } else if (hourToken.EndMeridiem == HourMeridiem.None && startMinute > endMinute && startMinute < 720 && endMinute < 720) { endMinute += (12 * 60); } return new ScheduleRule(days, startMinute, endMinute); } return null; }
private static void ParseUntilTimeGroup(List<ScheduleToken> tokens, Match m) { byte untilHour, untilMinute; HourMeridiem untilMeridiem; ParseTime(m, "UntilHour", "UntilMinute", "UntilMeridiem", "UntilTime", out untilHour, out untilMinute, out untilMeridiem); ScheduleToken untilToken = new ScheduleToken(ScheduleTokenType.Until); untilToken.EndHour = untilHour; untilToken.EndMinute = untilMinute; untilToken.EndMeridiem = untilMeridiem; tokens.Add(untilToken); }
public Schedule Parse(string s) { Console.WriteLine(s); s = s.ToLowerInvariant(); s = mDottedMeridiemPattern.Replace(s, delegate(Match meridienMatch) { Group am = meridienMatch.Groups["AM"]; Group pm = meridienMatch.Groups["PM"]; if (am.Success) { return "am"; } if (pm.Success) { return "pm"; } return meridienMatch.Value; }); Console.WriteLine(s); List<ScheduleToken> tokens = new List<ScheduleToken>(); Match m = mHoursPattern.Match(s); if (m.Success) { do { Console.WriteLine("\t{0}", m.Value); Group g = m.Groups["AllDay"]; if (g.Success) { ScheduleToken allDayToken = new ScheduleToken(ScheduleTokenType.AllDay); allDayToken.StartHour = 0; allDayToken.EndHour = 0; tokens.Add(allDayToken); continue; } g = m.Groups["Days"]; if (g.Success) { ParseDaysGroup(tokens, m, g); continue; } g = m.Groups["Hours"]; if (g.Success) { ParseHoursGroup(tokens, m); continue; } g = m.Groups["UntilTime"]; if (g.Success) { ParseUntilTimeGroup(tokens, m); continue; } g = m.Groups["MonthSpan"]; if (g.Success) { ParseMonthSpanGroup(tokens, m); continue; } g = m.Groups["DateSpan"]; if (g.Success) { ParseDateSpanGroup(tokens, m); continue; } g = m.Groups["SingleMonth"]; if (g.Success) { ParseSingleMonthGroup(tokens, m); continue; } g = m.Groups["Separator"]; if (g.Success) { tokens.Add(new ScheduleToken(ScheduleTokenType.Separator)); continue; } g = m.Groups["Closed"]; if (g.Success) { tokens.Add(new ScheduleToken(ScheduleTokenType.Closed)); continue; } g = m.Groups["Open"]; if (g.Success) { tokens.Add(new ScheduleToken(ScheduleTokenType.Open)); continue; } } while ((m = m.NextMatch()) != null && m.Success); } PrintTokens(tokens); string leftovers = mHoursPattern.Replace(s, string.Empty).Trim(); if (leftovers.Length > 0) { Console.WriteLine("Leftovers: {0}", leftovers); } Schedule schedule = ConvertTokensToSchedule(tokens); if (schedule != null) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Schedule:"); foreach (ScheduleRuleSpan rule in schedule.mPositiveRules) { sb.Append("\t"); rule.ToString(sb); sb.AppendLine(); } sb.AppendLine("Negative"); foreach (ScheduleRuleSpan rule in schedule.mNegativeRules) { sb.Append("\t"); rule.ToString(sb); sb.AppendLine(); } Console.WriteLine(sb.ToString()); } return schedule; }