private void GetSinglePartSuggestions(DatePartParser part, List <Suggestion> newSuggestions, DateTime currentDate) { // Check Possible Day (Of Month) if ((part.DateType & (DateTypeFlags.PossibleDayOfMonth | DateTypeFlags.DefiniteDayOfMonth)) > 0) { // Possible or definite day of month var suggestedDate = currentDate.SetDayOfMonth(part.DayOfMonthValue); var probability = part.DayOfMonthProbability; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Day of Week if ((part.DateType & DateTypeFlags.DefiniteDayOfWeek) > 0) { // Definite day of week // Next Day var suggestedDate = currentDate.NextDayOfWeek(part.DayOfWeekValue); var probability = 70; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } // Previous Day suggestedDate = currentDate.PreviousDayOfWeek(part.DayOfWeekValue); probability = 40; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Possible Month if ((part.DateType & (DateTypeFlags.PossibleMonth | DateTypeFlags.DefinitieMonth)) > 0) { // Possible or definite month var suggestedDate = currentDate.SetMonth(part.MonthValue); var probability = part.MonthProbability; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Possible Year if ((part.DateType & (DateTypeFlags.PossibleYear | DateTypeFlags.DefiniteYear)) > 0) { // Possible or definite year var suggestedDate = currentDate.SetYear(part.YearValue); var probability = part.YearProbability; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } }
public IEnumerable <Suggestion> GetFirstSuggestion(string searchText, object currentValue) { var currentDate = currentValue is DateTime ? (DateTime)currentValue : DateTime.Now; var newSuggestions = new List <Suggestion>(); // Only attempt to work out a relative time if the search string start with + or - if (searchText.Length < 2) { return(newSuggestions); } var firstCharacter = searchText[0]; if (firstCharacter != '-' && firstCharacter != '+') { return(newSuggestions); } // Find the parts in the time. var hours = 0; var minutes = 0; var matchFound = false; int val; var relativeTimeText = searchText.ToLower().Trim(firstCharacter); // Recognise parts with format like nnh nnm where nn is a number var matches = dmyRegex.Matches(relativeTimeText); foreach (Match m in matches) { if (int.TryParse(m.Groups[1].Value, out val) && val != 0) { switch (m.Groups[2].Value) { case "h": hours = val; break; case "m": minutes = val; break; } matchFound = true; } } if (!matchFound) { // Try a non specific match (assume minutes) var m = simpleRegex.Match(relativeTimeText); if (int.TryParse(m.Groups[0].Value, out val) && val != 0) { minutes = val; matchFound = true; } } if (matchFound) { if (firstCharacter == '-') { hours = -hours; minutes = -minutes; } var suggestedTime = ((DateTime)currentValue) .AddHours(hours) .AddMinutes(minutes); var s = new Suggestion ( "", suggestedTime.ToShortTimeString(), "", 100, suggestedTime ); newSuggestions.Add(s); } return(newSuggestions); }
private void GetTwoPartSuggestions(DatePartParser firstPart, DatePartParser secondPart, List <Suggestion> newSuggestions, DateTime currentDate) { // Check Possible Day (Of Month) and Month if ((firstPart.DateType & (DateTypeFlags.PossibleDayOfMonth | DateTypeFlags.DefiniteDayOfMonth)) > 0 && (secondPart.DateType & (DateTypeFlags.PossibleMonth | DateTypeFlags.DefinitieMonth)) > 0) { var suggestedDate = currentDate.SetDayOfMonth(firstPart.DayOfMonthValue).SetMonth(secondPart.MonthValue); var probability = (firstPart.DayOfMonthProbability + secondPart.MonthProbability) / 2; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Possible Month and Day of Month if ((secondPart.DateType & (DateTypeFlags.PossibleDayOfMonth | DateTypeFlags.DefiniteDayOfMonth)) > 0 && (firstPart.DateType & (DateTypeFlags.PossibleMonth | DateTypeFlags.DefinitieMonth)) > 0) { var suggestedDate = currentDate.SetDayOfMonth(secondPart.DayOfMonthValue).SetMonth(firstPart.MonthValue); var probability = (secondPart.DayOfMonthProbability + firstPart.MonthProbability) / 2; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Day of Week and Day (Of Month) if ((firstPart.DateType & DateTypeFlags.DefiniteDayOfWeek) > 0 && (secondPart.DateType & (DateTypeFlags.PossibleDayOfMonth | DateTypeFlags.DefiniteDayOfMonth)) > 0) { var suggestedDate = currentDate.NextDate(firstPart.DayOfWeekValue, secondPart.DayOfMonthValue); var probability = 70; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } suggestedDate = currentDate.PreviousDate(firstPart.DayOfWeekValue, secondPart.DayOfMonthValue); probability = 60; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } // Check Possible Month and year if ((firstPart.DateType & (DateTypeFlags.PossibleMonth | DateTypeFlags.DefinitieMonth)) > 0 && (secondPart.DateType & (DateTypeFlags.PossibleYear | DateTypeFlags.DefiniteYear)) > 0) { var suggestedDate = currentDate.SetMonth(firstPart.MonthValue).SetYear(secondPart.YearValue); var probability = (firstPart.MonthProbability + secondPart.YearProbability) / 2; if (currentDate != suggestedDate) { var s = new Suggestion ( "", suggestedDate.ToShortDateString(), "", probability, suggestedDate ); newSuggestions.Add(s); } } }