toDateTime method

DateTime toDateTime({
  1. bool utc = false,
})

This value as a DateTime.

Throws when the conversion would lose information:

  • DateTime resolves to microseconds and datetime2(7)/time(7) to 100 nanoseconds, so a nanosecond field that is not a whole number of microseconds cannot round-trip.
  • DateTime is either UTC or local and cannot carry an arbitrary offset, so a non-zero timezoneOffsetMinutes would be dropped.

Refusing rather than rounding is the same choice this package makes for DECIMAL: losing precision quietly is worse than an error. Generated code only calls this where the schema says the conversion is lossless, so the throw is a safety net for hand-written calls.

Implementation

DateTime toDateTime({bool utc = false}) {
  if (nanosecond % 1000 != 0) {
    throw ArgumentError.value(
      nanosecond,
      'nanosecond',
      'DateTime resolves to microseconds and this value carries $nanosecond '
          'nanoseconds. Keep the MssqlDateTimeValue, or read the column at '
          'a scale of 6 or less.',
    );
  }
  if (timezoneOffsetMinutes != 0) {
    throw ArgumentError.value(
      timezoneOffsetMinutes,
      'timezoneOffsetMinutes',
      'DateTime cannot carry an offset of $timezoneOffsetMinutes minutes; '
          'it is either UTC or local. Keep the MssqlDateTimeValue.',
    );
  }
  final microsecond = nanosecond ~/ 1000;
  return utc
      ? DateTime.utc(
          year,
          month,
          day,
          hour,
          minute,
          second,
          microsecond ~/ 1000,
          microsecond % 1000,
        )
      : DateTime(
          year,
          month,
          day,
          hour,
          minute,
          second,
          microsecond ~/ 1000,
          microsecond % 1000,
        );
}