|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Comment : instancier un objet TimeZoneInfo
Pour instancier un objet TimeZoneInfo
-
Déclarez un objet TimeZoneInfo. -
Appelez la méthode static (Shared dans Visual Basic) TimeZoneInfo.FindSystemTimeZoneById. -
Gérez toutes les exceptions levées par la méthode, en particulier TimeZoneNotFoundException qui se produit si le fuseau horaire n'est pas défini dans le Registre.
DateTime timeNow = DateTime.Now; try { TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); DateTime easternTimeNow = TimeZoneInfo.ConvertTime(timeNow, TimeZoneInfo.Local, easternZone); Console.WriteLine("{0} {1} corresponds to {2} {3}.", timeNow, TimeZoneInfo.Local.IsDaylightSavingTime(timeNow) ? TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, easternTimeNow, easternZone.IsDaylightSavingTime(easternTimeNow) ? easternZone.DaylightName : easternZone.StandardName); } // Handle exception // // As an alternative to simply displaying an error message, an alternate Eastern // Standard Time TimeZoneInfo object could be instantiated here either by restoring // it from a serialized string or by providing the necessary data to the // CreateCustomTimeZone method. catch (TimeZoneNotFoundException) { Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system."); } catch (InvalidTimeZoneException) { Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data."); } catch (SecurityException) { Console.WriteLine("The application lacks permission to read time zone information from the registry."); } catch (OutOfMemoryException) { Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone."); } // If we weren't passing FindSystemTimeZoneById a literal string, we also // would handle an ArgumentNullException.
-
qu'une référence à System.Core.dll soit ajoutée au projet ; -
que l'espace de noms System soit importé avec l'instruction using (requise en code C#).