Beispiel #1
0
    public override object FromDbValue(Type fieldType, object value)
    {
        var ticks    = (long)this.ConvertNumber(typeof(long), value);
        var timeSpan = TimeSpan.FromTicks(ticks);

        return(TimeOnly.FromTimeSpan(timeSpan));
    }
Beispiel #2
0
        public static void FromToTimeSpanTest()
        {
            Assert.Equal(TimeOnly.MinValue, TimeOnly.FromTimeSpan(TimeSpan.Zero));
            Assert.Equal(TimeSpan.Zero, TimeOnly.MinValue.ToTimeSpan());

            Assert.Equal(new TimeOnly(10, 20, 30), TimeOnly.FromTimeSpan(new TimeSpan(10, 20, 30)));
            Assert.Equal(new TimeSpan(14, 10, 50), new TimeOnly(14, 10, 50).ToTimeSpan());

            Assert.Equal(TimeOnly.MaxValue, TimeOnly.FromTimeSpan(TimeOnly.MaxValue.ToTimeSpan()));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("ticks", () => TimeOnly.FromTimeSpan(new TimeSpan(24, 0, 0)));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("ticks", () => TimeOnly.FromTimeSpan(new TimeSpan(-1, 0, 0)));
        }
    public virtual async Task TimeOnly_FromTimeSpan()
    {
        // We cannot evaluate TimeOnly.FromTimeSpan in .NET since there are some rows were the result is a day or more.
        using var ctx = CreateContext();

        var id = (await ctx.Set <Mission>().Where(m => TimeOnly.FromTimeSpan(m.Duration) == new TimeOnly(1, 2, 3)).SingleAsync()).Id;

        Assert.Equal(1, id);

        AssertSql(
            @"SELECT m.""Id"", m.""CodeName"", m.""Date"", m.""Duration"", m.""Rating"", m.""Time"", m.""Timeline""
FROM ""Missions"" AS m
WHERE m.""Duration""::time without time zone = TIME '01:02:03'
LIMIT 2");
    }
Beispiel #4
0
        private static ParseStringSpanDelegate GetParseStringSpanFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
                var typeCode = typeof(T).GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseObject);

                case TypeCode.Single:
                    return(value => MemoryProvider.Instance.ParseFloat(value));

                case TypeCode.Double:
                    return(value => MemoryProvider.Instance.ParseDouble(value));

                case TypeCode.Decimal:
                    return(value => MemoryProvider.Instance.ParseDecimal(value));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value.Length == 0 ? (char)0 : value.Length == 1 ? value[0] : JsonTypeSerializer.Unescape(value)[0]);
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value.ToString()));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value.ToString()));
                }
#if NET6_0
                if (typeof(T) == typeof(DateOnly))
                {
                    return(value => DateOnly.FromDateTime(DateTimeSerializer.ParseShortestXsdDateTime(value.ToString())));
                }
                if (typeof(T) == typeof(TimeOnly))
                {
                    return(value => TimeOnly.FromTimeSpan(DateTimeSerializer.ParseTimeSpan(value.ToString())));
                }
#endif
            }
            else
            {
                var typeCode = nullableType.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.IsNullOrEmpty()
                            ? (bool?)null
                            : value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseNullableObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseNullableObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseNullableObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseNullableObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseNullableObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseNullableObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseNullableObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseNullableObject);

                case TypeCode.Single:
                    return(value => value.IsNullOrEmpty() ? (float?)null : value.ParseFloat());

                case TypeCode.Double:
                    return(value => value.IsNullOrEmpty() ? (double?)null : value.ParseDouble());

                case TypeCode.Decimal:
                    return(value => value.IsNullOrEmpty() ? (decimal?)null : value.ParseDecimal());

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value.IsEmpty ? (char?)null : value.Length == 1 ? value[0] : JsonTypeSerializer.Unescape(value)[0]);
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value.ToString()));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => value.IsNullOrEmpty() ? (Guid?)null : value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value.ToString()));
                }
#if NET6_0
                if (typeof(T) == typeof(DateOnly?))
                {
                    return(value => value.IsNullOrEmpty() ? default : DateOnly.FromDateTime(DateTimeSerializer.ParseShortestXsdDateTime(value.ToString())));
                }
                if (typeof(T) == typeof(TimeOnly?))
                {
                    return(value => value.IsNullOrEmpty() ? default : TimeOnly.FromTimeSpan(DateTimeSerializer.ParseTimeSpan(value.ToString())));
                }
#endif
            }

            return(null);
        }
Beispiel #5
0
    public void Ejemplo()
    {
        DateOnly date = DateOnly.FromDateTime(DateTime.Now);

        TimeOnly time = TimeOnly.FromTimeSpan(TimeSpan.FromHours(13));
    }
    ///<summary>Uses Zero-timespan and unspecified datetime-kind</summary>
    public static DateTime ToDateTime(this DateOnly thisDate)
    {
        TimeOnly time = TimeOnly.FromTimeSpan(TimeSpan.Zero);

        return(thisDate.ToDateTime(time, DateTimeKind.Unspecified));
    }
 public static TimeOnly?ToTimeOnly(this TimeSpan?time)
 {
     return(time == null ? null : TimeOnly.FromTimeSpan(time.Value));
 }
 public static TimeOnly ToTimeOnly(this TimeSpan time)
 {
     return(TimeOnly.FromTimeSpan(time));
 }
