DateTimeConverter从UTC字符串转换 [英] DateTimeConverter converting from UTC string

查看:203
本文介绍了DateTimeConverter从UTC字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个序列化为字符串 2012-06-20T13:19:59.1091122Z的日期

I have a date serialized as a string "2012-06-20T13:19:59.1091122Z"

使用 DateTimeConverter ,它会被转换为DateTime对象{22:49:59.1091122},其中种类属性设置为本地。

Using the DateTimeConverter, this gets converted to a DateTime object {22:49:59.1091122} with the Kind property set to "Local".

例如。以下测试失败:

    private static readonly DateTime UtcDate = new DateTime(634757951991091122, DateTimeKind.Utc);
    private const string UtcSerialisedDate = "2012-06-20T13:19:59.1091122Z";

    [Test]
    public void DateTimeConverter_Convert_From_Utc_String()
    {
        // Arrange
        var converter = TypeDescriptor.GetConverter(typeof(DateTime));

        // Act
        var result = converter.ConvertFrom(UtcSerialisedDate);

        // Assert
        Assert.AreEqual(UtcDate, result);
        Assert.AreEqual(DateTimeKind.Utc, ((DateTime)result).Kind);
    }

我对此感到有些惊讶...我本来希望

I'm a bit surprised by this... I would have expected that the DateTime object returned by the converter would be in UTC.

文档确实说DateTimeConverter使用 DateTime.Parse ,但我想它一定不能使用 DateTimeStyles.RoundtripKind 选项。

The docs do say that DateTimeConverter uses DateTime.Parse, but I'm guessing it must not use the DateTimeStyles.RoundtripKind option.

推荐答案

这里的真正错误是没有传递任何DateTimeStyles的DateTime.Parse()应该仍然可以看到 Z,并意识到应该将其解析为UTC。

The real bug here is that DateTime.Parse() without any DateTimeStyles passed should still see the "Z" and recognize that it should be parsed as UTC. But good luck getting MS to acknowledge or fix that.

您的代码示例所说明的特殊问题是DateTimeConverter必须重写TypeConverter中的方法,因此无能为力。传递额外的参数,例如DateTimeStyles。太糟糕了,它没有为此实现某种静态或线程静态属性。它确实确实利用了线程的Culture.CurrentCulture,但DateTimeStyles与文化是分开的,所以,可惜-这是另一个死胡同。

The particular problem illustrated by your code sample is that the DateTimeConverter has to override methods from TypeConverter, and thus has no ability to pass extra parameters such as DateTimeStyles. It's too bad it doesn't implement some sort of static or threadstatic property for this. It does indeed take advantage of the thread's Culture.CurrentCulture, but DateTimeStyles are a separate thing from culture, so alas - that's another dead end.

我认为您已被锁定使用转换器,而不是直接调用解析?这是一个硬性要求吗?如果不是,则可以执行以下操作:

I assume you are locked in to using a converter, rather than just calling parse directly? Is this a hard requirement? If not, you could do the following:

public static object ConvertFrom<T>(string value)
{
  if (typeof(T) == typeof(DateTime))
    return DateTime.Parse(value, null, DateTimeStyles.RoundtripKind);

  var converter = TypeDescriptor.GetConverter(typeof(T));
  return converter.ConvertFrom(value);
}

另一种方法是使用 DateTimeOffsetConverter 代替-它正确理解Z时区。然后,您可以使用结果的 .UtcDateTime 属性返回具有UTC类型的DateTime。

Another approach would be to use a DateTimeOffsetConverter instead - it understands the Z timezone properly. You could then use the .UtcDateTime property of the result to get back to a DateTime with a UTC kind.

这篇关于DateTimeConverter从UTC字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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