dateTimeToZone function Null safety

DateTime dateTimeToZone(
  1. {required String zone,
  2. required DateTime datetime}
)

Takes a timezone and Datetime.

Returns the given DateTime adjusted to that timezone.

See timeZoneOffsets for all available time zones.

Assumes datetime is in UTC.

Implementation

DateTime dateTimeToZone({required String zone, required DateTime datetime}) {
  DateTime returner;
  Duration subtr;
  Duration addr;
  datetime = datetime.toUtc();

  var offset = timeZoneOffsets[zone];
  if (offset == null) throw ArgumentError('Unsupported zone: $zone');

  if (offset <= 0) {
    subtr = Duration(
        hours: offset.abs().truncate(),
        minutes: ((offset.abs() % 1) * 60).round());
    returner = datetime.subtract(subtr);
  } else {
    addr = Duration(
        hours: offset.truncate(), minutes: ((offset % 1) * 60).round());
    returner = datetime.add(addr);
  }
  return returner;
}