Esempio n. 1
0
            public static string TryParse(string str, out SmartDateTimeSpan result)
            {
                if (string.IsNullOrEmpty(str))
                {
                    result = null;
                    return(FilterValueConverter.Continue);
                }

                Match match = regex.Match(str);

                if (!match.Success)
                {
                    result = null;
                    return("Invalid Format: yyyy/mm/dd hh:mm:ss");
                }

                result = new SmartDateTimeSpan();

                return
                    (Assert(match, "year", "yyyy", 0, int.MaxValue, out result.Year) ??
                     Assert(match, "month", "mm", 1, 12, out result.Month) ??
                     Assert(match, "day", "dd", 1, 31, out result.Day) ??
                     Assert(match, "hour", "hh", 0, 23, out result.Hour) ??
                     Assert(match, "minute", "mm", 0, 59, out result.Minute) ??
                     Assert(match, "second", "ss", 0, 59, out result.Second));
            }
        public static Result <SmartDateTimeSpan>?TryParse(string?str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(null);
            }

            Match match = regex.Match(str);

            if (!match.Success)
            {
                return(new Result <SmartDateTimeSpan> .Error("Invalid Format: yyyy/mm/dd hh:mm:ss"));
            }

            var span = new SmartDateTimeSpan();

            string?error =
                Assert(match, "year", "yyyy", 0, int.MaxValue, out span.Year) ??
                Assert(match, "month", "mm", 1, 12, out span.Month) ??
                Assert(match, "day", "dd", 1, 31, out span.Day) ??
                Assert(match, "hour", "hh", 0, 23, out span.Hour) ??
                Assert(match, "minute", "mm", 0, 59, out span.Minute) ??
                Assert(match, "second", "ss", 0, 59, out span.Second);

            if (error.HasText())
            {
                return(new Result <SmartDateTimeSpan> .Error(error));
            }

            return(new Result <SmartDateTimeSpan> .Success(span));
        }
Esempio n. 3
0
        public Result <string?>?TryToStringValue(object?value, Type type)
        {
            if (value == null)
            {
                return(null);
            }

            DateTime dateTime = (DateTime)value;

            SmartDateTimeSpan ss = SmartDateTimeSpan.Substract(dateTime, TimeZoneManager.Now);

            return(new Result <string?> .Success(ss.ToString()));
        }
Esempio n. 4
0
        public string TryToStringValue(object value, Type type, out string result)
        {
            if (value == null)
            {
                result = null;
                return(FilterValueConverter.Continue);
            }

            DateTime dateTime = (DateTime)value;

            SmartDateTimeSpan ss = SmartDateTimeSpan.Substract(dateTime, TimeZoneManager.Now);

            result = ss.ToString();
            return(null);
        }
    public Result <string?>?TryToStringValue(object?value, Type type)
    {
        if (value == null)
        {
            return(null);
        }

        DateTime dateTime =
            value is string s?DateTime.ParseExact(s, type == typeof(DateTime)? "o" : "yyyy-MM-dd", CultureInfo.InvariantCulture) :
                value is DateOnly d?d.ToDateTime() :
                    value is DateTime dt ? dt : throw new UnexpectedValueException(value);

        SmartDateTimeSpan ss = SmartDateTimeSpan.Substract(dateTime, Clock.Now);

        return(new Result <string?> .Success(ss.ToString()));
    }
Esempio n. 6
0
        public Result <object?>?TryParseValue(string?value, Type type)
        {
            var res = SmartDateTimeSpan.TryParse(value);

            if (res == null)
            {
                return(null);
            }

            if (res is Result <SmartDateTimeSpan> .Error e)
            {
                return(new Result <object?> .Error(e.ErrorText));
            }

            return(new Result <object?> .Success(((Result <SmartDateTimeSpan> .Success)res).Value.ToDateTime()));
        }
