7

Using the Upper Sorbian culture (hsb) a DateTime object converted to a string uses the format "d. M. yyyy H.mm.ss 'hodź.'". ToString("G") for example returns "31. 12. 2011 5.06.07 hodź." for the 31. of December 2011, 05:06:07 AM.

Problem is though that trying to convert such a string back to a DateTime does not result true. Even simpler strings like "1. 1. 2011" or "1.1.2011" lead to no success. And just in case someone suggests to pass the culture when converting/persing: I did that of course.

Trying to parse "1.2.3" results in the current date with the time 01:02:03.

I consider that a bug. Or does someone know what could be wrong?

I am using .NET 4.5 RTM on a Windows 8 RTM machine.

Sample:

DateTime date = DateTime.Now;

CultureInfo culture = new CultureInfo("hsb");
string dateString = date.ToString("G", culture);
DateTime convertedDate; 

bool dateOkay = DateTime.TryParse(dateString, culture,
   DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay); 
// This results false although the date string was read by 
// ToString("G") (i.e. '20. 9. 2012 12.28.10 hodź.') and should be okay

dateString = "1. 1. 2000";
dateOkay = DateTime.TryParse(dateString, culture,
   DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay); 
// This results in false although the date string should be okay

dateString = "1.1.2000";
dateOkay = DateTime.TryParse(dateString, culture,
   DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay); 
// This results also in false

dateString = "1.2.3";
dateOkay = DateTime.TryParse(dateString, culture,
   DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay + ": " + convertedDate); 
// This results strangely in true. The converted date is the current date 
// with time 01:02:03.
bmargulies
  • 91,317
  • 38
  • 166
  • 290
Jürgen Bayer
  • 2,921
  • 3
  • 23
  • 44
  • Hmm. Your first example works for me, from Windows 7 but using .NET 4.5 RTM. Could you edit this into a short but complete console application? – Jon Skeet Sep 18 '12 at 18:29
  • You commentr indicate some correct answers but none of your questions are marked as answered. You do know the check indicates a correct answer. – paparazzo Sep 18 '12 at 18:37
  • @Jon: Sounds like a problem on Windows 8 (Microsoft changed many date formats). Here is simplified but working console app code: CultureInfo culture = new CultureInfo("hsb"); DateTime date = new DateTime(2011, 12, 31, 5, 6, 7); string dateString = date.ToString("G", culture); DateTime convertedDate; bool dateOkay = DateTime.TryParse(dateString, culture, DateTimeStyles.AllowInnerWhite, out convertedDate); Console.WriteLine(dateOkay); dateString = "1. 1. 2000"; dateOkay = DateTime.TryParse(dateString, culture, DateTimeStyles.AllowInnerWhite, out convertedDate); – Jürgen Bayer Sep 18 '12 at 20:21
  • Console.WriteLine(dateOkay); dateString = "1.1.2000"; dateOkay = DateTime.TryParse(dateString, culture, DateTimeStyles.AllowInnerWhite, out convertedDate); Console.WriteLine(dateOkay); dateString = "1.2.3"; dateOkay = DateTime.TryParse(dateString, culture, DateTimeStyles.AllowInnerWhite, out convertedDate); Console.WriteLine(dateOkay); Console.WriteLine(convertedDate); – Jürgen Bayer Sep 18 '12 at 20:22
  • Result is False False False True 18.09.2012 01:02:03 on my system (German) – Jürgen Bayer Sep 18 '12 at 20:23
  • @Blam. Sorry for that. I correct that. – Jürgen Bayer Sep 18 '12 at 20:24
  • 2
    @JürgenBayer: Please edit the console app into your question - code in comments doesn't read nicely. – Jon Skeet Sep 18 '12 at 21:09
  • I don't have an answer, but just to confirm the behaviour, running on Windows 8 Pro with VS 2012 + VS 2012 patch 1 as a 4.5 project I get false, false, false, true. – Ian Nov 30 '12 at 21:55
  • On Windows 7 it's True, True, True, True, and the last one is interpreted as day.month.year, so February 1st, 2003. I think you should file a bug report on the Windows 8 behavior. – Jeppe Stig Nielsen Feb 14 '13 at 07:00
  • I would file a bug report if I knew where. Microsoft Connect does not have a page for Windows 8 or the Windows Runtime. For all bugs reports I filed on the Visual Studio and .NET Framework connect page regarding the Windows Runtime I got the feedback that they were forwarded to the appropriate team. The bug entries were closed then. For me they kind of disappeared. – Jürgen Bayer Feb 14 '13 at 12:51

1 Answers1

0

In .Net 4.5 "hsb" is marked as neutral culture, so all DateTime parsing will be done by standard format provider. Use DateTime.ParseExact with format string instead. http://www.codeproject.com/Articles/3612/Use-of-different-Date-Time-formats-and-Culture-typ

===============================

I found when flag "IsNeutralCulture" in CultureInfo equals "true", date string parses in invariant formats (en-US). When I passed format MM/dd/yyyy DateTime.TryParse parsed date properly for culture "hsb".

And some quote from article, that I provided: "A DateTimeFormatInfo can be created only for the invariant culture or for specific culture, not for a neutral culture. DateTimeFormatInfo inherits from Object and implements ICloneable and IFormarProvider interfaces."

I found that DateTimeFormatInfo is specified for "hsb"culture, but as I told before, IsNeutralCulture = true. I expect in .Net framework 4.5 DateTimeFormatInfo is not using for parsing date when "IsNeutralCulture" equals "true"

QArea
  • 4,769
  • 1
  • 10
  • 22
  • Sure is "hsb" a neutral culture. As is "en" or "de". What do you mean with "standard format provider". Even for neutral cultures the appropriate format provider is used. Try with any neutral culture. The problem is not how to format dates though. I suppose that the resulting string is right for Upper Sorbian. The problem is that the formatted date is not recognized as a date when trying to parse it. – Jürgen Bayer Apr 02 '13 at 15:43