public static bool TryParseTimeString(string time, out CourseSectionTime result)
        {
            MatchCollection collection = timeStringRegex.Matches(time);

            result = new CourseSectionTime();
            List <CourseSectionTimeSpan> meetTimes = new List <CourseSectionTimeSpan>();

            if (collection.Count > 0)
            {
                foreach (Match match in collection)
                {
                    CourseSectionTimeSpan span;
                    DayOfWeek             day;
                    switch (match.Groups["day"].ToString().ToLowerInvariant())
                    {
                    case "monday":
                        day = DayOfWeek.Monday;
                        break;

                    case "tuesday":
                        day = DayOfWeek.Tuesday;
                        break;

                    case "wednesday":
                        day = DayOfWeek.Wednesday;
                        break;

                    case "thursday":
                        day = DayOfWeek.Thursday;
                        break;

                    case "friday":
                        day = DayOfWeek.Friday;
                        break;

                    default:
                        return(false);
                    }
                    span.Day = day;

                    byte start, end;
                    if (!UTCourseSectionTimeSpan.TryParseTimeSpanInt(match.Groups["start"].ToString(), out start))
                    {
                        return(false);
                    }
                    if (!UTCourseSectionTimeSpan.TryParseTimeSpanInt(match.Groups["end"].ToString(), out end))
                    {
                        return(false);
                    }

                    meetTimes.Add(new CourseSectionTimeSpan(day, start, end));
                }
                result.MeetTimes = meetTimes;
                return(true);
            }
            return(false);
        }
        public static bool TryParseRawTime(string raw, out CourseSectionTime time)
        {
            time = new CourseSectionTime();
            if (raw.Equals("TBA"))
            {
                time.MeetTimes = new List <CourseSectionTimeSpan>();
                time.TBA       = true;
                return(true);
            }

            if (!rawTimeRegex.IsMatch(raw))
            {
                return(false);
            }

            List <CourseSectionTimeSpan> spans = new List <CourseSectionTimeSpan>();

            foreach (Capture capture in rawTimeRegex.Match(raw).Groups["span"].Captures)
            {
                CourseSectionTimeSpan span;
                string rawSpan = capture.ToString();
                if (!UTCourseSectionTimeSpan.TryParseRawTimeSpan(rawSpan, out span))
                {
                    return(false);
                }

                // Correct the abbreviation form, e.g. TF9 => T9F9
                if (span.Start != 0 || span.End != 0)
                {
                    for (int i = 0; i < spans.Count; i++)
                    {
                        CourseSectionTimeSpan prevSpan = spans[i];
                        if (prevSpan.Start == 0 && prevSpan.End == 0)
                        {
                            prevSpan.Start = span.Start;
                            prevSpan.End   = span.End;
                        }
                        spans[i] = prevSpan;
                    }
                }

                spans.Add(span);
            }

            time.MeetTimes = spans;
            return(true);
        }
Beispiel #3
0
        public static bool TryParseRawTime(string time, out CourseSectionTime result)
        {
            if (time == "TBA")
            {
                result = new CourseSectionTime()
                {
                    TBA = true
                };
                return(true);
            }
            Regex           regex      = new Regex("((?<day>(mo)|(tu)|(we)|(th)|(fr)) (?<start>[0-9][0-9]?:[0-9]{2}) (?<end>[0-9][0-9]?:[0-9]{2}))+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection collection = regex.Matches(time);

            result = new CourseSectionTime();
            List <CourseSectionTimeSpan> meetTimes = new List <CourseSectionTimeSpan>();

            if (collection.Count > 0)
            {
                foreach (Match match in collection)
                {
                    CourseSectionTimeSpan span;
                    DayOfWeek             day;
                    switch (match.Groups["day"].ToString().ToLowerInvariant())
                    {
                    case "mo":
                        day = DayOfWeek.Monday;
                        break;

                    case "tu":
                        day = DayOfWeek.Tuesday;
                        break;

                    case "we":
                        day = DayOfWeek.Wednesday;
                        break;

                    case "th":
                        day = DayOfWeek.Thursday;
                        break;

                    case "fr":
                        day = DayOfWeek.Friday;
                        break;

                    default:
                        return(false);
                    }
                    span.Day = day;

                    byte start, end;
                    if (!UTCourseSectionTimeSpan.TryParseTimeSpanInt(match.Groups["start"].ToString(), out start))
                    {
                        return(false);
                    }
                    if (!UTCourseSectionTimeSpan.TryParseTimeSpanInt(match.Groups["end"].ToString(), out end))
                    {
                        return(false);
                    }

                    meetTimes.Add(new CourseSectionTimeSpan(day, start, end));
                }
                result.MeetTimes = meetTimes;
                return(true);
            }
            return(false);
        }