curDateTimeByZone function Null safety

DateTime curDateTimeByZone(
  1. {required String zone}
)

Takes a timezone, and returns the current DateTime in that timezone.

See timeZoneOffsets for all available time zones.

Implementation

DateTime curDateTimeByZone({required String zone}) {
  var now = DateTime.now();
  var toUTC = now.toUtc();
  DateTime returner;
  Duration subtr;
  Duration addr;

  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 = toUTC.subtract(subtr);
  } else {
    addr = Duration(
        hours: offset.truncate(), minutes: ((offset % 1) * 60).round());
    returner = toUTC.add(addr);
  }
  return returner;
}