convertTimezoneWithDateTime static method

DateTime convertTimezoneWithDateTime(
  1. DateTime fromTime,
  2. TGTimeZone fromTimeZone,
  3. TGTimeZone toTimeZone, {
  4. String dateTimePattern = _DATE_TIME_PATTERN,
})

Implementation

static DateTime convertTimezoneWithDateTime(
    DateTime fromTime, TGTimeZone fromTimeZone, TGTimeZone toTimeZone,
    {String dateTimePattern = _DATE_TIME_PATTERN}) {
  // Parse the input datetime
  final originalTime =
      DateTime.parse(DateFormat(dateTimePattern).format(fromTime));

  // Get the offset for the source and target timezones
  Duration? fromOffset = _getTimeZoneOffsets(fromTimeZone);
  Duration? toOffset = _getTimeZoneOffsets(toTimeZone);

  if (!toOffset.isNegative && fromOffset.isNegative) {
    fromOffset = fromOffset + const Duration(hours: 1);
  }

  if (toOffset.isNegative && !fromOffset.isNegative) {
    fromOffset = fromOffset - const Duration(hours: 1);
  }

  // Adjust the time fromTimezone > toTimezone
  final convertedTime = originalTime.subtract(fromOffset - toOffset);

  return convertedTime;
}