Пример #1
0
        internal static DateTime Transform(this DateTime dateTime, DateTimeTransformation transform, DateTimeSpecificationBounds boundary, Func <DateTime, int, DateTime> relative, Func <DateTime, int, DateTime> exact, Func <DateTime, DateTime> max)
        {
            if (transform.IsEmpty)
            {
                return(dateTime);
            }

            DateTime result = dateTime;

            if (transform.Type == DateTimeTransformationType.Relative)
            {
                result = relative(result, transform.Value);
            }
            else
            {
                if (transform.Type == DateTimeTransformationType.Exact)
                {
                    result = exact(result, transform.Value);
                }
                else if (transform.Type == DateTimeTransformationType.Max)
                {
                    result = max(result);
                }

                if (boundary == DateTimeSpecificationBounds.Upper)
                {
                    result = relative(result, 1).AddTicks(-1);
                }
            }

            return(result);
        }
Пример #2
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                DateTimeTransformation trans = new DateTimeTransformation();

                if (reader.TokenType == JsonToken.Integer)
                {
                    int val = Convert.ToInt32(reader.Value);
                    trans.Type  = val <= 0 ? DateTimeTransformationType.Relative : DateTimeTransformationType.Exact;
                    trans.Value = val;
                }
                else
                {
                    string val = (string)reader.Value;
                    if (val == "*")
                    {
                        trans.Type = DateTimeTransformationType.Max;
                    }
                    else if (val.StartsWith("-") || val.StartsWith("+"))
                    {
                        trans.Type = DateTimeTransformationType.Relative;
                        string num = val.Replace("-", "").Replace("+", "").Replace("=", "");
                        trans.Value = Int32.Parse(num) * (val.StartsWith("-") ? -1 : 1);
                    }
                    else if (val == string.Empty)
                    {
                        trans.Type = DateTimeTransformationType.None;
                    }
                    else
                    {
                        throw new FormatException(String.Format("'{0}' is not a valid value for DateTimeTransformation.", val));
                    }
                }

                return(trans);
            }