2016-01-13 127 views
2

我們在數據庫/模型中有DateTimeOffset。要在Web中顯示這些值,我們將DateTimeOffsets轉換爲當前用戶的時區。模糊DateTimeOffset示例

根據MSDN,可以的DateTimeOffset在特定的時區是不明確的:

TimeZoneInfo.IsAmbiguousTime Method (DateTimeOffset)

這沒有任何意義,我在所有。有人可以請給我一個例如DateTimeOffset這是不明確的?
我們在TimeZone「W.歐洲標準時間」。

+0

請注意,「DateTimeOffset」是時區_awareness_。它只有日期,時間和Utc偏移值。 –

回答

1

我認爲混淆來自於這裏定義的「模棱兩可」的方式。

要清楚,DateTimeOffset從來沒有模棱兩可的本身。 總是代表絕對瞬時時間的特定時刻。給定一個日期,時間和偏移量,我可以告訴你當地的時間和準確的UTC時間(通過應用偏移量)。

但是,值的壁掛時間部分可能在特定時區內不明確。也就是說,日期和時間只有,當你忽略偏移量。這就是TimeZoneInfo.IsAmbiguousTime告訴你的。如果不是用於偏移量,則該值將是不明確的。這個時間牆可能是那個時區的人可能會感到困惑的地方。

請注意,此方法有兩個過載,一個採用DateTime,另一個採用DateTimeOffset

  • DateTime一個非常有意義的時候.KindDateTimeKind.Unspecified

    DateTime dt = new DateTime(2016, 10, 30, 2, 0, 0); 
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 
    bool ambiguous = tz.IsAmbiguousTime(dt); // true 
    
  • 它與其他種類少一點意義,因爲它的轉換給定的時區第一 - 但仍做同樣的事情:

    DateTime dt = new DateTime(2016, 10, 30, 1, 0, 0, DateTimeKind.Utc); 
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 
    bool ambiguous = tz.IsAmbiguousTime(dt); // true 
    
  • DateTimeOffset過載基本上和前面的例子一樣。無論偏移是什麼,它都會應用到日期和時間,然後單獨檢查結果日期和時間的模糊性 - 就像第一個示例中一樣。

    DateTimeOffset dto = new DateTimeOffset(2016, 10, 30, 2, 0, 0, TimeSpan.FromHours(1)); 
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 
    bool ambiguous = tz.IsAmbiguousTime(dto); // true 
    
  • 即使偏移量爲毫無意義的那個時間段,它仍然被比較之前應用。

    DateTimeOffset dto = new DateTimeOffset(2016, 10, 29, 19, 0, 0, TimeSpan.FromHours(-5)); 
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 
    bool ambiguous = tz.IsAmbiguousTime(dto); // true 
    

它歸結到過載,這基本上是執行:

// Make sure the dto is adjusted to the tz. This could be a no-op if it already is. 
DateTimeOffset adjusted = TimeZoneInfo.ConvertTime(dto, tz); 

// Then just get the wall time, stripping away the offset. 
// The resulting datetime has unspecified kind. 
DateTime dt = adjusted.DateTime; 

// Finally, call the datetime version of the function 
bool ambiguous = tz.IsAmbiguousTime(dt); 

你可以看到這個in the .net reference source here。他們將它壓縮爲兩行,並且在DST不適用時使用捷徑來提高性能,但這就是它的作用。

+0

很好的回答!你對時間段的解釋和對時區值的無知有很大的幫助!我不知道框架只使用DateTimeOffset的DateTime部分。 –

+0

這只是關於* this *功能。還有很多其他地方確實考慮了偏移量。 –

3

請問這些文件說不清楚?

典型地,不明確的時間導致在時鐘被設定爲從夏令時

即返回到標準時間如果在凌晨2點你離開DST並將時鐘重置爲凌晨1點,那麼如果有人在凌晨1點30分左右開始講話,則不知道是從現在起30分鐘還是過去30分鐘發生。

有一組值(通常爲一小時長),它映射到UTC時間的兩組不同的時刻。

2

孔中的樣品是(在去年十月週日2:00-3:00

DateTimeOffset example = new DateTimeOffset(2015, 10, 25, 2, 30, 0, 
    new TimeSpan(0, 2, 0, 0)); 

TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 

if (tst.IsAmbiguousTime(example)) 
    Console.Write("Ambiguous time"); 

對面曖昧時間無效時間(去年3月的週日2:00- 3:00):

TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 

if (tst.IsInvalidTime(new DateTime(2016, 03, 27, 2, 30, 0))) 
    Console.Write("Invalid time");