dateTimeToZone function

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();
  if (timeZoneOffsets[zone] <= 0) {
    subtr = Duration(
        hours: timeZoneOffsets[zone].abs().truncate(),
        minutes: ((timeZoneOffsets[zone].abs() % 1) * 60).round());
    returner = datetime.subtract(subtr);
  } else {
    addr = Duration(
        hours: timeZoneOffsets[zone].truncate(),
        minutes: ((timeZoneOffsets[zone] % 1) * 60).round());
    returner = datetime.add(addr);
  }
  return returner;
}