TimeZoneInfo.HasSameRules(TimeZoneInfo) 方法

定义

指示当前对象和另一个 TimeZoneInfo 对象是否具有相同的调整规则。

public:
 bool HasSameRules(TimeZoneInfo ^ other);
public bool HasSameRules (TimeZoneInfo other);
member this.HasSameRules : TimeZoneInfo -> bool
Public Function HasSameRules (other As TimeZoneInfo) As Boolean

参数

other
TimeZoneInfo

要与当前的 TimeZoneInfo 对象进行比较的第二个对象。

返回

如果两个时区具有相同的调整规则和相同的基本偏移量,则为 true;否则为 false

例外

other 参数为 null

示例

通常,在 Windows 上的注册表中定义的多个时区以及 Linux 和 macOS 上的 ICU 库 与协调世界时 (UTC) 具有相同的调整规则。 以下示例向控制台显示这些时区的列表。

ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
TimeZoneInfo[] timeZoneArray = new TimeZoneInfo[timeZones.Count];
timeZones.CopyTo(timeZoneArray, 0);
// Iterate array from top to bottom
for (int ctr = timeZoneArray.GetUpperBound(0); ctr >= 1; ctr--)
{
   // Get next item from top
   TimeZoneInfo thisTimeZone = timeZoneArray[ctr];
   for (int compareCtr = 0; compareCtr <= ctr - 1; compareCtr++)
   {
      // Determine if time zones have the same rules
      if (thisTimeZone.HasSameRules(timeZoneArray[compareCtr]))
      {
         Console.WriteLine("{0} has the same rules as {1}", 
                           thisTimeZone.StandardName,
                           timeZoneArray[compareCtr].StandardName);
      }
   }
}
let timeZones = TimeZoneInfo.GetSystemTimeZones()
let timeZoneArray = Array.ofSeq timeZones
// Iterate array from top to bottom
for ctr = timeZoneArray.GetUpperBound 0 - 1 downto 0 do
    // Get next item from top
    let thisTimeZone = timeZoneArray[ctr]
    for compareCtr = 0 to ctr - 1 do
        // Determine if time zones have the same rules
        if thisTimeZone.HasSameRules timeZoneArray[compareCtr] then
            printfn $"{thisTimeZone.StandardName} has the same rules as {timeZoneArray[compareCtr].StandardName}"
Dim timeZones As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
Dim timeZoneArray(timeZones.Count - 1) As TimeZoneInfo
timeZones.CopyTo(timeZoneArray, 0) 
'Dim arrayPtr As Integer = 1
' Iterate array from top to bottom
For ctr As Integer = timeZoneArray.GetUpperBound(0) To 1 Step -1
   ' Get next item from top
   Dim thisTimeZone As TimeZoneInfo = timeZoneArray(ctr)
   For compareCtr As Integer = 0 To ctr - 1
      ' Determine if time zones have the same rules
      If thisTimeZone.HasSameRules(timeZoneArray(compareCtr)) Then
         Console.WriteLine("{0} has the same rules as {1}", _
                           thisTimeZone.StandardName, _
                           timeZoneArray(compareCtr).StandardName)
      End If                     
   Next      
Next

注解

与 方法一 TimeZoneInfo.Equals(TimeZoneInfo) 样, HasSameRules 方法指示两个时区的基偏移量 (是否与 BaseUtcOffset 属性) 定义相同,以及相同的调整规则。 TimeZoneInfo.Equals(TimeZoneInfo)与 方法不同, HasSameRules 不会比较属性) 定义的Id时区标识符 (。

适用于