即使日期是JSON中的UTC,为什么ServiceStack会发出本地时间?

时间:2014-06-03 17:19:14

标签: c# servicestack servicestack-text

长话短说 - 通过ServiceStack.Text的JSON解析器跳转的日期会丢失时区信息。奇怪的是,DateTimeSerializerTests.DateTime_Is_Serialized_As_Utc_and_Deserialized_as_local()似乎期望这种行为,并且DateTimeSerializer.Prepare()在解析为UTC的每个日期时间对象上显式调用ToLocalTime()

这是一个示例测试用例(MSTest,但很容易在任何地方运行)。本地通行证,但UTC和未指定不通过 - DateTime对象返回的类型始终为“本地”。

[TestMethod]
public void TestParseSingleDateTime_UTC()
{
    // In canonical UTC format
    var date = "2014-06-03T14:26:20.0030000Z";
    var raw = new DateTime(2014, 6, 3, 14, 26, 20, 3, DateTimeKind.Utc);
    var value = DateTimeSerializer.ParseShortestXsdDateTime(date);
    Assert.AreEqual(DateTimeKind.Utc, value.Kind);
    Assert.AreEqual(raw, value);
}

[TestMethod]
public void TestParseSingleDateTime_Local()
{
    // In local time zone
    var date = "2014-06-02T11:15:49.1480000-05:00";
    var raw = new DateTime(2014, 6, 2, 11, 15, 49, 148, DateTimeKind.Local);
    var value = DateTimeSerializer.ParseShortestXsdDateTime(date);
    Assert.AreEqual(DateTimeKind.Local, value.Kind);
    Assert.AreEqual(raw, value);
}

[TestMethod]
public void TestParseSingleDateTime_Unspecified()
{
    // Unspecified time zone, as we would parse from Excel cells with dates
    var date = "2012-01-06T00:00:00.0000000";
    var raw = new DateTime(2012, 1, 6, 0, 0, 0, DateTimeKind.Unspecified);
    var value = DateTimeSerializer.ParseShortestXsdDateTime(date);
    Assert.AreEqual(DateTimeKind.Unspecified, value.Kind);
    Assert.AreEqual(raw, value);
}

为什么这个默认行为?在这里使用JsConfig.AlwaysUseUtc不是一个好的解决方法,因为我无法将本地时间戳解析为本地时间戳。

1 个答案:

答案 0 :(得分:2)

如果有人发现了它,尽管它已经很老了,但应该可以通过JSON解析器的配置(可以在JsConfig全局使用)对其进行完全控制。

下面的示例(尽管未经测试)应该大致涵盖了我所理解的情况:

// Formats to use for the different date kinds
string utcTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'";
string localTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.fffffff";

// Serialization function
// Check if specified as UTC otherwise treat as local. 
JsConfig<DateTime>.SerializeFn = datetime =>
{
    switch (datetime.Kind)
    {
        case DateTimeKind.Utc:
            return datetime.ToString(utcTimeFormat);
        default: //DateTimeKind.Unspecified and DateTimeKind.Local
            return datetime.ToString(localTimeFormat);
    }
};

// Deserialization function
// Check which format provided, attempt to parse as datetime or return minValue.
JsConfig<DateTime>.DeSerializeFn = datetimeStr =>
{
    if (string.IsNullOrWhiteSpace(datetimeStr))
    {
        return DateTime.MinValue;
    }

    if (datetimeStr.EndsWith("Z") && 
        DateTime.TryParseExact(datetimeStr, utcTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out DateTime resultUtc))
    {
        return resultUtc;
    }
    else if (!datetimeStr.EndsWith("Z") && 
        DateTime.TryParseExact(datetimeStr, localTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime resultLocal))
    {
        return resultLocal;
    }

    return DateTime.MinValue;
};

为什么发生这种情况要么是设计选择,要么是我无法评论的疏忽。