예제 #1
0
        public void Overlaps(
            [IncludeDataSources(true, TestProvName.AllOracle /* TestProvName.AllPostgreSQL, ProviderName.DB2 */)] string context)
        {
            // Postgre and DB2 have support but needs to know the type of parameters explicitely,
            // so this test wouldn't work without adding casts at every constant.

            using var db   = GetDataContext(context);
            using var ints = SetupIntsTable(db);

            // OVERLAPS is neither client-evaluated, nor converted in providers without native support.
            // So tests are short because we don't want to test all edge cases of provider implementation.
            // We simply want to check if valid SQL is generated for all basic support types

            ints.Count(i => Row(DT.Parse("2020-10-01"), DT.Parse("2020-10-05"))
                       .Overlaps(Row(DT.Parse("2020-10-03"), DT.Parse("2020-11-09"))))
            .Should().Be(1);

            ints.Count(i => Row(DTO.Parse("2020-10-05"), DTO.Parse("2020-10-01"))
                       .Overlaps(Row(DTO.Parse("2020-10-03"), DTO.Parse("2020-11-09"))))
            .Should().Be(1);

            ints.Count(i => Row(DT.Parse("2020-10-03"), TimeSpan.Parse("6"))
                       .Overlaps(Row(DT.Parse("2020-10-05"), TimeSpan.Parse("1"))))
            .Should().Be(1);

            ints.Count(i => Row(DT.Parse("2020-10-03"), TimeSpan.Parse("6"))
                       .Overlaps(Row(DT.Parse("2020-10-05"), (TimeSpan?)null)))
            .Should().Be(1);
        }
예제 #2
0
 public static DateTimeOffset ReadDateOffset()
 => DateTimeOffset.Parse(ReadLine());
예제 #3
0
        /// <summary>
        /// Converts object to type.
        /// Supports conversion to Enum, DateTime,TimeSpan and CultureInfo
        /// </summary>
        /// <exception cref="InvalidCastException"></exception>
        /// <param name="data">Object to be converted</param>
        /// <param name="type">Type to convert to</param>
        /// <returns></returns>
        public static object ConvertTo(this object data, Type type)
        {
            var tp = type.GetTypeInfo();

            if (data == null)
            {
                if (tp.IsValueType && !type.IsNullable())
                {
                    throw new InvalidCastException("Cant convert null to a value type");
                }
                return(null);
            }

            var otp = data.GetType();

            if (otp.Equals(tp))
            {
                return(data);
            }
            if (tp.IsEnum)
            {
                if (data is string)
                {
                    return(Enum.Parse(type, data.ToString()));
                }
                var o = Enum.ToObject(type, data);
                return(o);
            }

            if (tp.IsValueType)
            {
                if (type == typeof(TimeSpan))
                {
                    return(TimeSpan.Parse(data.ToString()));
                }

                if (type == typeof(DateTime))
                {
                    if (data is DateTimeOffset)
                    {
                        return(((DateTimeOffset)data).DateTime);
                    }
                    return(DateTime.Parse(data.ToString()));
                }

                if (type == typeof(DateTimeOffset))
                {
                    if (data is DateTime)
                    {
                        var dt = (DateTime)data;
                        return(new DateTimeOffset(dt));
                    }
                    return(DateTimeOffset.Parse(data.ToString()));
                }

                if (type.IsNullable())
                {
                    var under = Nullable.GetUnderlyingType(type);
                    return(data.ConvertTo(under));
                }
            }

            return(System.Convert.ChangeType(data, type));
        }