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 #1322

Closed
GeirGrusom opened this Issue Jul 31, 2015 · 5 comments

Comments

Projects
None yet
4 participants
@GeirGrusom

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

This comment has been minimized.

Show comment
Hide comment
Member

weshaggard commented Jul 31, 2015

@tarekgh

This comment has been minimized.

Show comment
Hide comment
@tarekgh

tarekgh Jul 31, 2015

Member

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

Member

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

@jouniheikniemi

This comment has been minimized.

Show comment
Hide comment
@jouniheikniemi

jouniheikniemi 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 :-)

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

This comment has been minimized.

Show comment
Hide comment
@tarekgh

tarekgh Aug 30, 2015

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@tarekgh

tarekgh Sep 22, 2015

Member

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

Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment