DateTime.TryParse无法分析DateTime.MinValue [英] DateTime.TryParse cannot parse DateTime.MinValue

查看:177
本文介绍了DateTime.TryParse无法分析DateTime.MinValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个名为Json.NET库,使用下面的code内部解析一个JSON字符串转换为日期时间:

I'm using a library called Json.NET that uses the following code internally to parse a JSON string into a DateTime:

if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
{
     dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
     SetToken(JsonToken.Date, dt);
     return dt;
}

我想Json.NET是搞砸了转换,但它看起来像它的DateTime.TryParse本身搞坏真实的价值。

I thought Json.NET was screwing up the conversion, but it looks like it's DateTime.TryParse itself that's botching the value.

当我分析了以下有效的ISO日期(对应于UTC DateTime.MinValue):

When I parse the following valid Iso date (which corresponds to UTC DateTime.MinValue):

string json = "0001-01-01T00:00:00+00:00";
DateTime dt;
DateTime.TryParse(json, invariantCulture, DateTimeStyles.RoundtripKind, out dt);

其结果是一个本地化的日期时间: {0001-01-01下午8点00分00秒} ,将其转换回UTC时间给 {0001-01-02下午12时○○分00秒} 。从本质上讲,日溢,而这正是这类问题,你会期望DateTimeStyles.RoundtripKind避免的。

The result is a localized DateTime: {0001-01-01 8:00:00 PM}, which when converted back to Utc time gives {0001-01-02 0:00:00 PM}. Essentially, the date underflowed, which is exactly the kind of problem you would expect DateTimeStyles.RoundtripKind to avoid.

我如何避免这种情况?

推荐答案

为什么要用DateTimeStyles.RoundtripKind?该文档RoundtripKind说:

Why use DateTimeStyles.RoundtripKind? The documentation for RoundtripKind says:

日期的DateTimeKind场是当一个DateTime对象转换为使用O或R的标准格式说明符的字符串,字符串,然后转换回一个DateTime对象preserved。

The DateTimeKind field of a date is preserved when a DateTime object is converted to a string using the "o" or "r" standard format specifier, and the string is then converted back to a DateTime object.

从O或R的标准格式说明输出的字符串是不喜欢的ISO 8601字符串你正在试图解析。这听起来并不对我来说,RoundtripKind真的应该与任何日期时间字符串格式工作。这听起来像往返对于DateTime.Kind属性时,该字符串是在一个特定的格式。

The string output from the "o" or "r" standard format specifiers are not like the ISO 8601 string you are trying to parse. It doesn't sound to me like RoundtripKind is really supposed to work with any date time string format. It sounds like the round trip is for the DateTime.Kind property when the string is in a particular format.

既然你知道你正试图解析字符串的格式,那么我会建议使用DateTime.TryParseExact。

Since you know the format of the string you are trying to parse, then I would suggest using DateTime.TryParseExact.

我不得不支持了几个不同版本的ISO 8601字符串 - 无论这些格式都是有效的日期时间值在ISO 8601(也有日期,时间和分数秒甚至更多的选择,但我没有'牛逼的):

I have had to support a couple different versions of the ISO 8601 string - either of these formats are valid date-time values in ISO 8601 (and there are even more options for dates, times and fractional seconds, but I didn't those):

0001-01-01T00:00:00 + 00:00

0001-01-01T00:00:00+00:00

0001-01-01T00:00:00Z

0001-01-01T00:00:00Z

下面是将要处理或者这些格式的方法:

Here's a method that will handle either of these formats:

private bool TryParseIso8601(string s, out DateTime result)
{
    if (!string.IsNullOrEmpty(s))
    {
        string format = s.EndsWith("Z") ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:sszzz";
        return DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result);
    }

    result = new DateTime(0L, DateTimeKind.Utc);
    return false;
}

这篇关于DateTime.TryParse无法分析DateTime.MinValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