toDateTime method

DateTime toDateTime()

Converts from Julian to DateTime.

Implementation

DateTime toDateTime() {
  final j = value;
  final double d2 = j + 0.5;
  final int Z = d2.toInt();
  final int alpha = (Z - 1867216.25) ~/ 36524.25;
  final int A = Z + 1 + alpha - (alpha ~/ 4);
  final int B = A + 1524;
  final int C = ((B - 122.1) ~/ 365.25);
  final int D = (365.25 * C).toInt();
  final int E = ((B - D) ~/ 30.6001);

  // For reference: the fractional day of the month can be
  // calculated as follows:
  //
  // double day = B - D - (int)(30.6001 * E) + F;

  final month = (E <= 13) ? (E - 1) : (E - 13);
  final year = (month >= 3) ? (C - 4716) : (C - 4715);

  final jdJan01 = Julian.fromYearDoy(year, 1.0);
  final doy = j - jdJan01.value; // zero-relative

  final dtJan01 = DateTime.utc(year, 1, 1, 0, 0, 0);

  return dtJan01
      .add(Duration(microseconds: (doy * 24.0 * 3600 * 1000 * 1000).toInt()));
}