简体   繁体   中英

DateTime.TryParse() and DateTime.TryParseExact() doesn't parse date on linux/macos and work properly on windows

I try to parse a date that has format "Вс, 1 дек 2019 20:40:00 +0300" (Russian language) dotnet core 3.1 and both TryParse() and TryParseExact() work properly on windows but don't at linux and macos. I tried different formats and different DateTimeStyles, current culture and used System.Globalization.CultureInfo.GetCultureInfo("ru-RU") for getting russian language culture object. Tried it on ubuntu on several machines - it doesnt' parse date. This is the code I use with some formats:

string dateStr = "Вс, 1 дек 2019 20:40:00 +0300";
        string dateStr2 = "Вс, 1 дек 2019 20:50:00";
        DateTime result = default(DateTime);
        DateTime result2 = default(DateTime);
        string[] dateFormats = new string[]{
            "ddd, d MMM yyyy HH:mm:ss",
            "ddd, d MMM yyyy HH:mm:ss K",
            "ddd, d MMM yyyy H:mm:ss",
            "ddd, d MMM yyyy H:mm:ss K"
        };
        var ruRu = System.Globalization.CultureInfo.GetCultureInfo("ru-RU"); 

        if(!DateTime.TryParse(dateStr, ruRu, DateTimeStyles.None, out result)){
            DateTime.TryParseExact(dateStr, dateFormats,
            //System.Globalization.CultureInfo.CurrentCulture,
            ruRu,
            System.Globalization.DateTimeStyles.AssumeUniversal, out result);
        }

        if (!DateTime.TryParse(dateStr2, ruRu, DateTimeStyles.None, out result2)){
            DateTime.TryParseExact(dateStr2, dateFormats,
            //System.Globalization.CultureInfo.CurrentCulture,
            ruRu,
            System.Globalization.DateTimeStyles.AssumeUniversal, out result2);
        }

        Console.WriteLine(result);
        Console.WriteLine(result2);

In both cases under linux and macos I got '1/1/0001 12:00:00 AM'

well, seems that Linux .Net Core 2.2 has different month names defined comparing with the Windows one:

TestContext.WriteLine("month:{0}", string.Join(";", ruRu.DateTimeFormat.AbbreviatedMonthNames));

output is month:янв;фев;мар;апр;май;июн;июл;авг;сен;окт;ноя;дек; on Windows

but month:янв.;февр.;март;апр.;май;июнь;июль;авг.;сент.;окт.;нояб.;дек.; on Ubuntu (do not ask me why). Dot is added at the end of months abbreviations.

Also you have to take into account that июнь;июль;март are not shortened and февр is not like on Windows фев

Mac OS is build on some Unix kernel actually so it has the same .net framework as on Ubuntu I suppose.

Your case can be solved with new format string like below:

string[] dateFormats = new string[]{
"ddd, d MMM yyyy HH:mm:ss",
"ddd, d MMM yyyy HH:mm:ss K",
"ddd, d MMM. yyyy HH:mm:ss K", // this is added for linux 
"ddd, d MMM yyyy H:mm:ss",
"ddd, d MMM yyyy H:mm:ss K"
};

However it looks like a bug in the .net since will not parse March month from the Пн, 4 мар 2019 00:00:00 for example.

UPD: I've checked and had not found the way to parse Пн, 4 мар 2019 00:00:00 on Linux using the DateTime.TryParseExact because of the март on Linux will not be parsed from the мар

Seems that you should register new issue for .Net Core. Or parse the string manually but it sucks

Code below works :

            string[] dateStrs = {"Вс, 1 дек 2019 20:40:00 +0300","Вс, 1 дек 2019 20:50:00"};
            var ruRu = System.Globalization.CultureInfo.GetCultureInfo("ru-RU");
            string[] dateFormats = new string[]{
                "ddd, d MMM yyyy HH:mm:ss",
                "ddd, d MMM yyyy HH:mm:ss K",
                "ddd, d MMM yyyy H:mm:ss",
                "ddd, d MMM yyyy H:mm:ss K"
            };
            DateTime date;
            Boolean match = false;
            foreach (string dateStr in dateStrs)
            {
                match = DateTime.TryParseExact(dateStr,dateFormats, ruRu, System.Globalization.DateTimeStyles.None, out date);
                Console.WriteLine(date.ToString());

            }
            Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM