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

DateTime.Parse/TryParse fails if the date and time uses the same separator #4409

Closed
GeirGrusom opened this issue Jul 31, 2015 · 5 comments
Closed
Assignees

Comments

@GeirGrusom
Copy link

@GeirGrusom GeirGrusom commented Jul 31, 2015

The following code (in .NET 4.6) throws a FormatException:

var dateFormat = new DateTimeFormatInfo
{
    DateSeparator = ".",
    TimeSeparator = ".",
};

var textDate = new DateTime(2015, 1, 1).ToString(dateFormat); // 01.01.2015 00.00.00
var reverse = DateTime.Parse(textDate, dateFormat); // Throws FormatException

Windows 10 for some reason changed the time separator for some locales (norwegian, finnish and serbian) which now breaks with Parse and TryParse.

edit: missed the format on the parse.

@weshaggard
Copy link
Member

@weshaggard weshaggard commented Jul 31, 2015

@tarekgh
Copy link
Member

@tarekgh tarekgh commented Jul 31, 2015

This is known issue and not a new one. the parser is just thinking it is parsing a time (and not a date) and then decide it is not valid time. in general the Parse/TryParse is not guaranteeing the parse all possible strings and we always recommend to use ParseExact/TryParseExact to guarantee successful parsing.
Fixing the parser will not be trivial as it is written as finite state machine. we'll try to look if there is a safe way can fix this without breaking other cases

@tarekgh tarekgh self-assigned this Aug 20, 2015
@jouniheikniemi
Copy link

@jouniheikniemi jouniheikniemi commented Aug 27, 2015

Regarding the same issue in .NET Framework, see my blog post at http://www.heikniemi.net/hardcoded/2015/08/windows-10-breaks-net-date-parsing-in-certain-locales/. And now that the state machine has been fixed there, I hope the fix will flow down here as well :-)

@tarekgh
Copy link
Member

@tarekgh tarekgh commented Aug 30, 2015

The fix is already in the github side too. I didn't resolve this issue yet because I want to add some tests before closing the issue. let me know if you have any question.

@tarekgh
Copy link
Member

@tarekgh tarekgh commented Sep 22, 2015

The issue has been fixed and added the needed tests. closing the issue here.

@tarekgh tarekgh closed this Sep 22, 2015
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.