Esempio n. 7
0
        public string TryParseValue(string value, Type type, out object result)
        {
            string error = SmartDateTimeSpan.TryParse(value, out SmartDateTimeSpan ss);

            if (error != null)
            {
                if (DateTime.TryParse(value, out DateTime dtResult))
                {
                    result = dtResult;
                    return(null); //do not block
                }

                result = null;
                return(error);
            }

            result = ss.ToDateTime();
            return(null);
        }
    public Result <object?>?TryParseValue(string?value, Type type)
    {
        var res = SmartDateTimeSpan.TryParse(value);

        if (res == null)
        {
            return(null);
        }

        if (res is Result <SmartDateTimeSpan> .Error e)
        {
            return(new Result <object?> .Error(e.ErrorText));
        }

        if (res is Result <SmartDateTimeSpan> .Success s)
        {
            return(new Result <object?> .Success(type.UnNullify() == typeof(DateOnly)?(object)s.Value.ToDateTime().ToDateOnly() : (object)s.Value.ToDateTime()));
        }

        throw new UnexpectedValueException(res);
    }
        public static SmartDateTimeSpan Substract(DateTime date, DateTime now)
        {
            var ss = new SmartDateTimeSpan
            {
                Year  = Diference(now.Year - date.Year, "yyyy") ?? date.Year.ToString("0000"),
                Month = Diference(now.Month - date.Month, "mm") ?? date.Month.ToString("00"),
                Day   = date.Day == DateTime.DaysInMonth(date.Year, date.Month) ? "max" : (Diference(now.Day - date.Day, "dd") ?? date.Day.ToString("00")),
            };

            if (date == date.Date)
            {
                ss.Hour = ss.Minute = ss.Second = "00";
            }
            else
            {
                ss.Hour   = Diference(now.Hour - date.Hour, "hh") ?? date.Hour.ToString("00");
                ss.Minute = Diference(now.Minute - date.Minute, "mm") ?? date.Minute.ToString("00");
                ss.Second = Diference(now.Second - date.Second, "ss") ?? date.Second.ToString("00");
            }

            return(ss);
        }
            public static SmartDateTimeSpan Substract(DateTime date, DateTime now)
            {
                var ss = new SmartDateTimeSpan
                {
                    Year = Diference(now.Year - date.Year, "yyyy") ?? date.Year.ToString("0000"),
                    Month = Diference(now.Month - date.Month, "mm") ?? date.Month.ToString("00"),
                    Day = date.Day == DateTime.DaysInMonth(date.Year, date.Month) ? "max" : (Diference(now.Day - date.Day, "dd") ?? date.Day.ToString("00")),
                };

                if (date == date.Date)
                {
                    ss.Hour = ss.Minute = ss.Second = "00";
                }
                else
                {
                    ss.Hour = Diference(now.Hour - date.Hour, "hh") ?? date.Hour.ToString("00");
                    ss.Minute = Diference(now.Minute - date.Minute, "mm") ?? date.Minute.ToString("00");
                    ss.Second = Diference(now.Second - date.Second, "ss") ?? date.Second.ToString("00");
                }

                return ss;
            }
            public static string TryParse(string str, out SmartDateTimeSpan result)
            {
                if (string.IsNullOrEmpty(str))
                {
                    result = null;
                    return FilterValueConverter.Continue;
                }

                Match match = regex.Match(str);
                if (!match.Success)
                {
                    result = null;
                    return "Invalid Format: yyyy/mm/dd hh:mm:ss";
                }

                result = new SmartDateTimeSpan();

                return
                    Assert(match, "year", "yyyy", 0, int.MaxValue, out result.Year) ??
                    Assert(match, "month", "mm", 1, 12, out result.Month) ??
                    Assert(match, "day", "dd", 1, 31,  out result.Day) ??
                    Assert(match, "hour", "hh", 0, 23, out result.Hour) ??
                    Assert(match, "minute", "mm", 0, 59, out result.Minute) ??
                    Assert(match, "second", "ss", 0, 59, out result.Second);
            }
