英語で読む

次の方法で共有


TimeZoneInfo.HasSameRules(TimeZoneInfo) メソッド

定義

現在のオブジェクトと別の TimeZoneInfo オブジェクトが同じ調整規則を持っているかどうかを判断します。

C#
public bool HasSameRules(TimeZoneInfo other);

パラメーター

other
TimeZoneInfo

現在の TimeZoneInfo オブジェクトと比較するもう 1 つのオブジェクト。

戻り値

2 つのタイム ゾーンが同じ調整規則と同じベース オフセットを持っている場合は true。それ以外の場合は false

例外

other パラメーターが null です。

通常、Windows のレジストリと Linux および macOS 上の ICU ライブラリ で定義されているタイム ゾーンの数は、協定世界時 (UTC) と同じ調整規則から同じオフセットを持ちます。 次の例では、これらのタイム ゾーンの一覧をコンソールに表示します。

C#
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);
      }
   }
}

注釈

メソッドと同様に TimeZoneInfo.Equals(TimeZoneInfo) 、 メソッドは HasSameRules 、2 つのタイム ゾーンが同じベース オフセット (プロパティで BaseUtcOffset 定義) と同じ調整規則を持っているかどうかを示します。 TimeZoneInfo.Equals(TimeZoneInfo)メソッドとは異なり、 HasSameRules はタイム ゾーン識別子 (プロパティでId定義) を比較しません。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1