Simplified datetime object used for scripting, accessible through the "date" variable (e.g {{date.now}})
コード例 #1
0
        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");
        }
コード例 #2
0
        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>");
        }
コード例 #3
0
        /// <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());
        }
コード例 #4
0
ファイル: BuiltinFunctions.cs プロジェクト: ruo2012/scriban
        /// <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);
        }
コード例 #5
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #6
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
        /// <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();
        }
コード例 #7
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #8
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #9
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #10
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #11
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #12
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
 /// <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);
 }
コード例 #13
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
        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");
        }
コード例 #14
0
 /// <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));
 }
コード例 #15
0
 /// <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));
 }
コード例 #16
0
 /// <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));
 }
コード例 #17
0
 /// <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));
 }
コード例 #18
0
 /// <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));
 }
コード例 #19
0
 /// <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));
 }
コード例 #20
0
ファイル: ScriptDate.cs プロジェクト: lunet-io/scriban
        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>");
        }
コード例 #21
0
 /// <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));
 }