Esempio n. 12
0
        static Mapping()
        {
            MappingRepository <bool> .Mapping     = GetValue(ctx => ParseHtmlBool(ctx.Input));
            MappingRepository <byte> .Mapping     = GetValue(ctx => byte.Parse(ctx.Input));
            MappingRepository <sbyte> .Mapping    = GetValue(ctx => sbyte.Parse(ctx.Input));
            MappingRepository <short> .Mapping    = GetValue(ctx => short.Parse(ctx.Input));
            MappingRepository <ushort> .Mapping   = GetValue(ctx => ushort.Parse(ctx.Input));
            MappingRepository <int> .Mapping      = GetValue(ctx => int.Parse(ctx.Input));
            MappingRepository <uint> .Mapping     = GetValue(ctx => uint.Parse(ctx.Input));
            MappingRepository <long> .Mapping     = GetValue(ctx => long.Parse(ctx.Input));
            MappingRepository <ulong> .Mapping    = GetValue(ctx => ulong.Parse(ctx.Input));
            MappingRepository <float> .Mapping    = GetValue(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (float)ReflectionTools.ParsePercentage(ctx.Input, typeof(float), CultureInfo.CurrentCulture) : float.Parse(ctx.Input));
            MappingRepository <double> .Mapping   = GetValue(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (double)ReflectionTools.ParsePercentage(ctx.Input, typeof(double), CultureInfo.CurrentCulture) : double.Parse(ctx.Input));
            MappingRepository <decimal> .Mapping  = GetValue(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (decimal)ReflectionTools.ParsePercentage(ctx.Input, typeof(decimal), CultureInfo.CurrentCulture) : decimal.Parse(ctx.Input));
            MappingRepository <DateTime> .Mapping = GetValue(ctx => DateTime.Parse(ctx.HasInput ? ctx.Input : ctx.Inputs["Date"] + " " + ctx.Inputs["Time"]).FromUserInterface());
            MappingRepository <Guid> .Mapping     = GetValue(ctx => Guid.Parse(ctx.Input));
            MappingRepository <TimeSpan> .Mapping = GetValue(ctx =>
            {
                var dateFormatAttr = ctx.PropertyRoute.PropertyInfo.GetCustomAttribute <TimeSpanDateFormatAttribute>();
                if (dateFormatAttr != null)
                {
                    return(DateTime.ParseExact(ctx.Input, dateFormatAttr.Format, CultureInfo.CurrentCulture).TimeOfDay);
                }
                else
                {
                    return(TimeSpan.Parse(ctx.Input));
                }
            });
            MappingRepository <SqlHierarchyId> .Mapping = GetValue(ctx => SqlHierarchyId.Parse(ctx.Input));
            MappingRepository <ColorEmbedded> .Mapping  = GetValue(ctx => ctx.Input.HasText() ? ColorEmbedded.FromRGBHex(ctx.Input) : null);

            MappingRepository <bool?> .Mapping     = GetValueNullable(ctx => ParseHtmlBool(ctx.Input));
            MappingRepository <byte?> .Mapping     = GetValueNullable(ctx => byte.Parse(ctx.Input));
            MappingRepository <sbyte?> .Mapping    = GetValueNullable(ctx => sbyte.Parse(ctx.Input));
            MappingRepository <short?> .Mapping    = GetValueNullable(ctx => short.Parse(ctx.Input));
            MappingRepository <ushort?> .Mapping   = GetValueNullable(ctx => ushort.Parse(ctx.Input));
            MappingRepository <int?> .Mapping      = GetValueNullable(ctx => int.Parse(ctx.Input));
            MappingRepository <uint?> .Mapping     = GetValueNullable(ctx => uint.Parse(ctx.Input));
            MappingRepository <long?> .Mapping     = GetValueNullable(ctx => long.Parse(ctx.Input));
            MappingRepository <ulong?> .Mapping    = GetValueNullable(ctx => ulong.Parse(ctx.Input));
            MappingRepository <float?> .Mapping    = GetValueNullable(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (float)ReflectionTools.ParsePercentage(ctx.Input, typeof(float), CultureInfo.CurrentCulture) : float.Parse(ctx.Input));
            MappingRepository <double?> .Mapping   = GetValueNullable(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (double)ReflectionTools.ParsePercentage(ctx.Input, typeof(double), CultureInfo.CurrentCulture) : double.Parse(ctx.Input));
            MappingRepository <decimal?> .Mapping  = GetValueNullable(ctx => ctx.PropertyRoute != null && ReflectionTools.IsPercentage(Reflector.FormatString(ctx.PropertyRoute), CultureInfo.CurrentCulture) ? (decimal)ReflectionTools.ParsePercentage(ctx.Input, typeof(decimal), CultureInfo.CurrentCulture) : decimal.Parse(ctx.Input));
            MappingRepository <DateTime?> .Mapping = GetValue(ctx =>
            {
                var input = ctx.HasInput ? ctx.Input : " ".CombineIfNotEmpty(ctx.Inputs["Date"], ctx.Inputs["Time"]);



                if (input.HasText())
                {
                    DateTime dt;
                    if (DateTime.TryParse(input, out dt))
                    {
                        return(dt.FromUserInterface());
                    }
                    else
                    {
                        SmartDateTimeSpan sts;
                        string error = SmartDateTimeSpan.TryParse(input, out sts);
                        if (!error.HasText())
                        {
                            dt = sts.ToDateTime();
                            return(dt.FromUserInterface());
                        }
                    }
                }

                return((DateTime?)null);

                //return input.HasText() ? DateTime.Parse(input).FromUserInterface() : (DateTime?)null;
            });
            MappingRepository <Guid?> .Mapping     = GetValueNullable(ctx => Guid.Parse(ctx.Input));
            MappingRepository <TimeSpan?> .Mapping = GetValue(ctx =>
            {
                if (ctx.Input.IsNullOrEmpty())
                {
                    return((TimeSpan?)null);
                }

                var dateFormatAttr = ctx.PropertyRoute.PropertyInfo.GetCustomAttribute <TimeSpanDateFormatAttribute>();
                if (dateFormatAttr != null)
                {
                    return(DateTime.ParseExact(ctx.Input, dateFormatAttr.Format, CultureInfo.CurrentCulture).TimeOfDay);
                }
                else
                {
                    return(TimeSpan.Parse(ctx.Input));
                }
            });

            MappingRepository <string> .Mapping = StringTrim;
        }