public static void OAndRFormatsTest() { DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); string formattedDate = dateOnly.ToString("o"); Assert.Equal(10, formattedDate.Length); Assert.Equal('-', formattedDate[4]); Assert.Equal('-', formattedDate[7]); DateOnly parsedDateOnly = DateOnly.Parse(formattedDate); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan()); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.ParseExact(formattedDate, "O"); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), "O".AsSpan()); Assert.Equal(dateOnly, parsedDateOnly); formattedDate = dateOnly.ToString("r"); Assert.Equal(16, formattedDate.Length); Assert.Equal(',', formattedDate[3]); Assert.Equal(' ', formattedDate[4]); Assert.Equal(' ', formattedDate[7]); Assert.Equal(' ', formattedDate[11]); parsedDateOnly = DateOnly.Parse(formattedDate); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan()); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.ParseExact(formattedDate, "R"); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), "R".AsSpan()); Assert.Equal(dateOnly, parsedDateOnly); }
public override DateOnly?ReadJson(JsonReader reader, Type objectType, DateOnly?existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.Value == null) { return(null); } if (reader.Value.GetType() == typeof(string)) { Regex dateOnlyRegex = new(@"\d{4}-\d{1,2}-\d{1,2}"); Match match = dateOnlyRegex.Match((string)reader.Value); if (match.Success && match.Groups.Count == 1) { return(DateOnly.ParseExact(match.Groups[0].Value, DateFormat, CultureInfo.InvariantCulture)); } } if (reader.Value.GetType() == typeof(DateTime)) { return(DateOnly.FromDateTime((DateTime)reader.Value)); } return(null); }
public override DateOnly?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var value = reader.GetString(); if (string.IsNullOrEmpty(value)) { return(null); } return(DateOnly.ParseExact(value, DateFormat, CultureInfo.InvariantCulture)); }
/// <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 ? DateOnly.Parse(text, formatProvider, dateTimeStyle) : DateOnly.ParseExact(text, memberMapData.TypeConverterOptions.Formats, formatProvider, dateTimeStyle)); }
public static void AllCulturesTest() { DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { string formatted = dateOnly.ToString("d", ci); DateOnly parsedDateOnly = DateOnly.ParseExact(formatted, "d", ci); Assert.Equal(dateOnly, parsedDateOnly); formatted = dateOnly.ToString("D", ci); parsedDateOnly = DateOnly.ParseExact(formatted, "D", ci); Assert.Equal(dateOnly, parsedDateOnly); } }
public static void CustomFormattingTest() { DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); string format = "dd, ddd 'dash' MMMM \"dash\" yyyy"; string formatted = dateOnly.ToString(format); DateOnly parsedDateOnly = DateOnly.ParseExact(formatted, format); Assert.Equal(dateOnly, parsedDateOnly); parsedDateOnly = DateOnly.ParseExact(formatted.AsSpan(), format.AsSpan()); Assert.Equal(dateOnly, parsedDateOnly); Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy hh")); Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy m")); Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy s")); Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy z")); }
public static void FormatParseTest() { string[] patterns = new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern, "d", "D", "o", "r" }; DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today); foreach (string format in patterns) { string formattedDate = dateOnly.ToString(format); DateOnly parsedDateOnly = DateOnly.Parse(formattedDate); Assert.True(DateOnly.TryParse(formattedDate, out DateOnly parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan()); Assert.True(DateOnly.TryParse(formattedDate.AsSpan(), out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.Parse(formattedDate, CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParse(formattedDate, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan(), CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParse(formattedDate.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate, format); Assert.True(DateOnly.TryParseExact(formattedDate, format, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), format.AsSpan()); Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), format.AsSpan(), out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate, format, CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParseExact(formattedDate, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate, patterns); Assert.True(DateOnly.TryParseExact(formattedDate, patterns, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), patterns); Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), patterns, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate, patterns, CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParseExact(formattedDate, patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), patterns, CultureInfo.CurrentCulture); Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1)); Assert.Equal(dateOnly, parsedDateOnly); Assert.Equal(dateOnly, parsedDateOnly1); } }