convertTime function Null safety

double convertTime(
  1. Time from,
  2. Time to,
  3. double value
)

Convert Time. It may lose some decimals in the proccess.

from, to and value can NOT be null, otherwise an AssertionError is thrown

Basic usage:

main() {
  final time = convertTime(
    Time.minute, // from
    Time.second, // to
    1, // value
  );
  print(temperature); // 60
}

Read the documentation for more information

Implementation

double convertTime(Time from, Time to, double value) {
  verify(from, to, value);
  if (from == to) return value;
  if (from == Time.second) {
    switch (to) {
      case Time.nanosecond:
        return value * 1e+9;
      case Time.microsecond:
        return value * 1e+6;
      case Time.millisecond:
        return value * 1000;
      case Time.minute:
        return value / 60;
      case Time.hour:
        return value / 3600;
      case Time.day:
        return value / 86400;
      case Time.week:
        return value / 604800;
      case Time.month:
        return value / 2.628e+6;
      case Time.calendarYear:
        return value / 3.154e+7;
      case Time.decade:
        return value / 3.154e+8;
      case Time.century:
        return value / 3.154e+9;
      case Time.millenium:
        return value / 3.154e+10;
      default:
        return value;
    }
  } else {
    final second = Time.second;
    switch (from) {
      case Time.nanosecond:
        return convertTime(second, to, value / 1e+9);
      case Time.microsecond:
        return convertTime(second, to, value / 1e+6);
      case Time.millisecond:
        return convertTime(second, to, value / 1000);
      case Time.minute:
        return convertTime(second, to, value * 60);
      case Time.hour:
        return convertTime(second, to, value * 3600);
      case Time.day:
        return convertTime(second, to, value * 86400);
      case Time.week:
        return convertTime(second, to, value * 604800);
      case Time.month:
        return convertTime(second, to, value * 2.628e+6);
      case Time.calendarYear:
        return convertTime(second, to, value * 3.154e+7);
      case Time.decade:
        return convertTime(second, to, value * 3.154e+8);
      case Time.century:
        return convertTime(second, to, value * 3.154e+9);
      case Time.millenium:
        return convertTime(second, to, value * 3.154e+10);
      default:
        return value;
    }
  }
}