[InlineData("23:59:59.9999999")] // TimeOnly.MaxValue public static void TimeOnly_Write_Success(string value, string?expectedValue = null) { TimeOnly ts = TimeOnly.Parse(value); string json = JsonSerializer.Serialize(ts); Assert.Equal($"\"{expectedValue ?? value}\"", json); }
public override void ClassWithDateOnlyAndTimeOnlyValues_Roundtrip() { RunTest(new ClassWithDateOnlyAndTimeOnlyValues { DateOnly = DateOnly.Parse("2022-05-10"), NullableDateOnly = DateOnly.Parse("2022-05-10"), TimeOnly = TimeOnly.Parse("21:51:51"), NullableTimeOnly = TimeOnly.Parse("21:51:51"), }); RunTest(new ClassWithDateOnlyAndTimeOnlyValues()); void RunTest(ClassWithDateOnlyAndTimeOnlyValues expected) { string json = JsonSerializer.Serialize(expected, DefaultContext.ClassWithDateOnlyAndTimeOnlyValues); ClassWithDateOnlyAndTimeOnlyValues actual = JsonSerializer.Deserialize(json, ((ITestContext)MetadataWithPerTypeAttributeContext.Default).ClassWithDateOnlyAndTimeOnlyValues); Assert.Equal(expected.DateOnly, actual.DateOnly); Assert.Equal(expected.NullableDateOnly, actual.NullableDateOnly); Assert.Equal(expected.TimeOnly, actual.TimeOnly); Assert.Equal(expected.NullableTimeOnly, actual.NullableTimeOnly); } }
public static void OAndRFormatsTest() { TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); string formattedDate = timeOnly.ToString("o"); Assert.Equal(16, formattedDate.Length); Assert.Equal(':', formattedDate[2]); Assert.Equal(':', formattedDate[5]); Assert.Equal('.', formattedDate[8]); TimeOnly parsedTimeOnly = TimeOnly.Parse(formattedDate); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.Parse(formattedDate.AsSpan()); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.ParseExact(formattedDate, "O"); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.ParseExact(formattedDate.AsSpan(), "O".AsSpan()); Assert.Equal(timeOnly, parsedTimeOnly); timeOnly = new TimeOnly(timeOnly.Hour, timeOnly.Minute, timeOnly.Second); formattedDate = timeOnly.ToString("r"); Assert.Equal(8, formattedDate.Length); Assert.Equal(':', formattedDate[2]); Assert.Equal(':', formattedDate[5]); parsedTimeOnly = TimeOnly.Parse(formattedDate); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.Parse(formattedDate.AsSpan()); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.ParseExact(formattedDate, "R"); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.ParseExact(formattedDate.AsSpan(), "R".AsSpan()); Assert.Equal(timeOnly, parsedTimeOnly); }
public void Values_can_be_correctly_converted_to_nullable_timeonly_without_the_parser_specifying_a_custom_converter() { var option = new Option <TimeOnly?>("-x"); var value = option.Parse("-x 12:34:56").GetValueForOption(option); value.Should().Be(TimeOnly.Parse("12:34:56")); }
public void can_get_a_timeonly_that_happened_recently() { var now = TimeOnly.Parse("2:00 PM", CultureInfo.InvariantCulture); var maxBehind = now.AddMinutes(-5); var timeRecent = date.RecentTimeOnly(5, now); timeRecent.IsBetween(maxBehind, now).Should().BeTrue(); }
public void can_get_a_timeonly_that_will_happen_soon() { var now = TimeOnly.Parse("1:00 PM", CultureInfo.InvariantCulture); var maxTime = now.AddMinutes(5); var timeSoon = date.SoonTimeOnly(5, now); timeSoon.IsBetween(now, maxTime).Should().BeTrue(); }
public void PrecisionStrategy(string inputTime, string timeToCompareAgainst, string expectedResult, double precision) { Configurator.TimeOnlyHumanizeStrategy = new PrecisionTimeOnlyHumanizeStrategy(precision); var parsedInputTime = TimeOnly.Parse(inputTime); var parsedBaseTime = TimeOnly.Parse(timeToCompareAgainst); var actualResult = parsedInputTime.Humanize(parsedBaseTime); Assert.Equal(expectedResult, actualResult); }
public void can_get_random_timeonly_between_two_times_basic() { var start = TimeOnly.Parse("1:00 PM", CultureInfo.InvariantCulture); var end = TimeOnly.Parse("2:00 PM", CultureInfo.InvariantCulture); var someTimeBetween = date.BetweenTimeOnly(start, end); someTimeBetween.IsBetween(start, end).Should().BeTrue(); var outside = TimeOnly.Parse("2:30 PM", CultureInfo.InvariantCulture); outside.IsBetween(start, end).Should().BeFalse(); }
public void can_get_random_timeonly_between_two_times_wrap_around() { //wrap around from 2:00 PM to 1:00 PM; times from 1:00 PM -> 2:00 PM is excluded. var start = TimeOnly.Parse("2:00 PM", CultureInfo.InvariantCulture); var end = TimeOnly.Parse("1:00 PM", CultureInfo.InvariantCulture); var someTimeBetween = date.BetweenTimeOnly(end, start); someTimeBetween.IsBetween(end, start).Should().BeTrue(); var outside = TimeOnly.Parse("1:30 PM", CultureInfo.InvariantCulture); outside.IsBetween(start, end).Should().BeFalse(); }
/// <summary> /// Converts the string to an object. /// </summary> /// <param name="text">The string to convert to an object.</param> /// <param name="row">The <see cref="IReaderRow"/> for the current record.</param> /// <param name="memberMapData">The <see cref="MemberMapData"/> for the member being created.</param> /// <returns>The object created from the string.</returns> public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData) { if (text == null) { return(base.ConvertFromString(null, row, memberMapData)); } var formatProvider = (IFormatProvider)memberMapData.TypeConverterOptions.CultureInfo.GetFormat(typeof(DateTimeFormatInfo)) ?? memberMapData.TypeConverterOptions.CultureInfo; var dateTimeStyle = memberMapData.TypeConverterOptions.DateTimeStyle ?? DateTimeStyles.None; return(memberMapData.TypeConverterOptions.Formats == null || memberMapData.TypeConverterOptions.Formats.Length == 0 ? TimeOnly.Parse(text, formatProvider, dateTimeStyle) : TimeOnly.ParseExact(text, memberMapData.TypeConverterOptions.Formats, formatProvider, dateTimeStyle)); }
public static void InvalidFormatsTest() { DateTime dt = DateTime.Now; string formatted = dt.ToString(); Assert.Throws <FormatException>(() => TimeOnly.Parse(formatted)); Assert.Throws <FormatException>(() => TimeOnly.Parse(formatted.AsSpan())); Assert.False(TimeOnly.TryParse(formatted, out TimeOnly timeOnly)); Assert.False(TimeOnly.TryParse(formatted.AsSpan(), out timeOnly)); formatted = dt.ToString("d"); Assert.Throws <FormatException>(() => TimeOnly.Parse(formatted)); Assert.Throws <FormatException>(() => TimeOnly.Parse(formatted.AsSpan())); Assert.False(TimeOnly.TryParse(formatted, out timeOnly)); Assert.False(TimeOnly.TryParse(formatted.AsSpan(), out timeOnly)); }
public async Task TimeOnlyValueTest() { var expected = "12:30"; TimeOnly?value = null; using var ctx = new TestContext(); var cut = ctx.RenderComponent <BSInput <TimeOnly?> >(parameters => parameters.Add(p => p.Value, value) .Add(p => p.ValueChanged, e => { value = e; }) ); cut.Find("input").Change(expected); Assert.Equal(TimeOnly.Parse(expected), value); }
public static void BasicFormatParseTest() { string pattern = "hh:mm:ss tt"; DateTime dt = DateTime.Now; TimeOnly timeOnly = new TimeOnly(dt.Hour, dt.Minute, dt.Second); string s = timeOnly.ToString(pattern); TimeOnly parsedTimeOnly = TimeOnly.Parse(s); Assert.True(TimeOnly.TryParse(s, out TimeOnly parsedTimeOnly1)); Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12); Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute); Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly1.Hour % 12); Assert.Equal(timeOnly.Minute, parsedTimeOnly1.Minute); parsedTimeOnly = TimeOnly.Parse(s.AsSpan()); Assert.True(TimeOnly.TryParse(s.AsSpan(), out parsedTimeOnly1)); Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12); Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute); Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly1.Hour % 12); Assert.Equal(timeOnly.Minute, parsedTimeOnly1.Minute); s = timeOnly.ToString(pattern, CultureInfo.InvariantCulture); parsedTimeOnly = TimeOnly.Parse(s, CultureInfo.InvariantCulture); Assert.True(TimeOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture); Assert.True(TimeOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(parsedTimeOnly, parsedTimeOnly1); Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out parsedTimeOnly1)); AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal)); Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out parsedTimeOnly1)); AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal)); Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out parsedTimeOnly1)); AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)); Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out parsedTimeOnly1)); AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault)); s = " " + s + " "; parsedTimeOnly = TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces); Assert.Equal(timeOnly, parsedTimeOnly); parsedTimeOnly = TimeOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces); Assert.Equal(timeOnly, parsedTimeOnly); }
public async Task TimeOnlyValueTest() { var expected = TimeOnly.Parse("12:30:00"); var value = TimeOnly.Parse("12:30:00"); using var ctx = new TestContext(); var cut = ctx.RenderComponent <BSInputCheckbox <TimeOnly> >(parameters => parameters.Add(p => p.Value, value) .Add(p => p.CheckedValue, TimeOnly.Parse("12:30:00")) .Add(p => p.UnCheckedValue, TimeOnly.Parse("12:30:00")) .Add(p => p.ValueChanged, e => { value = e; }) ); cut.Find("input").Click(); Assert.Equal(expected, value); cut.Find("input").Click(); Assert.Equal(TimeOnly.Parse("12:30:00"), value); }
public override IEnumerable <ConvertTest> ConvertFromTestData() { TimeOnly timeOnly = new TimeOnly(10, 30, 50); yield return(ConvertTest.Valid("", TimeOnly.MinValue)); yield return(ConvertTest.Valid(" ", TimeOnly.MinValue)); yield return(ConvertTest.Valid(timeOnly.ToString(), TimeOnly.Parse(timeOnly.ToString()))); yield return(ConvertTest.Valid(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat), TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat)))); yield return(ConvertTest.Valid(" " + timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat) + " ", TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat)))); yield return(ConvertTest.Throws <FormatException>("invalid")); yield return(ConvertTest.CantConvertFrom(new object())); yield return(ConvertTest.CantConvertFrom(1)); }
/// <summary> /// Converts the given value object to a <see cref='System.TimeOnly'/> object. /// </summary> /// <inheritdoc /> public override object?ConvertFrom(ITypeDescriptorContext?context, CultureInfo?culture, object value) { if (value is string text) { text = text.Trim(); if (text.Length == 0) { return(TimeOnly.MinValue); } try { // See if we have a culture info to parse with. If so, then use it. DateTimeFormatInfo?formatInfo = null; if (culture != null) { formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo)); } if (formatInfo != null) { return(TimeOnly.Parse(text, formatInfo)); } else { return(TimeOnly.Parse(text, culture)); } } catch (FormatException e) { throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, (string)value, nameof(TimeOnly)), e); } } return(base.ConvertFrom(context, culture, value)); }
public void Data_Type_TimeOnly() { new HashBuilder().Add(TimeOnly.MinValue).GetHash().Should().Be("{abe11b52-2c1b-08cd-8b0e-dec4c52469bb}"); new HashBuilder().Add(TimeOnly.Parse("12:34")).GetHash().Should().Be("{05f5112f-2bac-73ea-0072-8ce0c890650a}"); new HashBuilder().Add(TimeOnly.MaxValue).GetHash().Should().Be("{e9eb33dc-dcb0-5d8b-a73f-290d02feb498}"); }
/// <summary> /// Register built-in conversions. This list is expected to grow over time. /// </summary> static ValueConverter() { Register <string, sbyte>(value => sbyte.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, byte>(value => byte.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, short>(value => short.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, ushort>(value => ushort.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, int>(value => int.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, uint>(value => uint.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, long>(value => long.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, ulong>(value => ulong.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, BigInteger>(value => BigInteger.Parse(value, NumberFormatInfo.InvariantInfo)); Register <string, float>(value => float.Parse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo)); Register <string, double>(value => double.Parse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo)); Register <string, decimal>(value => decimal.Parse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo)); #if NET6_0_OR_GREATER Register <string, DateOnly>(value => DateOnly.Parse(value, DateTimeFormatInfo.InvariantInfo)); Register <string, TimeOnly>(value => TimeOnly.Parse(value, DateTimeFormatInfo.InvariantInfo)); #endif Register <string, DateTime>(value => DateTimeOffset.Parse(value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal).UtcDateTime); Register <string, DateTimeOffset>(value => DateTimeOffset.Parse(value, DateTimeFormatInfo.InvariantInfo)); Register(typeof(string), typeof(bool), value => { string stringValue = (string)value; if (string.CompareOrdinal(stringValue, "1") == 0) { return(BoolBox.True); } else if (string.CompareOrdinal(stringValue, "0") == 0) { return(BoolBox.False); } return(Convert.ToBoolean(value, NumberFormatInfo.InvariantInfo).Boxed()); }); Register <string, Guid>(value => Guid.Parse(value)); Register <string, Uri>(value => new Uri(value)); Register <string, byte[]>(value => Convert.FromBase64String(value)); // such a built-in conversion for string->byte[] seems useful Register <DateTime, DateTimeOffset>(value => value); Register <DateTimeOffset, DateTime>(value => value.UtcDateTime); Register <TimeSpan, long>(value => (long)value.TotalSeconds); Register <int, sbyte>(value => checked ((sbyte)value)); Register <int, byte>(value => checked ((byte)value)); Register <int, short>(value => checked ((short)value)); Register <int, ushort>(value => checked ((ushort)value)); Register(typeof(int), typeof(bool), value => Convert.ToBoolean(value, NumberFormatInfo.InvariantInfo).Boxed()); Register <int, uint>(value => checked ((uint)value)); Register <int, long>(value => value); Register <int, ulong>(value => checked ((ulong)value)); Register <int, BigInteger>(value => new BigInteger(value)); Register <int, double>(value => value); Register <int, float>(value => value); Register <int, decimal>(value => value); Register <int, TimeSpan>(value => TimeSpan.FromSeconds(value)); Register <long, sbyte>(value => checked ((sbyte)value)); Register <long, byte>(value => checked ((byte)value)); Register <long, short>(value => checked ((short)value)); Register <long, ushort>(value => checked ((ushort)value)); Register <long, int>(value => checked ((int)value)); Register <long, uint>(value => checked ((uint)value)); Register <long, ulong>(value => checked ((ulong)value)); Register <long, BigInteger>(value => new BigInteger(value)); Register <long, double>(value => value); Register <long, float>(value => value); Register <long, decimal>(value => value); Register <long, TimeSpan>(value => TimeSpan.FromSeconds(value)); Register <BigInteger, sbyte>(value => checked ((sbyte)value)); Register <BigInteger, byte>(value => checked ((byte)value)); Register <BigInteger, decimal>(value => checked ((decimal)value)); Register <BigInteger, double>(value => checked ((double)value)); Register <BigInteger, short>(value => checked ((short)value)); Register <BigInteger, long>(value => checked ((long)value)); Register <BigInteger, sbyte>(value => checked ((sbyte)value)); Register <BigInteger, ushort>(value => checked ((ushort)value)); Register <BigInteger, uint>(value => checked ((uint)value)); Register <BigInteger, ulong>(value => checked ((ulong)value)); Register <BigInteger, int>(value => checked ((int)value)); Register <BigInteger, float>(value => checked ((float)value)); Register <uint, sbyte>(value => checked ((sbyte)value)); Register <uint, byte>(value => checked ((byte)value)); Register <uint, int>(value => checked ((int)value)); Register <uint, long>(value => value); Register <uint, ulong>(value => value); Register <uint, short>(value => checked ((short)value)); Register <uint, ushort>(value => checked ((ushort)value)); Register <uint, BigInteger>(value => new BigInteger(value)); Register <ulong, BigInteger>(value => new BigInteger(value)); Register <byte, sbyte>(value => checked ((sbyte)value)); Register <byte, int>(value => value); Register <byte, long>(value => value); Register <byte, ulong>(value => value); Register <byte, short>(value => value); Register <byte, ushort>(value => value); Register <byte, BigInteger>(value => new BigInteger(value)); Register <sbyte, byte>(value => checked ((byte)value)); Register <sbyte, int>(value => value); Register <sbyte, long>(value => value); Register <sbyte, ulong>(value => checked ((ulong)value)); Register <sbyte, short>(value => value); Register <sbyte, ushort>(value => checked ((ushort)value)); Register <sbyte, BigInteger>(value => new BigInteger(value)); Register <float, double>(value => value); Register <float, decimal>(value => checked ((decimal)value)); Register <float, BigInteger>(value => new BigInteger(value)); Register <double, float>(value => checked ((float)value)); Register <double, decimal>(value => checked ((decimal)value)); Register <char, byte>(value => checked ((byte)value)); Register <char, int>(value => value); Register <decimal, double>(value => checked ((double)value)); }
public static void FormatParseTest() { string[] patterns = new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern, "t", "T", "o", "r" }; TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); foreach (string format in patterns) { string formattedTime = timeOnly.ToString(format); timeOnly = TimeOnly.Parse(formattedTime); Assert.True(TimeOnly.TryParse(formattedTime, out TimeOnly parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly1); TimeOnly parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan()); Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.Parse(formattedTime, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParse(formattedTime, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan(), CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format); Assert.True(TimeOnly.TryParseExact(formattedTime, format, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan()); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns); Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); } }
public virtual TimeOnly GetTimeOnly(int ordinal) => TimeOnly.Parse(GetString(ordinal), CultureInfo.InvariantCulture);
//TODO: benchmark PrimitiveConverters vs PrimitiveConverter() // private static readonly IReadOnlyDictionary<Type, Func<string, object?>> PrimitiveConverters = // new Dictionary<Type, Func<string, object?>> // { // [typeof(sbyte)] = v => sbyte.Parse(v, FormatProvider), // [typeof(byte)] = v => byte.Parse(v, FormatProvider), // [typeof(short)] = v => short.Parse(v, FormatProvider), // [typeof(ushort)] = v => ushort.Parse(v, FormatProvider), // [typeof(int)] = v => int.Parse(v, FormatProvider), // [typeof(uint)] = v => uint.Parse(v, FormatProvider), // [typeof(long)] = v => long.Parse(v, FormatProvider), // [typeof(ulong)] = v => ulong.Parse(v, FormatProvider), //#if NET5_0_OR_GREATER // [typeof(Half)] = v => Half.Parse(v, FormatProvider), //#endif // [typeof(float)] = v => float.Parse(v, FormatProvider), // [typeof(double)] = v => double.Parse(v, FormatProvider), // [typeof(decimal)] = v => decimal.Parse(v, FormatProvider), // [typeof(Guid)] = v => Guid.Parse(v), // [typeof(DateTime)] = v => DateTime.Parse(v, FormatProvider), // [typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, FormatProvider), // [typeof(TimeSpan)] = v => TimeSpan.Parse(v, FormatProvider), // }; private static object?PrimitiveConverter(Type targetType, string value) { if (targetType.IsPrimitive) { if (targetType == typeof(sbyte)) { return(sbyte.Parse(value, FormatProvider)); } if (targetType == typeof(byte)) { return(byte.Parse(value, FormatProvider)); } if (targetType == typeof(short)) { return(short.Parse(value, FormatProvider)); } if (targetType == typeof(ushort)) { return(ushort.Parse(value, FormatProvider)); } if (targetType == typeof(int)) { return(int.Parse(value, FormatProvider)); } if (targetType == typeof(uint)) { return(uint.Parse(value, FormatProvider)); } if (targetType == typeof(long)) { return(long.Parse(value, FormatProvider)); } if (targetType == typeof(ulong)) { return(ulong.Parse(value, FormatProvider)); } #if NET5_0_OR_GREATER if (targetType == typeof(Half)) { return(Half.Parse(value, FormatProvider)); } #endif if (targetType == typeof(float)) { return(float.Parse(value, FormatProvider)); } if (targetType == typeof(double)) { return(double.Parse(value, FormatProvider)); } if (targetType == typeof(decimal)) { return(decimal.Parse(value, FormatProvider)); } } if (targetType == typeof(Guid)) { return(Guid.Parse(value)); } if (targetType == typeof(TimeSpan)) { return(TimeSpan.Parse(value, FormatProvider)); } if (targetType == typeof(DateTime)) { return(DateTime.Parse(value, FormatProvider)); } if (targetType == typeof(DateTimeOffset)) { return(DateTimeOffset.Parse(value, FormatProvider)); } #if NET6_0_OR_GREATER if (targetType == typeof(DateOnly)) { return(DateOnly.Parse(value, FormatProvider)); } if (targetType == typeof(TimeOnly)) { return(TimeOnly.Parse(value, FormatProvider)); } #endif return(null); }