Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
DateTimeOffset yapısı, DateTime yapısına göre daha fazla saat dilimi farkındalığı sağlasa da, DateTime parametreler yöntem çağrılarında daha yaygın olarak kullanılır. Bu yaklaşım nedeniyle, DateTimeOffset değerlerini DateTime değerlerine ve bunun tersine dönüştürebilme yeteneği önemlidir. Bu makalede, bu dönüştürmelerin mümkün olduğunca fazla saat dilimi bilgisini koruyacak şekilde nasıl gerçekleştirebileceğiniz gösterilmektedir.
Uyarı
Zaman dilimlerinde DateTime ve DateTimeOffset türlerinin saatleri temsil ederken bazı sınırlamaları vardır. Kind özelliğiyle yalnızca DateTime Eşgüdümlü Evrensel Saati (UTC) ve sistemin yerel saat dilimini yansıtabiliyor. DateTimeOffset UTC'den bir saatin uzaklığını yansıtır, ancak bu uzaklığı ait olduğu gerçek saat dilimini yansıtmaz. Saat değerleri ve saat dilimleri desteği hakkında daha fazla bilgi için bkz. DateTime, DateTimeOffset, TimeSpan ve TimeZoneInfo Arasında Seçim Yapma.
DateTime'dan DateTimeOffset'e dönüştürmeler
DateTimeOffset yapısı, çoğu dönüştürme işlemi için uygun olan DateTime'den DateTimeOffset'ye dönüştürme gerçekleştirmek amacıyla iki eşdeğer yol sağlar.
Yeni bir DateTime nesne oluşturmak için DateTimeOffset değerine dayanan DateTimeOffset oluşturucu.
DateTime değerini bir DateTimeOffset nesnesine atamanıza olanak tanıyan örtük dönüştürme işleci.
UTC ve yerel DateTime değerler için, Offset sonuçta elde edilen DateTimeOffset değerin özelliği UTC veya yerel saat dilimi uzaklığını doğru şekilde yansıtır. Örneğin, aşağıdaki kod UTC saatini eşdeğer DateTimeOffset değerine dönüştürür:
DateTime utcTime1 = new DateTime(2008, 6, 19, 7, 0, 0);
utcTime1 = DateTime.SpecifyKind(utcTime1, DateTimeKind.Utc);
DateTimeOffset utcTime2 = utcTime1;
Console.WriteLine($"Converted {utcTime1} {utcTime1.Kind} to a DateTimeOffset value of {utcTime2}");
// This example displays the following output to the console:
// Converted 6/19/2008 7:00:00 AM Utc to a DateTimeOffset value of 6/19/2008 7:00:00 AM +00:00
Dim utcTime1 As Date = Date.SpecifyKind(#06/19/2008 7:00AM#, _
DateTimeKind.Utc)
Dim utcTime2 As DateTimeOffset = utcTime1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
utcTime1, _
utcTime1.Kind.ToString(), _
utcTime2)
' This example displays the following output to the console:
' Converted 6/19/2008 7:00:00 AM Utc to a DateTimeOffset value of 6/19/2008 7:00:00 AM +00:00
Bu durumda değişkenin utcTime2 uzaklığı 00:00'dır. Benzer şekilde, aşağıdaki kod yerel saati eşdeğer DateTimeOffset değerine dönüştürür. Dönüştürme, ABD Pasifik Standart Saat diliminde çalıştırılır:
DateTime localTime1 = new DateTime(2008, 6, 19, 7, 0, 0);
localTime1 = DateTime.SpecifyKind(localTime1, DateTimeKind.Local);
DateTimeOffset localTime2 = localTime1;
Console.WriteLine($"Converted {localTime1} {localTime1.Kind} to a DateTimeOffset value of {localTime2}");
// This example displays the following output to the console:
// Converted 6/19/2008 7:00:00 AM Local to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
Dim localTime1 As Date = Date.SpecifyKind(#06/19/2008 7:00AM#, DateTimeKind.Local)
Dim localTime2 As DateTimeOffset = localTime1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
localTime1, _
localTime1.Kind.ToString(), _
localTime2)
' This example displays the following output to the console:
' Converted 6/19/2008 7:00:00 AM Local to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
Ancak, özelliği DateTimeolan Kind değerler için DateTimeKind.Unspecified bu iki dönüştürme yöntemi, uzaklığı yerel saat dilimi olan bir DateTimeOffset değer üretir. Dönüştürme, ABD Pasifik Standart Saat diliminde çalıştırılan aşağıdaki örnekte gösterilmiştir:
DateTime time1 = new DateTime(2008, 6, 19, 7, 0, 0); // Kind is DateTimeKind.Unspecified
DateTimeOffset time2 = time1;
Console.WriteLine($"Converted {time1} {time1.Kind} to a DateTimeOffset value of {time2}");
// This example displays the following output to the console:
// Converted 6/19/2008 7:00:00 AM Unspecified to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
Dim time1 As Date = #06/19/2008 7:00AM# ' Kind is DateTimeKind.Unspecified
Dim time2 As DateTimeOffset = time1
Console.WriteLine("Converted {0} {1} to a DateTimeOffset value of {2}", _
time1, _
time1.Kind.ToString(), _
time2)
' This example displays the following output to the console:
' Converted 6/19/2008 7:00:00 AM Unspecified to a DateTimeOffset value of 6/19/2008 7:00:00 AM -07:00
DateTime Değer tarih ve saati yerel saat dilimi veya UTC dışında gösteriyorsa, aşırı yüklenmiş DateTimeOffset yapıcısını çağırarak DateTimeOffset değerine dönüştürebilir ve saat dilimi bilgilerini koruyabilirsiniz. Örneğin, aşağıdaki örnek Merkezi Standart Saati yansıtan bir DateTimeOffset nesnenin örneğini oluşturur:
DateTime time1 = new DateTime(2008, 6, 19, 7, 0, 0); // Kind is DateTimeKind.Unspecified
try
{
DateTimeOffset time2 = new DateTimeOffset(time1,
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(time1));
Console.WriteLine($"Converted {time1} {time1.Kind} to a DateTime value of {time2}");
}
// Handle exception if time zone is not defined in registry
catch (TimeZoneNotFoundException)
{
Console.WriteLine("Unable to identify target time zone for conversion.");
}
// This example displays the following output to the console:
// Converted 6/19/2008 7:00:00 AM Unspecified to a DateTime value of 6/19/2008 7:00:00 AM -05:00
Dim time1 As Date = #06/19/2008 7:00AM# ' Kind is DateTimeKind.Unspecified
Try
Dim time2 As New DateTimeOffset(time1, _
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(time1))
Console.WriteLine("Converted {0} {1} to a DateTime value of {2}", _
time1, _
time1.Kind.ToString(), _
time2)
' Handle exception if time zone is not defined in registry
Catch e As TimeZoneNotFoundException
Console.WriteLine("Unable to identify target time zone for conversion.")
End Try
' This example displays the following output to the console:
' Converted 6/19/2008 7:00:00 AM Unspecified to a DateTime value of 6/19/2008 7:00:00 AM -05:00
Bu oluşturucu aşırı yüklemesinin ikinci parametresi, utc'den saat uzaklığını temsil eden bir TimeSpan nesnedir. Zamanın ilgili saat diliminin TimeZoneInfo.GetUtcOffset(DateTime) yöntemini çağırarak elde edin. Yöntemin tek parametresi, dönüştürülecek tarih ve saati temsil eden değerdir DateTime . Saat dilimi gün ışığından yararlanma saatini destekliyorsa, bu parametre yöntemin ilgili tarih ve saat için uygun uzaklığı belirlemesine olanak tanır.
DateTimeOffset'ten DateTime'a dönüştürmeler
En yaygın kullanım amacı, DateTimeOffset ile DateTime arasında dönüştürme yapmak için DateTime özelliğidir. Ancak, aşağıdaki örnekte gösterildiği gibi özelliği DateTimeolan Kind bir Unspecified değer döndürür:
DateTime baseTime = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset sourceTime;
DateTime targetTime;
// Convert UTC to DateTime value
sourceTime = new DateTimeOffset(baseTime, TimeSpan.Zero);
targetTime = sourceTime.DateTime;
Console.WriteLine($"{sourceTime} converts to {targetTime} {targetTime.Kind}");
// Convert local time to DateTime value
sourceTime = new DateTimeOffset(baseTime,
TimeZoneInfo.Local.GetUtcOffset(baseTime));
targetTime = sourceTime.DateTime;
Console.WriteLine($"{sourceTime} converts to {targetTime} {targetTime.Kind}");
// Convert Central Standard Time to a DateTime value
try
{
TimeSpan offset = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(baseTime);
sourceTime = new DateTimeOffset(baseTime, offset);
targetTime = sourceTime.DateTime;
Console.WriteLine($"{sourceTime} converts to {targetTime} {targetTime.Kind}");
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("Unable to create DateTimeOffset based on U.S. Central Standard Time.");
}
// This example displays the following output to the console:
// 6/19/2008 7:00:00 AM +00:00 converts to 6/19/2008 7:00:00 AM Unspecified
// 6/19/2008 7:00:00 AM -07:00 converts to 6/19/2008 7:00:00 AM Unspecified
// 6/19/2008 7:00:00 AM -05:00 converts to 6/19/2008 7:00:00 AM Unspecified
Const baseTime As Date = #06/19/2008 7:00AM#
Dim sourceTime As DateTimeOffset
Dim targetTime As Date
' Convert UTC to DateTime value
sourceTime = New DateTimeOffset(baseTime, TimeSpan.Zero)
targetTime = sourceTime.DateTime
Console.WriteLine("{0} converts to {1} {2}", _
sourceTime, _
targetTime, _
targetTime.Kind.ToString())
' Convert local time to DateTime value
sourceTime = New DateTimeOffset(baseTime, _
TimeZoneInfo.Local.GetUtcOffset(baseTime))
targetTime = sourceTime.DateTime
Console.WriteLine("{0} converts to {1} {2}", _
sourceTime, _
targetTime, _
targetTime.Kind.ToString())
' Convert Central Standard Time to a DateTime value
Try
Dim offset As TimeSpan = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(baseTime)
sourceTime = New DateTimeOffset(baseTime, offset)
targetTime = sourceTime.DateTime
Console.WriteLine("{0} converts to {1} {2}", _
sourceTime, _
targetTime, _
targetTime.Kind.ToString())
Catch e As TimeZoneNotFoundException
Console.WriteLine("Unable to create DateTimeOffset based on U.S. Central Standard Time.")
End Try
' This example displays the following output to the console:
' 6/19/2008 7:00:00 AM +00:00 converts to 6/19/2008 7:00:00 AM Unspecified
' 6/19/2008 7:00:00 AM -07:00 converts to 6/19/2008 7:00:00 AM Unspecified
' 6/19/2008 7:00:00 AM -05:00 converts to 6/19/2008 7:00:00 AM Unspecified
Yukarıdaki örnek, DateTimeOffset değerin UTC ile ilişkisi hakkındaki tüm bilgilerin DateTime özelliği kullanıldığında dönüştürme tarafından kaybedildiğini göstermektedir. Yapısı özelliğinde yalnızca bu iki saat dilimini yansıttığı için bu davranış UTC saatine veya sistemin yerel saatine DateTimeOffsetDateTime karşılık gelen değerleri de etkilerKind.
DateTimeOffset değerini bir DateTime değerine dönüştürürken mümkün olduğunca çok saat dilimi bilgisini korumak için DateTimeOffset.UtcDateTime ve DateTimeOffset.LocalDateTime özelliklerini kullanabilirsiniz.
UTC saatini dönüştürme
Dönüştürülen DateTime değerin UTC saati olduğunu belirtmek için özelliğinin DateTimeOffset.UtcDateTime değerini alabilirsiniz. DateTime özelliğinden iki şekilde farklıdır:
Offset Özellik değeri eşit TimeSpan.Zerodeğilse, saati UTC'ye dönüştürür.
Uyarı
Uygulamanız, dönüştürülen DateTime değerlerin zaman içinde tek bir noktayı kesin olarak tanımlamasını gerektiriyorsa, DateTimeOffset'yi DateTime'a dönüştürmelerin tümünü DateTimeOffset.UtcDateTime özelliğini kullanarak işlemek için düşünmelisiniz.
Aşağıdaki kod, offseti TimeSpan.Zero ile eşit olan bir DateTimeOffset değerini DateTime değerine dönüştürmek için UtcDateTime özelliğini kullanır.
DateTimeOffset utcTime1 = new DateTimeOffset(2008, 6, 19, 7, 0, 0, TimeSpan.Zero);
DateTime utcTime2 = utcTime1.UtcDateTime;
Console.WriteLine($"{utcTime1} converted to {utcTime2} {utcTime2.Kind}");
// The example displays the following output to the console:
// 6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
Dim utcTime1 As New DateTimeOffset(#06/19/2008 7:00AM#, TimeSpan.Zero)
Dim utcTime2 As Date = utcTime1.UtcDateTime
Console.WriteLine("{0} converted to {1} {2}", _
utcTime1, _
utcTime2, _
utcTime2.Kind.ToString())
' The example displays the following output to the console:
' 6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
Aşağıdaki kod, bir UtcDateTime değeri üzerinde hem saat dilimi dönüştürme hem de tür dönüştürme gerçekleştirmek için DateTimeOffset özelliğini kullanır:
DateTimeOffset originalTime = new DateTimeOffset(2008, 6, 19, 7, 0, 0, new TimeSpan(5, 0, 0));
DateTime utcTime = originalTime.UtcDateTime;
Console.WriteLine($"{originalTime} converted to {utcTime} {utcTime.Kind}");
// The example displays the following output to the console:
// 6/19/2008 7:00:00 AM +05:00 converted to 6/19/2008 2:00:00 AM Utc
Dim originalTime As New DateTimeOffset(#6/19/2008 7:00AM#, _
New TimeSpan(5, 0, 0))
Dim utcTime As Date = originalTime.UtcDateTime
Console.WriteLine("{0} converted to {1} {2}", _
originalTime, _
utcTime, _
utcTime.Kind.ToString())
' The example displays the following output to the console:
' 6/19/2008 7:00:00 AM +05:00 converted to 6/19/2008 2:00:00 AM Utc
Yerel saati dönüştürme
DateTimeOffset değerinin yerel saati temsil ettiğini belirtmek için, DateTime özelliği tarafından döndürülen DateTimeOffset.DateTime değerini static (Visual Basic'de Shared) SpecifyKind yöntemine geçirebilirsiniz. yöntemi, ona ilk parametresi olarak geçirilen tarih ve saati döndürür, ancak özelliğini ikinci parametresi tarafından belirtilen değere ayarlar Kind . Yerel saat dilimine karşılık gelen uzaklığa sahip bir DateTimeOffset değerini dönüştürürken, aşağıdaki kod SpecifyKind yöntemini kullanır.
DateTime sourceDate = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset utcTime1 = new DateTimeOffset(sourceDate,
TimeZoneInfo.Local.GetUtcOffset(sourceDate));
DateTime utcTime2 = utcTime1.DateTime;
if (utcTime1.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(utcTime1.DateTime)))
utcTime2 = DateTime.SpecifyKind(utcTime2, DateTimeKind.Local);
Console.WriteLine($"{utcTime1} converted to {utcTime2} {utcTime2.Kind}");
// The example displays the following output to the console:
// 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
Dim sourceDate As Date = #06/19/2008 7:00AM#
Dim utcTime1 As New DateTimeOffset(sourceDate, _
TimeZoneInfo.Local.GetUtcOffset(sourceDate))
Dim utcTime2 As Date = utcTime1.DateTime
If utcTime1.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(utcTime1.DateTime)) Then
utcTime2 = DateTime.SpecifyKind(utcTime2, DateTimeKind.Local)
End If
Console.WriteLine("{0} converted to {1} {2}", _
utcTime1, _
utcTime2, _
utcTime2.Kind.ToString())
' The example displays the following output to the console:
' 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
DateTimeOffset.LocalDateTime özelliğini, bir DateTimeOffset değerini yerel DateTime değerine dönüştürmek için de kullanabilirsiniz. Döndürülen DateTime değerinin Kind özelliği Local. Aşağıdaki kod, ofseti yerel saat dilimine karşılık gelen bir DateTimeOffset değerini dönüştürürken DateTimeOffset.LocalDateTime özelliğini kullanır:
DateTime sourceDate = new DateTime(2008, 6, 19, 7, 0, 0);
DateTimeOffset localTime1 = new DateTimeOffset(sourceDate,
TimeZoneInfo.Local.GetUtcOffset(sourceDate));
DateTime localTime2 = localTime1.LocalDateTime;
Console.WriteLine($"{localTime1} converted to {localTime2} {localTime2.Kind}");
// The example displays the following output to the console:
// 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
Dim sourceDate As Date = #06/19/2008 7:00AM#
Dim localTime1 As New DateTimeOffset(sourceDate, _
TimeZoneInfo.Local.GetUtcOffset(sourceDate))
Dim localTime2 As Date = localTime1.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
localTime1, _
localTime2, _
localTime2.Kind.ToString())
' The example displays the following output to the console:
' 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
özelliğini kullanarak DateTime bir DateTimeOffset.LocalDateTime değer aldığınızda, özelliğin get erişimcisi önce değeri UTC'ye dönüştürürDateTimeOffset, ardından yöntemini çağırarak ToLocalTime yerel saate dönüştürür. Bu davranış, bir saat dilimi dönüştürmesiyle birlikte bir tür dönüştürmesi gerçekleştirirken DateTimeOffset.LocalDateTime özelliğinden bir değer alabileceğiniz anlamına gelir. Ayrıca, dönüştürmeyi gerçekleştirirken yerel saat diliminin ayarlama kurallarının uygulandığı anlamına gelir. Aşağıdaki kod, hem tür hem de saat dilimi dönüştürme gerçekleştirmek için özelliğinin DateTimeOffset.LocalDateTime kullanımını gösterir. Örnek çıkış, Pasifik Saat Dilimi (ABD ve Kanada) olarak ayarlanmış bir makineye yöneliktir. Kasım tarihi Utc-8 olan Pasifik Standart Saati, Haziran tarihi ise UTC-7 olan Yaz Saati'dir.
DateTimeOffset originalDate;
DateTime localDate;
// Convert time originating in a different time zone
originalDate = new DateTimeOffset(2008, 6, 18, 7, 0, 0,
new TimeSpan(-5, 0, 0));
localDate = originalDate.LocalDateTime;
Console.WriteLine($"{originalDate} converted to {localDate} {localDate.Kind}");
// Convert time originating in a different time zone
// so local time zone's adjustment rules are applied
originalDate = new DateTimeOffset(2007, 11, 4, 4, 0, 0,
new TimeSpan(-5, 0, 0));
localDate = originalDate.LocalDateTime;
Console.WriteLine($"{originalDate} converted to {localDate} {localDate.Kind}");
// The example displays the following output to the console,
// when you run it on a machine that is set to Pacific Time (US & Canada):
// 6/18/2008 7:00:00 AM -05:00 converted to 6/18/2008 5:00:00 AM Local
// 11/4/2007 4:00:00 AM -05:00 converted to 11/4/2007 1:00:00 AM Local
Dim originalDate As DateTimeOffset
Dim localDate As Date
' Convert time originating in a different time zone
originalDate = New DateTimeOffset(#06/19/2008 7:00AM#, _
New TimeSpan(-5, 0, 0))
localDate = originalDate.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
originalDate, _
localDate, _
localDate.Kind.ToString())
' Convert time originating in a different time zone
' so local time zone's adjustment rules are applied
originalDate = New DateTimeOffset(#11/04/2007 4:00AM#, _
New TimeSpan(-5, 0, 0))
localDate = originalDate.LocalDateTime
Console.WriteLine("{0} converted to {1} {2}", _
originalDate, _
localDate, _
localDate.Kind.ToString())
' The example displays the following output to the console,
' when you run it on a machine that is set to Pacific Time (US & Canada):
' 6/18/2008 7:00:00 AM -05:00 converted to 6/18/2008 5:00:00 AM Local
' 11/4/2007 4:00:00 AM -05:00 converted to 11/4/2007 1:00:00 AM Local
Genel amaçlı dönüştürme yöntemi
Aşağıdaki örnek, DateTimeOffset değerlerini DateTime değerlerine dönüştüren ConvertFromDateTimeOffset adlı bir yöntemi tanımlar. Uzaklığı temelinde değerin DateTimeOffset UTC saati mi, yerel saat mi yoksa başka bir saat mi olduğunu belirler ve döndürülen tarih ve saat değerinin Kind özelliğini buna göre tanımlar.
static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)
{
if (dateTime.Offset.Equals(TimeSpan.Zero))
return dateTime.UtcDateTime;
else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
else
return dateTime.DateTime;
}
Function ConvertFromDateTimeOffset(dateTime As DateTimeOffset) As Date
If dateTime.Offset.Equals(TimeSpan.Zero) Then
Return dateTime.UtcDateTime
ElseIf dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime))
Return Date.SpecifyKind(dateTime.DateTime, DateTimeKind.Local)
Else
Return dateTime.DateTime
End If
End Function
Aşağıdaki örnek, UTC saatini, yerel saati ve ABD Orta Standart Saat dilimindeki bir saati temsil eden değerleri dönüştürmek için ConvertFromDateTimeOffset yöntemini çağırırDateTimeOffset.
DateTime timeComponent = new DateTime(2008, 6, 19, 7, 0, 0);
DateTime returnedDate;
// Convert UTC time
DateTimeOffset utcTime = new DateTimeOffset(timeComponent, TimeSpan.Zero);
returnedDate = ConvertFromDateTimeOffset(utcTime);
Console.WriteLine($"{utcTime} converted to {returnedDate} {returnedDate.Kind}");
// Convert local time
DateTimeOffset localTime = new DateTimeOffset(timeComponent,
TimeZoneInfo.Local.GetUtcOffset(timeComponent));
returnedDate = ConvertFromDateTimeOffset(localTime);
Console.WriteLine($"{localTime} converted to {returnedDate} {returnedDate.Kind}");
// Convert Central Standard Time
DateTimeOffset cstTime = new DateTimeOffset(timeComponent,
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(timeComponent));
returnedDate = ConvertFromDateTimeOffset(cstTime);
Console.WriteLine($"{cstTime} converted to {returnedDate} {returnedDate.Kind}");
// The example displays the following output to the console:
// 6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
// 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
// 6/19/2008 7:00:00 AM -05:00 converted to 6/19/2008 7:00:00 AM Unspecified
Dim timeComponent As Date = #06/19/2008 7:00AM#
Dim returnedDate As Date
' Convert UTC time
Dim utcTime As New DateTimeOffset(timeComponent, TimeSpan.Zero)
returnedDate = ConvertFromDateTimeOffset(utcTime)
Console.WriteLine("{0} converted to {1} {2}", _
utcTime, _
returnedDate, _
returnedDate.Kind.ToString())
' Convert local time
Dim localTime As New DateTimeOffset(timeComponent, _
TimeZoneInfo.Local.GetUtcOffset(timeComponent))
returnedDate = ConvertFromDateTimeOffset(localTime)
Console.WriteLine("{0} converted to {1} {2}", _
localTime, _
returnedDate, _
returnedDate.Kind.ToString())
' Convert Central Standard Time
Dim cstTime As New DateTimeOffset(timeComponent, _
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(timeComponent))
returnedDate = ConvertFromDateTimeOffset(cstTime)
Console.WriteLine("{0} converted to {1} {2}", _
cstTime, _
returnedDate, _
returnedDate.Kind.ToString())
' The example displays the following output to the console:
' 6/19/2008 7:00:00 AM +00:00 converted to 6/19/2008 7:00:00 AM Utc
' 6/19/2008 7:00:00 AM -07:00 converted to 6/19/2008 7:00:00 AM Local
' 6/19/2008 7:00:00 AM -05:00 converted to 6/19/2008 7:00:00 AM Unspecified
Uyarı
Kod, uygulamaya ve tarih ile saat değerlerinin kaynağına bağlı olarak her zaman geçerli olmayabilecek aşağıdaki iki varsayımda bulunur:
TimeSpan.Zero öğesinin zaman farkı olan bir tarih ve saat değerinin UTC’yi temsil ettiğini varsayar. Aslında UTC, belirli bir saat dilimindeki bir saat değildir, ancak dünya saat dilimlerindeki saatlerin standartlaştırıldığı saattir. Saat dilimlerinin uzaklığı Zeroda olabilir.
Uzaklığı yerel saat dilimine eşit olan bir tarih ve saatin yerel saat dilimini temsil ettiğini varsayar. Tarih ve saat değerleri özgün saat dilimiyle ilişkilendirilmediğinden bu durum geçerli olmayabilir; tarih ve saat, aynı uzaklıkta başka bir saat diliminde kaynaklanmış olabilir.