public static void TryFormatTest() { Span <char> buffer = stackalloc char[100]; TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); buffer.Fill(' '); Assert.True(timeOnly.TryFormat(buffer, out int charsWritten)); Assert.Equal(charsWritten, buffer.TrimEnd().Length); buffer.Fill(' '); Assert.True(timeOnly.TryFormat(buffer, out charsWritten, "o")); Assert.Equal(16, charsWritten); Assert.Equal(16, buffer.TrimEnd().Length); buffer.Fill(' '); Assert.True(timeOnly.TryFormat(buffer, out charsWritten, "R")); Assert.Equal(8, charsWritten); Assert.Equal(8, buffer.TrimEnd().Length); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten)); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "r")); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "O")); Assert.Throws <FormatException>(() => { Span <char> buff = stackalloc char[100]; timeOnly.TryFormat(buff, out charsWritten, "u"); }); Assert.Throws <FormatException>(() => { Span <char> buff = stackalloc char[100]; timeOnly.TryFormat(buff, out charsWritten, "dd-yyyy"); }); Assert.Throws <FormatException>(() => $"{timeOnly:u}"); Assert.Throws <FormatException>(() => $"{timeOnly:dd-yyyy}"); }
public static void TryFormatTest() { Span <char> buffer = stackalloc char[100]; TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); Assert.True(timeOnly.TryFormat(buffer, out int charsWritten)); Assert.True(timeOnly.TryFormat(buffer, out charsWritten, "o")); Assert.Equal(16, charsWritten); Assert.True(timeOnly.TryFormat(buffer, out charsWritten, "R")); Assert.Equal(8, charsWritten); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten)); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "r")); Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "O")); }