getUtcTimeFromLocal method

  1. @useResult
DateTime getUtcTimeFromLocal(
  1. double offset
)

Converts this local DateTime to UTC using the given timezone offset.

offset is the hours-ahead-of-UTC value for this DateTime's timezone. Positive for timezones east of UTC (e.g., 2.0 for UTC+2), negative for timezones west of UTC (e.g., -5.0 for UTC-5). Fractional values represent partial hours (e.g., 5.5 for UTC+5:30).

Always returns a non-null DateTime. Returns the original instance if offset is 0.

Implementation

@useResult
DateTime getUtcTimeFromLocal(double offset) {
  if (offset == 0) {
    return this;
  }

  final int hours = offset.truncate();
  final int minutes = ((offset - hours) * DateConstants.minutesPerHour).round();

  return subtract(Duration(hours: hours, minutes: minutes));
}