public void Test_DateTimeOffset_Dates(string format, string expected)
        {
            // only the date part will be compared
            var args = new object[]
            { SystemTime.OffsetNow().AddDays(-1), SystemTime.OffsetNow(), SystemTime.OffsetNow().AddDays(1) };
            var smart = Smart.CreateDefaultSmartFormat();

            smart.Test(format, args, expected);
        }
Exemple #2
0
 private object[] GetArgs()
 {
     return(new object[] {
         0, 1, 2, 3,
         -1, -2,                                                                                       // {4},{5}
         TestFactory.GetPerson(),                                                                      // {6}
         false, true,                                                                                  // {7},{8}
         // Note: only the date part will be compared:
         new DateTime(1111, 1, 1, 1, 1, 1), SystemTime.Now(), new DateTime(5555, 5, 5, 5, 5, 5),       // {9},{10},{11}
         new TimeSpan(-1, -1, -1, -1, -1), TimeSpan.Zero, new TimeSpan(5, 5, 5, 5, 5),                 // {12},{13},{14}
         "Hello", "",                                                                                  // {15},{16}
         new { NotNull = true }, null !,                                                               // {17},{18}
         // Note: only the date part will be compared:
         SystemTime.OffsetNow().AddDays(-1), SystemTime.OffsetNow(), SystemTime.OffsetNow().AddDays(1) // {19},{20},{21}
     });
 public BookMatch(
     BookMatchId id,
     Guid matchId,
     Guid userId,
     decimal stake,
     string currency,
     BookType bookType)
 {
     Id         = id;
     MatchId    = matchId;
     UserId     = userId;
     Stake      = stake;
     Currency   = currency;
     BookType   = bookType;
     IsClose    = false;
     CreateDate = SystemTime.OffsetNow();
 }
        public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
        {
            var format  = formattingInfo.Format;
            var current = formattingInfo.CurrentValue;

            if (format == null)
            {
                return(false);
            }
            // Ignore a leading ":", which is used to bypass the PluralLocalizationExtension
            if (format.baseString[format.startIndex] == ':')
            {
                format = format.Substring(1);
            }

            // See if the format string contains un-nested "|":
            var parameters = format.Split('|');

            if (parameters.Count == 1)
            {
                return(false);                       // There are no parameters found.
            }
            // See if the value is a number:
            var currentIsNumber =
                current is byte || current is short || current is int || current is long ||
                current is float || current is double || current is decimal;

            // An Enum is a number too:
#if !NET45
            if (currentIsNumber == false && current != null && current.GetType().GetTypeInfo().IsEnum)
#else
            if (currentIsNumber == false && current != null && current.GetType().IsEnum)
#endif
            { currentIsNumber = true; }

            var currentNumber = currentIsNumber ? Convert.ToDecimal(current) : 0;

            int paramIndex; // Determines which parameter to use for output

            // First, we'll see if we are using "complex conditions":
            if (currentIsNumber)
            {
                paramIndex = -1;
                while (true)
                {
                    paramIndex++;
                    if (paramIndex == parameters.Count)
                    {
                        return(true);
                    }

                    if (!TryEvaluateCondition(parameters[paramIndex], currentNumber, out var conditionWasTrue,
                                              out var outputItem))
                    {
                        // This parameter doesn't have a
                        // complex condition (making it a "else" condition)

                        // Only do "complex conditions" if the first item IS a "complex condition".
                        if (paramIndex == 0)
                        {
                            break;
                        }
                        // Otherwise, output the "else" section:
                        conditionWasTrue = true;
                    }

                    // If the conditional statement was true, then we can break.
                    if (conditionWasTrue)
                    {
                        formattingInfo.Write(outputItem, current);
                        return(true);
                    }
                }

                // We don't have any "complex conditions",
                // so let's do the normal conditional formatting:
            }

            var paramCount = parameters.Count;

            // Determine the Current item's Type:
            if (currentIsNumber)
            {
                if (currentNumber < 0)
                {
                    paramIndex = paramCount - 1;
                }
                else
                {
                    paramIndex = Math.Min((int)Math.Floor(currentNumber), paramCount - 1);
                }
            }
            else
            {
                switch (current)
                {
                case bool boolArg:
                    // Bool: True|False
                    paramIndex = boolArg ? 0 : 1;
                    break;

                // Date: Past|Present|Future   or   Past/Present|Future
                case DateTime dateTimeArg when paramCount == 3 && dateTimeArg.ToUniversalTime().Date == SystemTime.Now().ToUniversalTime().Date:
                    paramIndex = 1;
                    break;

                case DateTime dateTimeArg when dateTimeArg.ToUniversalTime() <= SystemTime.Now().ToUniversalTime():
                    paramIndex = 0;

                    break;

                case DateTime dateTimeArg:
                    paramIndex = paramCount - 1;
                    break;

                // Date: Past|Present|Future   or   Past/Present|Future
                case DateTimeOffset dateTimeOffsetArg when paramCount == 3 && dateTimeOffsetArg.UtcDateTime.Date == SystemTime.OffsetNow().UtcDateTime.Date:
                    paramIndex = 1;
                    break;

                case DateTimeOffset dateTimeOffsetArg when dateTimeOffsetArg.UtcDateTime <= SystemTime.OffsetNow().UtcDateTime:
                    paramIndex = 0;
                    break;

                case DateTimeOffset dateTimeOffsetArg:
                    paramIndex = paramCount - 1;
                    break;

                // TimeSpan: Negative|Zero|Positive  or  Negative/Zero|Positive
                case TimeSpan timeSpanArg when paramCount == 3 && timeSpanArg == TimeSpan.Zero:
                    paramIndex = 1;
                    break;

                case TimeSpan timeSpanArg when timeSpanArg.CompareTo(TimeSpan.Zero) <= 0:
                    paramIndex = 0;

                    break;

                case TimeSpan timeSpanArg:
                    paramIndex = paramCount - 1;
                    break;

                case string stringArg:
                    // String: Value|NullOrEmpty
                    paramIndex = !string.IsNullOrEmpty(stringArg) ? 0 : 1;
                    break;

                default:
                {
                    // Object: Something|Nothing
                    var arg = current;
                    paramIndex = arg != null ? 0 : 1;
                    break;
                }
                }
            }

            // Now, output the selected parameter:
            var selectedParameter = parameters[paramIndex];

            // Output the selectedParameter:
            formattingInfo.Write(selectedParameter, current);
            return(true);
        }
Exemple #5
0
        public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
        {
            var format  = formattingInfo.Format;
            var current = formattingInfo.CurrentValue;

            if (format != null && format.HasNested)
            {
                return(false);
            }
            string options;

            if (formattingInfo.FormatterOptions != "")
            {
                options = formattingInfo.FormatterOptions;
            }
            else if (format != null)
            {
                options = format.GetLiteralText();
            }
            else
            {
                options = "";
            }

            TimeSpan fromTime;

            switch (current)
            {
            case TimeSpan timeSpan:
                fromTime = timeSpan;
                break;

            case DateTime dateTime:
                if (formattingInfo.FormatterOptions != "")
                {
                    fromTime = SystemTime.Now().ToUniversalTime().Subtract(dateTime.ToUniversalTime());
                }
                else
                {
                    return(false);
                }
                break;

            case DateTimeOffset dateTimeOffset:
                if (formattingInfo.FormatterOptions != "")
                {
                    fromTime = SystemTime.OffsetNow().UtcDateTime.Subtract(dateTimeOffset.UtcDateTime);
                }
                else
                {
                    return(false);
                }
                break;

            default:
                return(false);
            }

            var timeTextInfo = GetTimeTextInfo(formattingInfo.FormatDetails.Provider);

            if (timeTextInfo == null)
            {
                return(false);
            }
            var formattingOptions = TimeSpanFormatOptionsConverter.Parse(options);
            var timeString        = fromTime.ToTimeString(formattingOptions, timeTextInfo);

            formattingInfo.Write(timeString);
            return(true);
        }