共用方式為


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 方法會指出兩個時區的基底位移 (是否與 屬性) 和相同的調整規則所定義 BaseUtcOffsetTimeZoneInfo.Equals(TimeZoneInfo)不同于 方法, HasSameRules 不會比較屬性所 Id 定義 (時區識別碼) 。

適用於