/// <summary> /// To /// </summary> /// <param name="text"></param> /// <param name="formatProvider"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static DateTimeSpan To( string text, IFormatProvider formatProvider = null, DateTimeSpan defaultVal = default) { if (formatProvider is null) { formatProvider = DateTimeFormatInfo.CurrentInfo; } return(DateTimeSpan.TryParse(text, formatProvider, out var dateTimeSpan) ? dateTimeSpan : defaultVal); }
/// <summary> /// Computes a natural language date/time addition or subtraction expression such as Jan 1, 2000 + 5 days, returns the result of this expression. Returns null if input is not valid. /// </summary> /// <param name="expression">A natural language date/time addition or subtraction expression. Example: "Jan 1, 2000 + 5 days"</param> /// <returns></returns> public static string Calculate(string expression) { string result = null; const char Plus = '+'; const char Minus = '-'; string[] parts = expression.Split(new char[] { Plus, Minus }); if (parts.Length != 2) { return(null); } char[] operandInput = expression.Replace(parts[0], string.Empty).Replace(parts[1], string.Empty).Trim().ToCharArray(); if (operandInput.Length != 1) { return(null); } char operand = operandInput[0]; if (operand != Plus && operand != Minus) { return(null); } if (!TryParseBetter(parts[0], out DateTime dateTime1)) { return(null); } if (TryParseBetter(parts[1], out DateTime dateTime2)) { switch (operand) { case Plus: result = PrettifyDateTime(Calculator.Add(dateTime1, dateTime2)); break; case Minus: result = Calculator.Subtract(dateTime1, dateTime2).ToString(); break; } } else if (DateTimeSpan.TryParse(parts[1], out DateTimeSpan dateTimeSpan)) { switch (operand) { case Plus: result = PrettifyDateTime(Calculator.Add(dateTime1, dateTimeSpan)); break; case Minus: result = PrettifyDateTime(Calculator.Subtract(dateTime1, dateTimeSpan)); break; } } return(result); }
/// <summary> /// Is /// </summary> /// <param name="text"></param> /// <param name="formatProvider"></param> /// <param name="matchedCallback"></param> /// <returns></returns> public static bool Is( string text, IFormatProvider formatProvider = null, Action <DateTimeSpan> matchedCallback = null) { if (string.IsNullOrWhiteSpace(text)) { return(false); } if (formatProvider is null) { formatProvider = DateTimeFormatInfo.CurrentInfo; } return(DateTimeSpan.TryParse(text, formatProvider, out var dateTimeSpan) .IfTrueThenInvoke(matchedCallback, dateTimeSpan)); }
public static string ExplainSyntaxError(string expression) { string[] parts = expression.Split(new char[] { '+', '-' }); if (parts.Length < 2) { return(Properties.Resources.SyntaxErrorNoOperand); } if (parts.Length > 2) { return(Properties.Resources.SyntaxErrorTooManyOperands); } if (!DateTime.TryParse(parts[0], out _)) { return(string.Format(Properties.Resources.SyntaxErrorPart1NotValid, parts[0])); } if (!DateTime.TryParse(parts[1], out _) && !DateTimeSpan.TryParse(parts[1], out _)) { return(string.Format(Properties.Resources.SyntaxErrorPart2NotValid, parts[1])); } return(Properties.Resources.Usage); }