2013-09-27 95 views
1

我最近將我的項目的ServiceStack.Text從3.9.23升級到最新的stable。ServiceStack.Text輸出UTC偏移量

我有一些單元測試,以確保我們輸出的日期格式不會改變。升級後他們現在失敗了。該測試是這樣的:

[Test] 
[TestCase(2012, 06, 22, 03, 26, 23, 837, "\"\\/Date(1340328383837+0200)\\/\"")] // Daylight savings time test in DK (+0200) 
[TestCase(1997, 10, 30, 11, 23, 49, 060, "\"\\/Date(878207029060+0100)\\/\"")] 
[TestCase(2050, 01, 14, 00, 00, 00, 000, "\"\\/Date(2525727600000+0100)\\/\"")] 
public void SerializeDate_ReturnsExpectedOutput(int year, int month, int day, int hour, int minute, int second, int ms, string expected) 
{ 
    var dt = new DateTime(year, month, day, hour, minute, second, ms).ToUniversalTime(); 
    dt = TimeZoneInfo.ConvertTimeFromUtc(dt, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")); 

    string serialized = ServiceStack.Text.JsonSerializer.SerializeToString(dt); 

    Assert.AreEqual(expected, serialized, "DateTime Serialization failure, got {0} but expected {1} for DateTime = {2}", 
     serialized, expected, dt); 
} 

測試失敗,因爲現在ServiceStack.Text輸出的UTC零偏移,這不是我想要的東西,所以我得到:

String lengths are both 30. Strings differ at index 21. 
    Expected: ""\\/Date(1340328383837+0200)\\/"" 
    But was: ""\\/Date(1340328383837-0000)\\/"" 
    ---------------------------------^ 

如何配置ServiceStack.Text使用舊的行爲?

回答

1

設置:

ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.DCJSCompatible; 

解決了這個問題對我來說,這基本上就是我需要DateTime是否與DateTimeKind.Unspecified因爲如果他們在那裏本地時間來對待。我查看了ServiceStack.Text源,而這個處理程序就是這麼做的。請注意,處理程序會拋出UTC偏移量,並將時間當作本地時間,將字符串解析爲DateTime輸入。 (幸運的是也適用於我的應用程序)。