Julian.fromYearAndDoy constructor

Julian.fromYearAndDoy(
  1. int year,
  2. double doy
)

Create a Julian date object given a year and day-of-year.

year The year, including the century (i.e., 2012). doy Day of year (1 means January 1, etc.). The fractional part of the day value is the fractional portion of the day. Examples: day = 1.0 Jan 1 00h day = 1.5 Jan 1 12h day = 2.0 Jan 2 00h

Implementation

/// The fractional part of the day value is the fractional portion of
/// the day.
/// Examples:
///    day = 1.0  Jan 1 00h
///    day = 1.5  Jan 1 12h
///    day = 2.0  Jan 2 00h
factory Julian.fromYearAndDoy(int year, double doy) {
  // Arbitrary years used for error checking
  if (year < 1900 || year > 2100) {
    throw Exception('Year (1900, 2100)');
  }

  // The last day of a leap year is day 366
  if (doy < 1.0 || doy >= 367.0) {
    throw Exception('Day (1, 367)');
  }

  // Now calculate Julian date
  // Ref: "Astronomical Formulae for Calculators", Jean Meeus, pages 23-25

  year--;

  // Centuries are not leap years unless they divide by 400
  int A = year ~/ 100;
  int B = 2 - A + (A ~/ 4);

  final jan01 =
      (365.25 * year).floor() + (30.6001 * 14).floor() + 1720994.5 + B;

  return Julian._(jan01 + doy);
}