ToTimeZoneInfo method

Location ToTimeZoneInfo(
  1. ExchangeService? service
)
Converts this time zone definition into a TimeZoneInfo structure. The service.

Implementation

tz.Location ToTimeZoneInfo(ExchangeService? service) {
  Validate();

  // Retrieve the base offset to UTC, standard and daylight display names from
  // the last transition group, which is the one that currently applies given that
  // transitions are ordered chronologically.
  CustomTimeZoneCreateParams creationParams =
      _transitions[_transitions.length - 1].TargetGroup!
          .GetCustomTimeZoneCreationParams();

  List<TimeZoneAdjustmentRule> adjustmentRules = <TimeZoneAdjustmentRule>[];

  // null stands for DateTime.MinValue / DateTime.MaxValue.
  DateTime? startDate;
  DateTime? endDate;

  for (int i = 0; i < _transitions.length; i++) {
    if (i < _transitions.length - 1) {
      endDate = _ToWallClock(
        (_transitions[i + 1] as AbsoluteDateTransition).DateTime!,
      );
    } else {
      endDate = null;
    }

    // OM:1648848 Due to bad timezone data from clients the
    // startDate may not always come before the effectiveEndDate
    if (startDate == null || endDate == null || startDate.isBefore(endDate)) {
      TimeZoneAdjustmentRule adjustmentRule = _transitions[i].TargetGroup!
          .CreateAdjustmentRule(startDate, endDate);

      adjustmentRules.add(adjustmentRule);

      startDate = endDate;
    } else {
      service?.TraceMessage(
        TraceFlags.EwsTimeZones,
        "The startDate '$startDate' is not before the effectiveEndDate '$endDate'. Will skip creating adjustment rule.",
      );
    }
  }

  final Duration baseOffset = Duration(
    milliseconds: creationParams.BaseOffsetToUtc!.time,
  );

  // If there are no adjustment rules with daylight saving time and the offset never
  // changes, the time zone does not support Daylight saving time.
  final bool hasRules = adjustmentRules.any(
    (rule) => rule.SupportsDaylight || rule.StandardOffset != baseOffset,
  );

  return TimeZoneRuleConverter.ToLocation(
    (StringUtils.IsNullOrEmpty(Id) ? Name : Id) ?? "",
    TimeZoneRuleSet(
      baseOffset,
      hasRules ? adjustmentRules : <TimeZoneAdjustmentRule>[],
    ),
    standardName:
        creationParams.StandardDisplayName ??
        TimeZonePeriod.StandardPeriodName,
    daylightName:
        creationParams.DaylightDisplayName ??
        TimeZonePeriod.DaylightPeriodName,
  );
}