convertToCurrentTimeZone static method

DateTime convertToCurrentTimeZone(
  1. DateTime dateTime,
  2. String fromTimeZone,
  3. String toTimeZone
)

(Incomplete/Incorrect Implementation)

Attempts to convert a DateTime from one timezone to another. Warning: The current implementation using getTimeZoneOffset is likely incorrect for handling different timezones and daylight saving properly. It is recommended to use a robust library like timezone for this functionality.

Implementation

static DateTime convertToCurrentTimeZone(
  DateTime dateTime,
  String fromTimeZone,
  String toTimeZone,
) {
  // Parse the input datetime with the source timezone
  DateTime parsedDateTime =
      DateFormat(
        'yyyy-MM-dd HH:mm:ss',
      ).parse(dateTime.toString(), true).toLocal();

  // Convert the datetime to the target timezone
  DateTime convertedDateTime = parsedDateTime.toUtc().add(
    Duration(
      hours: getTimeZoneOffset(toTimeZone) - getTimeZoneOffset(fromTimeZone),
    ),
  );

  return convertedDateTime;
}