Beispiel #9
0
        public override async Task <T> GetFieldValueAsync <T>(int i, CancellationToken cancellationToken)
        {
            CheckState();
            CheckPosition();
            CheckIndex(i);

            var type = typeof(T);

            type = Nullable.GetUnderlyingType(type) ?? type;
            try
            {
                if (type == typeof(bool))
                {
                    return((T)(object)_row[i].GetBoolean());
                }
                else if (type == typeof(byte))
                {
                    return((T)(object)_row[i].GetByte());
                }
                else if (type == typeof(char))
                {
                    return((T)(object)_row[i].GetChar());
                }
                else if (type == typeof(Guid))
                {
                    return((T)(object)_row[i].GetGuid());
                }
                else if (type == typeof(short))
                {
                    return((T)(object)_row[i].GetInt16());
                }
                else if (type == typeof(int))
                {
                    return((T)(object)_row[i].GetInt32());
                }
                else if (type == typeof(long))
                {
                    return((T)(object)_row[i].GetInt64());
                }
                else if (type == typeof(float))
                {
                    return((T)(object)_row[i].GetFloat());
                }
                else if (type == typeof(double))
                {
                    return((T)(object)_row[i].GetDouble());
                }
                else if (type == typeof(string))
                {
                    return((T)(object)await _row[i].GetStringAsync(cancellationToken).ConfigureAwait(false));
                }
                else if (type == typeof(decimal))
                {
                    return((T)(object)_row[i].GetDecimal());
                }
                else if (type == typeof(DateTime))
                {
                    return((T)(object)_row[i].GetDateTime());
                }
                else if (type == typeof(TimeSpan))
                {
                    return((T)(object)_row[i].GetTimeSpan());
                }
                else if (type == typeof(byte[]))
                {
                    return((T)(object)await _row[i].GetBinaryAsync().ConfigureAwait(false));
                }
                else if (type == typeof(FbDecFloat))
                {
                    return((T)(object)_row[i].GetDecFloat());
                }
                else if (type == typeof(BigInteger))
                {
                    return((T)(object)_row[i].GetInt128());
                }
                else if (type == typeof(FbZonedDateTime))
                {
                    return((T)(object)_row[i].GetZonedDateTime());
                }
                else if (type == typeof(FbZonedTime))
                {
                    return((T)(object)_row[i].GetZonedTime());
                }
#if NET6_0_OR_GREATER
                else if (type == typeof(DateOnly))
                {
                    return((T)(object)DateOnly.FromDateTime(_row[i].GetDateTime()));
                }
#endif
#if NET6_0_OR_GREATER
                else if (type == typeof(TimeOnly))
                {
                    return((T)(object)TimeOnly.FromTimeSpan(_row[i].GetTimeSpan()));
                }
#endif
                else
                {
                    return((T)await _row[i].GetValueAsync().ConfigureAwait(false));
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Sets <paramref name="value"/> on the property defined by <paramref name="memberInfo"/> on <paramref name="entity"/>.
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="value"></param>
        /// <param name="entity"></param>
        public static void SetMemberValue(MemberInfo memberInfo, object value, object entity)
        {
            // if this is a nullable type, we need to get the underlying type (ie. int?, float?, guid?, etc.)
            var type           = GetMemberType(memberInfo);
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }

            if (value == DBNull.Value)
            {
                value = null;
            }
            else
            {
                value = ConvertValueToEnum(value, type);
            }

            // custom support for converting to DateOnly and TimeOnly
            if (type == typeof(DateOnly) && value is DateTime dt1)
            {
                value = DateOnly.FromDateTime(dt1);
            }
            else if (type == typeof(TimeOnly))
            {
                switch (value)
                {
                case DateTime dt2:
                    value = TimeOnly.FromDateTime(dt2);
                    break;

                case TimeSpan ts:
                    value = TimeOnly.FromTimeSpan(ts);
                    break;
                }
            }

            try
            {
                switch (memberInfo.MemberType)
                {
                case MemberTypes.Field:
                    ((FieldInfo)memberInfo).SetValue(entity, value);
                    break;

                case MemberTypes.Property:
                    ((PropertyInfo)memberInfo).SetValue(entity, value, null);
                    break;

                default:
                    throw new ArgumentException($"Only {MemberTypes.Field} and {MemberTypes.Property} are supported.", nameof(memberInfo));
                }
            }
            catch (Exception innerException)
            {
                string valueType = value == null ? "unknown (=NULL)" : value.GetType().ToString();
                string message   = $"Error while setting the {memberInfo.Name} member with an object of type {value}";

                throw new Exception(message, innerException);
            }
        }