private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left, ScriptDate right) { switch (expression.Operator) { case ScriptBinaryOperator.Substract: return(new ScriptTimeSpan(left.value - right.value)); case ScriptBinaryOperator.CompareEqual: return(left.value == right.value); case ScriptBinaryOperator.CompareNotEqual: return(left.value != right.value); case ScriptBinaryOperator.CompareLess: return(left.value < right.value); case ScriptBinaryOperator.CompareLessOrEqual: return(left.value <= right.value); case ScriptBinaryOperator.CompareGreater: return(left.value > right.value); case ScriptBinaryOperator.CompareGreaterOrEqual: return(left.value >= right.value); } throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for timespan"); }
private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left, ScriptTimeSpan right) { switch (expression.Operator) { case ScriptBinaryOperator.Add: return(new ScriptDate((DateTime)left + right)); } throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for between <date> and <timespan>"); }
/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <param name="pattern">The date format pattern.</param> /// <param name="date">The date to format.</param> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public static string ToString(string pattern, ScriptDate date) { if (string.IsNullOrEmpty(pattern)) { return(date.ToString()); } // If pattern is %g only, use the default date if (pattern == "%g") { return(ToString("%g %d %b %Y", date)); } var builder = new StringBuilder(); var defaultCulture = CultureInfo.CurrentCulture; for (int i = 0; i < pattern.Length; i++) { var c = pattern[i]; if (c == '%' && (i + 1) < pattern.Length) { i++; Func <DateTime, CultureInfo, string> formatter; var format = pattern[i]; // Switch to invariant culture if (format == 'g') { defaultCulture = CultureInfo.InvariantCulture; continue; } if (Formats.TryGetValue(format, out formatter)) { builder.Append(formatter.Invoke(date, defaultCulture)); } else { builder.Append('%'); builder.Append(format); } } else { builder.Append(c); } } return(builder.ToString()); }
/// <summary> /// Registers all scriban builtins to the specified <see cref="ScriptObject"/>. /// </summary> /// <param name="builtins">The builtins object.</param> /// <exception cref="System.ArgumentNullException">If builtins is null</exception> public static void Register(ScriptObject builtins) { if (builtins == null) { throw new ArgumentNullException(nameof(builtins)); } IncludeFunction.Register(builtins); ObjectFunctions.Register(builtins); ScriptDate.Register(builtins); ScriptTimeSpan.Register(builtins); ArrayFunctions.Register(builtins); StringFunctions.Register(builtins); MathFunctions.Register(builtins); }
/// <summary> /// Adds millis to date. /// </summary> /// <param name="millis">The millis.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMilliseconds(double millis, ScriptDate date) { return date.value.AddMilliseconds(millis); }
/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <param name="pattern">The date format pattern.</param> /// <param name="date">The date to format.</param> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public static string ToString(string pattern, ScriptDate date) { if (string.IsNullOrEmpty(pattern)) { return date.ToString(); } // If pattern is %g only, use the default date if (pattern == "%g") { return ToString("%g %d %b %Y", date); } var builder = new StringBuilder(); var defaultCulture = CultureInfo.CurrentCulture; for (int i = 0; i < pattern.Length; i++) { var c = pattern[i]; if (c == '%' && (i + 1) < pattern.Length) { i++; Func<DateTime, CultureInfo, string> formatter; var format = pattern[i]; // Switch to invariant culture if (format == 'g') { defaultCulture = CultureInfo.InvariantCulture; continue; } if (Formats.TryGetValue(format, out formatter)) { builder.Append(formatter.Invoke(date, defaultCulture)); } else { builder.Append('%'); builder.Append(format); } } else { builder.Append(c); } } return builder.ToString(); }
/// <summary> /// Adds minutes to date. /// </summary> /// <param name="minutes">The minutes.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMinutes(double minutes, ScriptDate date) { return date.value.AddMinutes(minutes); }
/// <summary> /// Adds seconds to date. /// </summary> /// <param name="seconds">The seconds.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddSeconds(double seconds, ScriptDate date) { return date.value.AddSeconds(seconds); }
/// <summary> /// Adds months to date. /// </summary> /// <param name="months">The months.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMonths(int months, ScriptDate date) { return date.value.AddMonths(months); }
/// <summary> /// Adds hours to date. /// </summary> /// <param name="hours">The hours.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddHours(double hours, ScriptDate date) { return date.value.AddHours(hours); }
/// <summary> /// Adds days to date. /// </summary> /// <param name="days">The days.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddDays(double days, ScriptDate date) { return date.value.AddDays(days); }
/// <summary> /// Adds years to date. /// </summary> /// <param name="years">The years.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddYears(int years, ScriptDate date) { return date.value.AddYears(years); }
private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left, ScriptDate right) { switch (expression.Operator) { case ScriptBinaryOperator.Substract: return new ScriptTimeSpan(left.value - right.value); case ScriptBinaryOperator.CompareEqual: return left.value == right.value; case ScriptBinaryOperator.CompareNotEqual: return left.value != right.value; case ScriptBinaryOperator.CompareLess: return left.value < right.value; case ScriptBinaryOperator.CompareLessOrEqual: return left.value <= right.value; case ScriptBinaryOperator.CompareGreater: return left.value > right.value; case ScriptBinaryOperator.CompareGreaterOrEqual: return left.value >= right.value; } throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for timespan"); }
/// <summary> /// Adds years to date. /// </summary> /// <param name="years">The years.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddYears(int years, ScriptDate date) { return(date.value.AddYears(years)); }
/// <summary> /// Adds months to date. /// </summary> /// <param name="months">The months.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMonths(int months, ScriptDate date) { return(date.value.AddMonths(months)); }
/// <summary> /// Adds hours to date. /// </summary> /// <param name="hours">The hours.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddHours(double hours, ScriptDate date) { return(date.value.AddHours(hours)); }
/// <summary> /// Adds millis to date. /// </summary> /// <param name="millis">The millis.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMilliseconds(double millis, ScriptDate date) { return(date.value.AddMilliseconds(millis)); }
/// <summary> /// Adds seconds to date. /// </summary> /// <param name="seconds">The seconds.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddSeconds(double seconds, ScriptDate date) { return(date.value.AddSeconds(seconds)); }
/// <summary> /// Adds minutes to date. /// </summary> /// <param name="minutes">The minutes.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddMinutes(double minutes, ScriptDate date) { return(date.value.AddMinutes(minutes)); }
private object EvaluateBinaryExpression(ScriptBinaryExpression expression, ScriptDate left, ScriptTimeSpan right) { switch (expression.Operator) { case ScriptBinaryOperator.Add: return new ScriptDate((DateTime)left + right); } throw new ScriptRuntimeException(expression.Span, $"Operator [{expression.Operator}] is not supported for between <date> and <timespan>"); }
/// <summary> /// Adds days to date. /// </summary> /// <param name="days">The days.</param> /// <param name="date">The date.</param> /// <returns>A new date</returns> public static ScriptDate AddDays(double days, ScriptDate date) { return(date.value.AddDays(days)); }