ScopeToTimeZone method

  1. @override
DateTime ScopeToTimeZone(
  1. ExchangeServiceBase service,
  2. DateTime dateTime,
  3. PropertyBag propertyBag,
  4. bool isUpdateOperation,
)
override
Scopes the date time property to the appropriate time zone, if necessary. The service emitting the request. The date time. The property bag. Indicates whether the scoping is to be performed in the context of an update operation.

Implementation

@override
DateTime ScopeToTimeZone(
  ExchangeServiceBase service,
  DateTime dateTime,
  PropertyBag propertyBag,
  bool isUpdateOperation,
) {
  if (!propertyBag.Owner!.GetIsCustomDateTimeScopingRequired()) {
    // Most item types do not require a custom scoping mechanism. For those item types,
    // use the default scoping mechanism.
    return super.ScopeToTimeZone(
      service,
      dateTime,
      propertyBag,
      isUpdateOperation,
    );
  } else {
    // Appointment, however, requires a custom scoping mechanism which is based on an
    // associated time zone property.
    PropertyDefinition timeZoneProperty = GetTimeZoneProperty(
      service.RequestedServerVersion,
    );
    OutParam<Object> timeZonePropertyValueOutParam = OutParam<Object>();

    bool timeZonePropertyIsSet = propertyBag.TryGetProperty(
      timeZoneProperty,
      timeZonePropertyValueOutParam,
    );
    Object? timeZonePropertyValue = timeZonePropertyValueOutParam.param;

    if (timeZonePropertyIsSet &&
        timeZonePropertyValue != null &&
        propertyBag.IsPropertyUpdated(timeZoneProperty)) {
      // If we have the associated time zone property handy and if it has been updated locally,
      // then we scope the date time to that time zone.
      try {
        DateTime convertedDateTime = EwsUtilities.ConvertTime(
          dateTime,
          timeZonePropertyValue is TimeZone
              ? EwsUtilities.LocationFromTimeZone(timeZonePropertyValue)
              : timeZonePropertyValue as Location,
          UTC,
        );

        // This is necessary to stamp the date/time with the Utc kind.
        return convertedDateTime;
      } on TimeZoneConversionException catch (ex, stacktrace) {
        throw PropertyException(
          "InvalidDateTime($dateTime)",
          Name,
          ex,
          stacktrace,
        );
      }
    } else {
      if (isUpdateOperation) {
        // In an update operation, what we do depends on what version of EWS
        // we are targeting.
        if (service.RequestedServerVersion ==
            ExchangeVersion.Exchange2007_SP1) {
          // For Exchange 2007 SP1, we still need to scope to the service's time zone.
          return super.ScopeToTimeZone(
            service,
            dateTime,
            propertyBag,
            isUpdateOperation,
          );
        } else {
          // Otherwise, we let the server scope to the appropriate time zone.
          return dateTime;
        }
      } else {
        // In a Create operation, always scope to the service's time zone.
        return super.ScopeToTimeZone(
          service,
          dateTime,
          propertyBag,
          isUpdateOperation,
        );
      }
    }
  }
}