Skip to content

Add the support of DateOnly and TimeOnly types in TryParse method Utf8Parser class #53768

Description

@goldytech

Background and Motivation

In .the current version of .NET (5.0.203) Utf8Parser class has only support for DateTime.
https://docs.microsoft.com/en-us/dotnet/api/system.buffers.text.utf8parser.tryparse?view=net-5.0#System_Buffers_Text_Utf8Parser_TryParse_System_ReadOnlySpan_System_Byte__System_DateTime__System_Int32__System_Char_

With .NET 6 Preview 4 two new types have been introduced DateOnly and TimeOnly. It would be good to have the support of these two types in the TryParse method of Utf8Parser class. In the current version if the date is only passed without time the out variable returns a null value. I believe there will be legitimate UseCases where only date and time parsing are required explicitly. In order to extract the DateTime now , developers have to fallback to Encoding.UTF8.GetString() and then use DateTime.TryParse() method to get the date / time value. This is resource intensive operation and allocations get increased.
Eg.

if (DateTime.TryParse(Encoding.UTF8.GetString(buffer, out var dateofJoining))

                           {
                                 record.DateOfJoining = dateofJoining;
                            }  

Proposed API

Additional two overload methods must be added to current Utf8Parser static class to support DateOnly and TimeOnly types.

Usage Examples

Eg
For DateOnly type

if (Utf8Parser.TryParse(buffer, out DateOnly value, out var bytesConsumed))
                                {
                                    record.DateOfJoining = value;
                                }

For TimeOnly type

if (Utf8Parser.TryParse(buffer, out TimeOnly value, out var bytesConsumed))
                                {
                                    record.ApplicationTime = value;
                                }

Alternative Designs

Risks

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 5, 2021
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 5, 2021
  3. added this to the Future milestone on Jun 5, 2021
  4. tarekgh commented on Jun 5, 2021

    @tarekgh
    Member
  5. teo-tsirpanis commented on Mar 15, 2022

    @teo-tsirpanis
    Contributor

    And Half. And corresponding members in Utf8Formatter.

  6. tannergooding commented on Sep 8, 2022

    @tannergooding
    Member

    @teo-tsirpanis could you open a separate proposal for Half?

  7. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Sep 8, 2022
  8. bartonjs commented on Jul 20, 2023

    @bartonjs
    Member

    Video

    Rather than adding things on Utf8Parser, the plan forward is to add the UTF-8 parse methods to the relevant types themselves (generally along with implementing IUtf8SpanParsable.

    For DateOnly and TimeOnly, these API additions are already covered under #81500.

    It came up in discussion that DateOnly (and maybe TimeOnly) might want more options than the simple parse, like DateTime.TryParseExact has, but those would just be overloads to the TryParse methods on their types, and still wouldn't affect Utf8Parser. As those also weren't part of this original proposal, they're left for someone else to do the research on and create a dedicated issue for.

  9. removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Jul 20, 2023
  10. ghost locked as resolved and limited conversation to collaborators on Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions