dateTimeToOffset function

DateTime dateTimeToOffset({
  1. required double offset,
  2. required DateTime datetime,
})

Takes a UTC offset and DateTime.

Returns the given DateTime adjusted to that offset.

Generally, dateTimeToZone is preferable for readability purposes, so use that if possible. Only use this if desired timezone/offset is not listed in timeZoneOffsets.

Assumes datetime is in UTC.

Implementation

DateTime dateTimeToOffset(
    {required double offset, required DateTime datetime}) {
  var toUTC = datetime;
  DateTime returner;
  Duration subtr;
  Duration addr;
  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;
}