getLeapSeconds function

num getLeapSeconds(
  1. double tai, {
  2. bool pre1972LeapSeconds = false,
})

Returns the number of leap seconds in effect for the specified time instant, tai, specified in the TAI time scale. The number of leap seconds relates the UTC time scale to the TAI time scale.

UTC = TAI - leap seconds.

UTC, as of 2018, was running 37 seconds behind TAI. Historical information for leap seconds is available from http://tycho.usno.navy.mil

Integral leap seconds were instituted in 1972. For the period 1961-1971, 'official' leap seconds equations are used to relate TAI and UTC. By default these equations are ignored and all pre-1972 leap seconds values will be returned as zero. To instead use the parametric equations for leap seconds between 1961 and the end of 1971, set pre1972LeapSeconds to true.

For dates before 1958, delta T should instead be used to establish the relationship between Universal Time (UT1) and TAI. UTC is not corrected for changes in the Earth's length of day before 1958.

Implementation

num getLeapSeconds(double tai, {bool pre1972LeapSeconds = false}) {
  // Use the TAI scale to identify when leap seconds occur

  // Outside thresholds?
  if (tai < 94694400.0) {
    return 0; //   (2441317.5-2436204.5) * 86400.0         [< 1 Jan 1961]
  }
  if (tai >= 1861920036.0) {
    return 37; //  ((2457754.5-2436204.5) * 86400.0) + 36 [>= 1 Jan 2017]
  }

  // Pre-1972? (2441317.5 - 2436204.5) * 86400.0 [< 1 Jan 1972]
  if (tai < 441763200.0) {
    if (pre1972LeapSeconds) {
      // Even though the equations below use (presumably) the Julian Date in
      // the UTC time scale, we use the Julian Date in the TAI time scale.
      // This introduces a very small error on the order of 2x10^-7 seconds,
      // which is within the accuracy of this method.
      final mjdTAI = (tai / 86400.0) + 36204.0;

      if (tai < 113011201.6975700) {
        return 1.422818 + (mjdTAI - 37300.0) * 0.0012960; // [< 1 Aug 1961]
      }
      if (tai < 126230401.8458580) {
        return 1.372818 + (mjdTAI - 37300.0) * 0.0012960; // [< 1 Jan 1962]
      }
      if (tai < 184032002.5972788) {
        return 1.845858 + (mjdTAI - 37665.0) * 0.0011232; // [< 1 Nov 1963]
      }
      if (tai < 189302402.7657940) {
        return 1.945858 + (mjdTAI - 37665.0) * 0.0011232; // [< 1 Jan 1964]
      }
      if (tai < 197164802.8837300) {
        return 3.240130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Apr 1964]
      }
      if (tai < 210384003.1820180) {
        return 3.340130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Sep 1964]
      }
      if (tai < 220924803.4401300) {
        return 3.440130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Jan 1965]
      }
      if (tai < 226022403.6165940) {
        return 3.540130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Mar 1965]
      }
      if (tai < 236563203.8747060) {
        return 3.640130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Jul 1965]
      }
      if (tai < 241920004.0550580) {
        return 3.740130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Sep 1965]
      }
      if (tai < 252460804.3123170) {
        return 3.840130 + (mjdTAI - 38761.0) * 0.0012960; // [< 1 Jan 1966]
      }
      if (tai < 318211206.2856820) {
        return 4.313170 + (mjdTAI - 39126.0) * 0.0025920; // [< 1 Feb 1968]
      }
      return 4.213170 + (mjdTAI - 39126.0) * 0.0025920; // [< 1 Jan 1972]
    }

    return 0;
  }

  // Integral Leap Seconds (1972- )
  if (tai < 457488010.0) {
    return 10; // ((2441499.5-2436204.5) * 86400.0) + 10 [< 1 Jul 1972]
  }
  if (tai < 473385611.0) {
    return 11; // ((2441683.5-2436204.5) * 86400.0) + 11 [< 1 Jan 1973]
  }
  if (tai < 504921612.0) {
    return 12; // ((2442048.5-2436204.5) * 86400.0) + 12 [< 1 Jan 1974]
  }
  if (tai < 536457613.0) {
    return 13; // ((2442413.5-2436204.5) * 86400.0) + 13 [< 1 Jan 1975]
  }
  if (tai < 567993614.0) {
    return 14; // ((2442778.5-2436204.5) * 86400.0) + 14 [< 1 Jan 1976]
  }
  if (tai < 599616015.0) {
    return 15; // ((2443144.5-2436204.5) * 86400.0) + 15 [< 1 Jan 1977]
  }
  if (tai < 631152016.0) {
    return 16; // ((2443509.5-2436204.5) * 86400.0) + 16 [< 1 Jan 1978]
  }
  if (tai < 662688017.0) {
    return 17; // ((2443874.5-2436204.5) * 86400.0) + 17 [< 1 Jan 1979]
  }
  if (tai < 694224018.0) {
    return 18; // ((2444239.5-2436204.5) * 86400.0) + 18 [< 1 Jan 1980]
  }
  if (tai < 741484819.0) {
    return 19; // ((2444786.5-2436204.5) * 86400.0) + 19 [< 1 Jul 1981]
  }
  if (tai < 773020820.0) {
    return 20; // ((2445151.5-2436204.5) * 86400.0) + 20 [< 1 Jul 1982]
  }
  if (tai < 804556821.0) {
    return 21; // ((2445516.5-2436204.5) * 86400.0) + 21 [< 1 Jul 1983]
  }
  if (tai < 867715222.0) {
    return 22; // ((2446247.5-2436204.5) * 86400.0) + 22 [< 1 Jul 1985]
  }
  if (tai < 946684823.0) {
    return 23; // ((2447161.5-2436204.5) * 86400.0) + 23 [< 1 Jan 1988]
  }
  if (tai < 1009843224.0) {
    return 24; // ((2447892.5-2436204.5) * 86400.0) + 24 [< 1 Jan 1990]
  }
  if (tai < 1041379225.0) {
    return 25; // ((2448257.5-2436204.5) * 86400.0) + 25 [< 1 Jan 1991]
  }
  if (tai < 1088640026.0) {
    return 26; // ((2448804.5-2436204.5) * 86400.0) + 26 [< 1 Jul 1992]
  }
  if (tai < 1120176027.0) {
    return 27; // ((2449169.5-2436204.5) * 86400.0) + 27 [< 1 Jul 1993]
  }
  if (tai < 1151712028.0) {
    return 28; // ((2449534.5-2436204.5) * 86400.0) + 28 [< 1 Jul 1994]
  }
  if (tai < 1199145629.0) {
    return 29; // ((2450083.5-2436204.5) * 86400.0) + 29 [< 1 Jan 1996]
  }
  if (tai < 1246406430.0) {
    return 30; // ((2450630.5-2436204.5) * 86400.0) + 30 [< 1 Jul 1997]
  }
  if (tai < 1293840031.0) {
    return 31; // ((2451179.5-2436204.5) * 86400.0) + 31 [< 1 Jan 1999]
  }
  if (tai < 1514764832.0) {
    return 32; // ((2453736.5-2436204.5) * 86400.0) + 32 [< 1 Jan 2006]
  }
  if (tai < 1609459233.0) {
    return 33; // ((2454832.5-2436204.5) * 86400.0) + 33 [< 1 Jan 2009]
  }
  if (tai < 1719792034.0) {
    return 34; // ((2456109.5-2436204.5) * 86400.0) + 34 [< 1 Jul 2012]
  }
  if (tai < 1814400035.0) {
    return 35; // ((2457204.5-2436204.5) * 86400.0) + 35 [< 1 Jul 2015]
  }
  return 36; // ((2457754.5-2436204.5) * 86400.0) + 36 [< 1 Jan 2017]

  // 37 [>= 1861920036.0; 1 Jan 2017] handled at start of method.
}