Upraviť

Zdieľať cez


TimeZoneInfo.ClearCachedData Method

Definition

Clears cached time zone data.

public:
 static void ClearCachedData();
public static void ClearCachedData ();
static member ClearCachedData : unit -> unit
Public Shared Sub ClearCachedData ()

Remarks

Cached time zone data includes data on the local time zone and the Coordinated Universal Time (UTC) zone.

You might call the ClearCachedData method to reduce the memory devoted to the application's cache of time zone information or to reflect the fact that the local system's time zone has changed.

Storing references to the local and UTC time zones is not recommended. After the call to the ClearCachedData method, these object variables will be undefined TimeZoneInfo objects that are no longer references to TimeZoneInfo.Local or TimeZoneInfo.Utc. For example, in the following code, the second call to the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method throws an ArgumentException because the local variable is no longer considered equal to TimeZoneInfo.Local.

TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
TimeZoneInfo local = TimeZoneInfo.Local;
Console.WriteLine(TimeZoneInfo.ConvertTime(DateTime.Now, local, cst));

TimeZoneInfo.ClearCachedData();
try
{
   Console.WriteLine(TimeZoneInfo.ConvertTime(DateTime.Now, local, cst));
}
catch (ArgumentException e)
{
   Console.WriteLine(e.GetType().Name + "\n   " + e.Message);
}
open System

let cst = TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time"
let local = TimeZoneInfo.Local
printfn $"{TimeZoneInfo.ConvertTime(DateTime.Now, local, cst)}"

TimeZoneInfo.ClearCachedData()
try
   printfn $"{TimeZoneInfo.ConvertTime(DateTime.Now, local, cst)}"
with :? ArgumentException as e ->
   printfn $"{e.GetType().Name}\n   {e.Message}"
Dim cst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
Dim local As TimeZoneInfo = TimeZoneInfo.Local
Console.WriteLine(TimeZoneInfo.ConvertTime(Date.Now, local, cst))

TimeZoneInfo.ClearCachedData()
Try
   Console.WriteLine(TimeZoneInfo.ConvertTime(Date.Now, local, cst))
Catch e As ArgumentException
   Console.WriteLine(e.GetType().Name & vbCrLf & "   " & e.Message)
End Try

Applies to