Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting to DateOnly/TimeOnly from string #59253

Closed
Tracked by #64603
maxkoshevoi opened this issue Sep 17, 2021 · 5 comments
Closed
Tracked by #64603

Converting to DateOnly/TimeOnly from string #59253

maxkoshevoi opened this issue Sep 17, 2021 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.DateTime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@maxkoshevoi
Copy link
Contributor

Related to: dotnet/aspnetcore#36511

Description

DateTime can be converted from string (TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString("01.01.0001")), while DateOnly and TimeOnly cannot. This causes some issues. For example, if they are added as query parameters in Asp.Net, they would be represented as 6 and 5 fields respectively instead of one string field (see dotnet/aspnetcore#36511 for more info).

For now, I've created a NuGet with custom type converters to solve this: https://github.com/maxkoshevoi/DateOnlyTimeOnly.AspNet/tree/main/DateOnlyTimeOnly.AspNet/Converters/Type

Are there any plans to enable string to DateOnly/TimeOnly conversion?

Regression?

No

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Sep 17, 2021
@ghost
Copy link

ghost commented Sep 17, 2021

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Related to: dotnet/aspnetcore#36511

Description

DateTime can be converted from string (TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString("01.01.0001")), while DateOnly and TimeOnly cannot. This causes some issues. For example, if they are added as query parameters in Asp.Net, they would be represented as 6 and 5 fields respectively instead of one string field (see dotnet/aspnetcore#36511 for more info).

For now, I've created a NuGet with custom type converters to solve this: https://github.com/maxkoshevoi/DateOnlyTimeOnly.AspNet/tree/main/DateOnlyTimeOnly.AspNet/Converters/Type

Are there any plans to enable string to DateOnly/TimeOnly conversion?

Regression?

No

Author: maxkoshevoi
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

@jeffhandley jeffhandley changed the title Convering DateOnly/TimeOnly from string Converting to DateOnly/TimeOnly from string Sep 30, 2021
@jeffhandley jeffhandley added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner labels Sep 30, 2021
@jeffhandley jeffhandley added this to the 7.0.0 milestone Sep 30, 2021
@0xced
Copy link
Contributor

0xced commented Jan 27, 2022

It's a bummer that the DateOnly and TimeOnly type converters have been forgotten in .NET 6. But this is the perfect opportunity to use the new IParseable<TSelf> preview feature and create a generic type converter in a few lines of code.

Note that you'll have to add <EnablePreviewFeatures>true</EnablePreviewFeatures> in your csproj and add a reference to the System.Runtime.Experimental package. Make sure to use version 6.0.2-mauipre.1.22054.8. For some reason version 6.0.0 doesn't work whe using the .NET 6.0.101 SDK and produces the following error:

[CS0246] The type or namespace name 'IParseable<>' could not be found (are you missing a using directive or an assembly reference?

Here's how to implement the generic converter:

using System;
using System.ComponentModel;
using System.Globalization;

namespace SampleCode;

public class ParseableTypeConverter<T> : TypeConverter where T : IParseable<T>
{
    public static void Register()
    {
        TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(ParseableTypeConverter<T>)));
    }

    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
    {
        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }

    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        if (value is string stringValue)
        {
            return T.Parse(stringValue, culture);
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Then register DateOnly and TimeOnly converters globally:

ParseableTypeConverter<DateOnly>.Register();
ParseableTypeConverter<TimeOnly>.Register();

Of course, it would still be nice to have those converters built-in for .NET 7.

@martincostello
Copy link
Member

For some reason version 6.0.0 doesn't work when using the .NET 6.0.101 SDK

#62840

@tarekgh
Copy link
Member

tarekgh commented May 1, 2022

Closing this as we are tracking the official proposal in #68743 and #68744

@tarekgh tarekgh closed this as completed May 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.DateTime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

6 participants