TimeZoneInfo.FromSerializedString(String) 方法
定义
反序列化一个字符串,以重新创建原始的已序列化的 TimeZoneInfo 对象。Deserializes a string to re-create an original serialized TimeZoneInfo object.
public:
static TimeZoneInfo ^ FromSerializedString(System::String ^ source);
public static TimeZoneInfo FromSerializedString (string source);
static member FromSerializedString : string -> TimeZoneInfo
Public Shared Function FromSerializedString (source As String) As TimeZoneInfo
参数
- source
- String
已序列化的 TimeZoneInfo 对象的字符串表示形式。The string representation of the serialized TimeZoneInfo object.
返回
原始序列化对象。The original serialized object.
例外
source 参数是一个 null 字符串。The source parameter is a null string.
源参数不能反序列化回 TimeZoneInfo 对象。The source parameter cannot be deserialized back into a TimeZoneInfo object.
示例
下面的示例尝试从本地系统检索南极洲/南极点时区。The following example tries to retrieve the Antarctica/South Pole time zone from the local system. 如果失败,代码会尝试从应用程序目录中的文本文件检索有关时区的信息。If it fails, the code tries to retrieve information about the time zone from a text file in the application directory. 如果此尝试失败,代码会创建时区,并将有关时区的信息写入文本文件。If this attempt fails, the code creates the time zone and writes information about the time zone to the text file.
private TimeZoneInfo InitializeTimeZone()
{
TimeZoneInfo southPole = null;
// Determine if South Pole time zone is defined in system
try
{
southPole = TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
}
// Time zone does not exist; create it, store it in a text file, and return it
catch
{
const string filename = @".\TimeZoneInfo.txt";
bool found = false;
if (File.Exists(filename))
{
StreamReader reader = new StreamReader(filename);
string timeZoneInfo;
while (reader.Peek() >= 0)
{
timeZoneInfo = reader.ReadLine();
if (timeZoneInfo.Contains("Antarctica/South Pole"))
{
southPole = TimeZoneInfo.FromSerializedString(timeZoneInfo);
reader.Close();
found = true;
break;
}
}
}
if (! found)
{
// Define transition times to/from DST
TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 1, DayOfWeek.Sunday);
TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 3, DayOfWeek.Sunday);
// Define adjustment rule
TimeSpan delta = new TimeSpan(1, 0, 0);
TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1989, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
// Create array for adjustment rules
TimeZoneInfo.AdjustmentRule[] adjustments = {adjustment};
// Define other custom time zone arguments
string displayName = "(GMT+12:00) Antarctica/South Pole";
string standardName = "Antarctica/South Pole Standard Time";
string daylightName = "Antarctica/South Pole Daylight Time";
TimeSpan offset = new TimeSpan(12, 0, 0);
southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
// Write time zone to the file
StreamWriter writer = new StreamWriter(filename, true);
writer.WriteLine(southPole.ToSerializedString());
writer.Close();
}
}
return southPole;
}
Private Function InitializeTimeZone() As TimeZoneInfo
Dim southPole As TimeZoneInfo = Nothing
' Determine if South Pole time zone is defined in system
Try
southPole = TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time")
' Time zone does not exist; create it, store it in a text file, and return it
Catch e As TimeZoneNotFoundException
Dim found As Boolean
Const filename As String = ".\TimeZoneInfo.txt"
If File.Exists(filename) Then
Dim reader As StreamReader = New StreamReader(fileName)
Dim timeZoneString As String
Do While reader.Peek() >= 0
timeZoneString = reader.ReadLine()
If timeZoneString.Contains("Antarctica/South Pole") Then
southPole = TimeZoneInfo.FromSerializedString(timeZoneString)
reader.Close()
found = True
Exit Do
End If
Loop
End If
If Not found Then
' Define transition times to/from DST
Dim startTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00#, 10, 1, DayOfWeek.Sunday)
Dim endTransition As TimeZoneInfo.TransitionTime = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#02:00:00#, 3, 3, DayOfWeek.Sunday)
' Define adjustment rule
Dim delta As TimeSpan = New TimeSpan(1, 0, 0)
Dim adjustment As TimeZoneInfo.AdjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#10/01/1989#, Date.MaxValue.Date, delta, startTransition, endTransition)
' Create array for adjustment rules
Dim adjustments() As TimeZoneInfo.AdjustmentRule = {adjustment}
' Define other custom time zone arguments
Dim displayName As String = "(GMT+12:00) Antarctica/South Pole"
Dim standardName As String = "Antarctica/South Pole Standard Time"
Dim daylightName As String = "Antarctica/South Pole Daylight Time"
Dim offset As TimeSpan = New TimeSpan(12, 0, 0)
southPole = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments)
' Write time zone to the file
Dim writer As StreamWriter = New StreamWriter(filename, True)
writer.WriteLine(southPole.ToSerializedString())
writer.Close()
End If
End Try
Return southPole
End Function
注解
有一种替代方法是提供创建在 Windows 上的注册表中找不到或在 Linux 和 macOS 上的 ICU 库 中找不到的时区所需的所有代码。There is an alternative to providing all the code required to create a time zone that is not found in the registry on Windows or in the ICU Library on Linux and macOS. 你可以定义自定义时区,并使用 ToSerializedString 独立可执行文件中的方法,或使用应用程序的安装程序将时区保存为字符串。You can define a custom time zone and either use the ToSerializedString method in a standalone executable or use an application's setup program to save the time zone as a string. 然后,应用程序可以从其存储位置检索此字符串,然后使用方法对其进行实例化 FromSerializedString 。The application can then retrieve this string from its storage location and instantiate it using the FromSerializedString method.