Csharp/C Sharp/Development Class/TimeZoneInfo
Содержание
- 1 The Id property corresponds to the value passed to FindSystemTimeZoneById.
- 2 The TimeZoneInfo class works in a similar manner. TimeZoneInfo.Local returns the current local time zone:
- 3 TimeZoneInfo also provides IsDaylightSavingTime and GetUtcOffset methods--the difference is that they accept either a DateTime or DateTimeOffset.
- 4 Week refers to the week of the month, with "5" meaning the last week.
The Id property corresponds to the value passed to FindSystemTimeZoneById.
using System;
public class MainClass {
public static void Main() {
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
Console.WriteLine(z.Id);
}
}
The TimeZoneInfo class works in a similar manner. TimeZoneInfo.Local returns the current local time zone:
using System;
public class MainClass {
public static void Main() {
TimeZoneInfo zone = TimeZoneInfo.Local;
Console.WriteLine(zone.StandardName);
Console.WriteLine(zone.DaylightName);
}
}
TimeZoneInfo also provides IsDaylightSavingTime and GetUtcOffset methods--the difference is that they accept either a DateTime or DateTimeOffset.
using System;
public class MainClass {
public static void Main() {
TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");
Console.WriteLine(wa.Id);
Console.WriteLine(wa.DisplayName);
Console.WriteLine(wa.BaseUtcOffset);
Console.WriteLine(wa.SupportsDaylightSavingTime);
}
}
Week refers to the week of the month, with "5" meaning the last week.
using System;
using System.Globalization;
public class MainClass {
public static void Main() {
TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");
foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules()) {
Console.WriteLine("Rule: applies from " + rule.DateStart +
" to " + rule.DateEnd);
Console.WriteLine(" Delta: " + rule.DaylightDelta);
Console.WriteLine(" Start: " + FormatTransitionTime
(rule.DaylightTransitionStart, false));
Console.WriteLine(" End: " + FormatTransitionTime
(rule.DaylightTransitionEnd, true));
Console.WriteLine();
}
}
static string FormatTransitionTime(TimeZoneInfo.TransitionTime tt,
bool endTime) {
if (endTime && tt.IsFixedDateRule
&& tt.Day == 1 && tt.Month == 1
&& tt.TimeOfDay == DateTime.MinValue)
return "-";
string s;
if (tt.IsFixedDateRule)
s = tt.Day.ToString();
else
s = "The first second third fourth last".Split()[tt.Week - 1] +
" " + tt.DayOfWeek + " in";
return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames[tt.Month - 1]
+ " at " + tt.TimeOfDay.TimeOfDay;
}
}