Прочетете на английски Редактиране

Споделяне чрез


TimeZoneInfo.HasSameRules(TimeZoneInfo) Method

Definition

Indicates whether the current object and another TimeZoneInfo object have the same adjustment rules.

C#
public bool HasSameRules(TimeZoneInfo other);

Parameters

other
TimeZoneInfo

A second object to compare with the current TimeZoneInfo object.

Returns

true if the two time zones have identical adjustment rules and an identical base offset; otherwise, false.

Exceptions

The other parameter is null.

Examples

Typically, a number of time zones defined in the registry on Windows and the ICU Library on Linux and macOS have the same offset from Coordinated Universal Time (UTC) and the same adjustment rules. The following example displays a list of these time zones to the console.

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

Remarks

Like the TimeZoneInfo.Equals(TimeZoneInfo) method, the HasSameRules method indicates whether two time zones have the same base offset (as defined by the BaseUtcOffset property) and the same adjustment rules. Unlike the TimeZoneInfo.Equals(TimeZoneInfo) method, HasSameRules does not compare time zone identifiers (as defined by the Id property).

Applies to

Продукт Версии
.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