TimeZoneInfo.AdjustmentRule.Equals Method (TimeZoneInfo.AdjustmentRule)

 

Determines whether the current TimeZoneInfo.AdjustmentRule object is equal to a second TimeZoneInfo.AdjustmentRule object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public bool Equals(
	TimeZoneInfo.AdjustmentRule other
)

Parameters

other
Type: System.TimeZoneInfo.AdjustmentRule

The object to compare with the current object.

Return Value

Type: System.Boolean

true if both TimeZoneInfo.AdjustmentRule objects have equal values; otherwise, false.

To determine whether two TimeZoneInfo.AdjustmentRule objects are equal, the Equals(TimeZoneInfo.AdjustmentRule) method compares the member values of each object. Two adjustment rules are equal if they have the same effective dates, the same delta, and identical values for the TimeZoneInfo.TransitionTime objects returned by their DaylightTransitionStart and DaylightTransitionEnd properties.

The following example calls the TimeZoneInfo.AdjustmentRule.Equals(TimeZoneInfo.AdjustmentRule) method to compare the adjustment rules for Central Standard Time with those for Canada Central Standard Time and Mexico Standard Time.

string timeZoneName = "";
// Get CST, Canadian CST, and Mexican CST adjustment rules
TimeZoneInfo.AdjustmentRule[] usCstAdjustments = null;
TimeZoneInfo.AdjustmentRule[] canCstAdjustments = null;
TimeZoneInfo.AdjustmentRule[] mexCstAdjustments = null;
try
{
   timeZoneName = "Central Standard Time";
   usCstAdjustments = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName).GetAdjustmentRules();
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The {0} time zone is not defined in the registry.", 
                     timeZoneName);
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Data for the {0} time zone is invalid.", 
                     timeZoneName);
}
try
{
   timeZoneName = "Canada Central Standard Time";
   canCstAdjustments = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName).GetAdjustmentRules();
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The {0} time zone is not defined in the registry.", 
                     timeZoneName);
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Data for the {0} time zone is invalid.", 
                     timeZoneName);
}
try
{
   timeZoneName = "Central Standard Time (Mexico)";
   mexCstAdjustments = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName).GetAdjustmentRules();
}   
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The {0} time zone is not defined in the registry.", 
                     timeZoneName);
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Data for the {0} time zone is invalid.", 
                     timeZoneName);
}
// Determine if CST and other time zones have the same rules
foreach(TimeZoneInfo.AdjustmentRule rule in usCstAdjustments)
{
   Console.WriteLine("Comparing Central Standard Time rule for {0:d} to {1:d} with:", 
                     rule.DateStart, rule.DateEnd);
   // Compare with Canada Central Standard Time
   if (canCstAdjustments.Length == 0)
   {
      Console.WriteLine("   Canada Central Standard Time has no adjustment rules.");
   }   
   else
   {
      foreach (TimeZoneInfo.AdjustmentRule canRule in canCstAdjustments)
      {
         Console.WriteLine("   Canadian CST for {0:d} to {1:d}: {2}", 
                           canRule.DateStart, canRule.DateEnd, 
                           rule.Equals(canRule) ? "Equal" : "Not Equal");
      }              
   }          

   // Compare with Mexico Central Standard Time
   if (mexCstAdjustments.Length == 0)
   {
      Console.WriteLine("   Mexican Central Standard Time has no adjustment rules.");
   }
   else
   {
      foreach (TimeZoneInfo.AdjustmentRule mexRule in mexCstAdjustments)
      {
         Console.WriteLine("   Mexican CST for {0:d} to {1:d}: {2}", 
                           mexRule.DateStart, mexRule.DateEnd, 
                           rule.Equals(mexRule) ? "Equal" : "Not Equal");
      }              
   }
}   
// This code displays the following output to the console:
// 
// Comparing Central Standard Time rule for 1/1/0001 to 12/31/9999 with:
//    Canada Central Standard Time has no adjustment rules.
//    Mexican CST for 1/1/0001 to 12/31/9999: Equal

This code displays the following output to the console:

Comparing Central Standard Time rule for 1/1/0001 to 12/31/9999 with:
   Canada Central Standard Time has no adjustment rules.
   Mexican CST for 1/1/0001 to 12/31/9999: Equal

.NET Framework
Available since 3.5
Return to top
Show: