/// <summary> /// This methods returns true if the given timestamp (expressed as a /// <see cref="System.DateTime.Ticks">DateTime.Ticks</see> value) matches the pattern, /// according to the given time zone. /// </summary> /// <param name="timezone">A TimeSpan of time zone.</param> /// <param name="ticks">The timestamp, as a <see cref="System.DateTime.Ticks">DateTime.Ticks</see> value.</param> /// <returns> /// true if the given timestamp matches the pattern. /// </returns> public bool Match(TimeSpan timezone, long ticks) { DateTime gc = new DateTime((timezone + TimeSpan.FromTicks(ticks)).Ticks); int minute = gc.Minute; int hour = gc.Hour; int dayOfMonth = gc.Month; int month = gc.Month; int dayOfWeek = (int)gc.DayOfWeek; int year = gc.Year; for (int i = 0; i < matcherSize; i++) { IValueMatcher minuteMatcher = minuteMatchers[i]; IValueMatcher hourMatcher = hourMatchers[i]; IValueMatcher dayOfMonthMatcher = dayOfMonthMatchers[i]; IValueMatcher monthMatcher = monthMatchers[i]; IValueMatcher dayOfWeekMatcher = dayOfWeekMatchers[i]; bool eval = minuteMatcher.Match(minute) && hourMatcher.Match(hour) && ((dayOfMonthMatcher is DayOfMonthValueMatcher) ? ((DayOfMonthValueMatcher)dayOfMonthMatcher).Match(dayOfMonth, month, DateTime.IsLeapYear(year)) : dayOfMonthMatcher.Match(dayOfMonth)) && monthMatcher.Match(month) && dayOfWeekMatcher.Match(dayOfWeek); if (eval) { return(true); } } return(false); }
private static string UnParse(IValueMatcher matcher) { switch (matcher) { case AnyValueMatcher _: return("*"); case EqualityValueMatcher equalityValueMatcher: return(equalityValueMatcher.MatchValue.ToString()); case RegexValueMatcher _: throw new NotImplementedException(); default: throw new ArgumentOutOfRangeException(nameof(matcher)); } }
private bool Matches(IValueMatcher valueMatcher, object value) { switch (valueMatcher) { case AnyValueMatcher _: return(true); case EqualityValueMatcher equalityValueMatcher: if (!(value is string) && equalityValueMatcher.MatchValue is string stringValue) { return(string.Equals(value?.ToString(), stringValue, StringComparison.OrdinalIgnoreCase)); } return(Equals(value, equalityValueMatcher.MatchValue)); case RegexValueMatcher regexValueMatcher: throw new NotImplementedException(); default: throw new ArgumentOutOfRangeException(nameof(valueMatcher)); } }
public FieldMatcher(string fieldName, IValueMatcher fieldValue) { FieldName = fieldName; FieldValue = fieldValue; }
/** * It returns the next matching moment as a millis value. * * @return The next matching moment as a millis value. */ public DateTime NextMatchingDate() { // Go a minute ahead. time = time.AddMinutes(1); // Is it matching? if (schedulingPattern.Match(time)) { return(time); } // Go through the matcher groups. int size = schedulingPattern.matcherSize; DateTime[] times = new DateTime[size]; for (int k = 0; k < size; k++) { // Ok, split the time! DateTime c = time; // Gets the matchers. IValueMatcher minuteMatcher = schedulingPattern.minuteMatchers[k]; IValueMatcher hourMatcher = schedulingPattern.hourMatchers[k]; IValueMatcher dayOfMonthMatcher = schedulingPattern.dayOfMonthMatchers[k]; IValueMatcher dayOfWeekMatcher = schedulingPattern.dayOfWeekMatchers[k]; IValueMatcher monthMatcher = schedulingPattern.monthMatchers[k]; for (; ;) { // day of week for (; ;) { // month for (; ;) { // day of month for (; ;) { // hour for (; ;) { // minutes if (minuteMatcher.Match(c.Minute)) { break; } else { c = c.AddMinutes(1); } } if (hourMatcher.Match(c.Hour)) { break; } else { c = c.AddHours(1); c = new DateTime(c.Year, c.Month, c.Day, c.Hour, 0, 0); } } if (dayOfMonthMatcher is DayOfMonthValueMatcher) { DayOfMonthValueMatcher aux = (DayOfMonthValueMatcher)dayOfMonthMatcher; if (aux.Match(c.Day, c.Month, DateTime.IsLeapYear(c.Year))) { break; } else { c = c.AddDays(1).Date; } } else if (dayOfMonthMatcher.Match(c.Day)) { break; } else { c = c.AddDays(1).Date; } } if (monthMatcher.Match(c.Month)) { break; } else { c = c.AddMonths(1); c = new DateTime(c.Year, c.Month, 1); } } // Day of week. int dayOfWeek = (int)c.DayOfWeek; if (dayOfWeekMatcher.Match(dayOfWeek)) { break; } else { c = c.AddDays(1).Date; } } // Seems it matches! times[k] = c; } // Which one? DateTime min = DateTime.MaxValue; for (int k = 0; k < size; k++) { if (times[k] < min) { min = times[k]; } } // Updates the object current time value. time = min; // Here it is. return(time); }