Megosztás a következőn keresztül:


Útmutató: Az előre definiált UTC és helyi időzóna-objektumok elérése

Az TimeZoneInfo osztály két tulajdonságot biztosít, Utc és Locala kód hozzáférést biztosít az előre definiált időzóna-objektumokhoz. Ez a témakör azt ismerteti, hogyan érheti el az TimeZoneInfo adott tulajdonságok által visszaadott objektumokat.

Az egyezményes világidő (UTC) TimeZoneInfo objektum elérése

  1. Használja a static (Shared Visual Basic) TimeZoneInfo.Utc tulajdonságot a koordinált egyetemes idő eléréséhez.

  2. Ahelyett, hogy a TimeZoneInfo tulajdonság által visszaadott objektumot egy objektumváltozóhoz rendeli, a tulajdonságon keresztül továbbra is hozzáférhet a TimeZoneInfo.Utc koordinált univerzális időhöz.

A helyi időzóna elérése

  1. static A (SharedVisual Basic) TimeZoneInfo.Local tulajdonság használatával érheti el a helyi rendszer időzónáját.

  2. A tulajdonság által visszaadott objektum objektumváltozóhoz való TimeZoneInfo hozzárendelése helyett továbbra is a tulajdonságon keresztül érheti el a helyi időzónát TimeZoneInfo.Local .

Példa

Az alábbi kód az TimeZoneInfo.Local amerikai és TimeZoneInfo.Utc a kanadai keleti standard időzónából való idő konvertálására, valamint az időzóna nevének a konzolra való megjelenítésére szolgál.

// Create Eastern Standard Time value and TimeZoneInfo object
DateTime estTime = new DateTime(2007, 1, 1, 00, 00, 00);
string timeZoneName = "Eastern Standard Time";
try
{
    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);

    // Convert EST to local time
    DateTime localTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local);
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.",
            estTime,
            est,
            localTime,
            TimeZoneInfo.Local.IsDaylightSavingTime(localTime) ?
                      TimeZoneInfo.Local.DaylightName :
                      TimeZoneInfo.Local.StandardName);

    // Convert EST to UTC
    DateTime utcTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc);
    Console.WriteLine("At {0} {1}, the time is {2} {3}.",
            estTime,
            est,
            utcTime,
            TimeZoneInfo.Utc.StandardName);
}
catch (TimeZoneNotFoundException)
{
    Console.WriteLine("The {timeZoneName} zone cannot be found in the registry.");
}
catch (InvalidTimeZoneException)
{
    Console.WriteLine("The registry contains invalid data for the {timeZoneName} zone.");
}

// The example produces the following output to the console:
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the local time is 1/1/2007 12:00:00 AM Eastern Standard Time.
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the time is 1/1/2007 5:00:00 AM UTC.

' Create Eastern Standard Time value and TimeZoneInfo object      
Dim estTime As Date = #01/01/2007 00:00:00#
Dim timeZoneName As String = "Eastern Standard Time"
Try
    Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)

    ' Convert EST to local time
    Dim localTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local)
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.", _
            estTime, _
            est, _
            localTime, _
            IIf(TimeZoneInfo.Local.IsDaylightSavingTime(localTime), _
                TimeZoneInfo.Local.DaylightName, _
                TimeZoneInfo.Local.StandardName))

    ' Convert EST to UTC
    Dim utcTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc)
    Console.WriteLine("At {0} {1}, the time is {2} {3}.", _
            estTime, _
            est, _
            utcTime, _
            TimeZoneInfo.Utc.StandardName)
Catch e As TimeZoneNotFoundException
    Console.WriteLine("The {0} zone cannot be found in the registry.", _
                      timeZoneName)
Catch e As InvalidTimeZoneException
    Console.WriteLine("The registry contains invalid data for the {0} zone.", _
                      timeZoneName)
End Try

A helyi időzónát mindig a tulajdonságon keresztül kell elérnie ahelyett TimeZoneInfo.Local , hogy a helyi időzónát egy TimeZoneInfo objektumváltozóhoz rendeli. Hasonlóképpen, az UTC-zóna TimeZoneInfo objektumváltozóhoz rendelése helyett mindig a tulajdonságon keresztül kell elérnie az TimeZoneInfo.Utc egyezményes világidőt. Ez megakadályozza, hogy az TimeZoneInfo objektumváltozót a metódus hívása érvényteleníti TimeZoneInfo.ClearCachedData .

Lásd még