Konwertuj UTC DateTime na DateTimeOffset
Muszę się przekonaćUTC
ciągi dat doDateTimeOffsets
.
Musi działać z strefą czasową, która różni się od strefy czasowej komputerów. Na przykład. obecna strefa czasowa komputera to +02: 00, ale chcę utworzyć DateTimeOffset z przesunięciem -4: 00.
Czytałem już wiele pytań na temat stackoverflow, ale żaden z nich nie rozwiązał mojego problemu.
Właśnie to muszę zrobić:
Wkład: „2012-11-20T00: 00: 00Z”
Wydajność: DateTimeOffset z:
UtcDateTime of2012-11-20 00:00poprawne przesunięcie Utc dla zdefiniowanej strefy czasowej (w tym przykładzie 01:00)LocalDateTime:2012-11-20 01:00 (= UtcDateTime + Offset)Oczywiście należy wziąć pod uwagę czas letni.
edytować: Aby wszystko było jeszcze bardziej zrozumiałe, spróbuj wypełnić następujący fragment kodu:
DateTimeOffset result;
const string dateString = "2012-11-20T00:00:00Z";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date
//do conversion here
Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0)); //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.LocalDateTime, new DateTime(2012, 11, 20, 1, 0, 0));