UTC and leap seconds
The last leap second so far came at the end of 2016. That night, clocks running UTC read
23:59:59, then 23:59:60, then 00:00:00. The minute had 61 seconds. Most software can’t
represent that second at all. celestial-time can:
use celestial_time::scales::conversions::{ToTAI, ToUTC};
use celestial_time::scales::utc::UTC;
for s in [
"2016-12-31T23:59:59Z",
"2016-12-31T23:59:60Z",
"2016-12-31T23:59:60.5Z",
"2017-01-01T00:00:00Z",
] {
let utc: UTC = s.parse()?;
let tai = utc.to_tai()?;
println!("{s} {tai} back: {}", tai.to_utc()?.to_iso8601()?);
}
// 2016-12-31T23:59:59Z TAI JD 2457753.5 + 1.0004050925925927 back: 2016-12-31T23:59:59.000
// 2016-12-31T23:59:60Z TAI JD 2457753.5 + 1.0004166666666667 back: 2016-12-31T23:59:60.000
// 2016-12-31T23:59:60.5Z TAI JD 2457753.5 + 1.0004224537037036 back: 2016-12-31T23:59:60.500
// 2017-01-01T00:00:00Z TAI JD 2457754.5 + 0.00042824074074074075 back: 2017-01-01T00:00:00.000
Each UTC instant maps to its own TAI instant and back, including the half second in the middle of the leap second.
Why UTC has leap seconds
UTC is a compromise between two kinds of time. Atomic clocks tick perfectly even seconds. The Earth’s rotation, which sets sunrise and noon, doesn’t: tides slow it down, and the core and atmosphere make it wander. TAI and UT1 roughly agreed in 1958; by 2017 they were 37 s apart.
UTC ticks atomic seconds, the same as TAI, and the IERS inserts a leap second at the end of June or December whenever UT1 (the Earth’s rotation) is about to get more than 0.9 s away. So UTC is always within a second of the sky, and TAI - UTC is a whole number of seconds that grows by one with each leap second: 10 s in 1972, 37 s since 2017.
The cost is that UTC isn’t uniform. A day that ends in a leap second is 86,401 s long.
Measure intervals in TAI
A UTC Julian date on a leap second day counts fractions of an 86,401 s day, so subtracting two UTC dates across the leap second gives the wrong answer:
use celestial_time::scales::conversions::ToTAI;
use celestial_time::scales::utc::UTC;
let before: UTC = "2016-12-31T23:59:59Z".parse()?;
let after: UTC = "2017-01-01T00:00:00Z".parse()?;
let utc_days = after.to_julian_date() - before.to_julian_date();
let tai_days = after.to_tai()?.to_julian_date() - before.to_tai()?.to_julian_date();
println!("UTC: {:.9} s", utc_days * 86400.0);
println!("TAI: {:.9} s", tai_days * 86400.0);
// UTC: 1.999976852 s
// TAI: 2.000000000 s
Two seconds passed: 23:59:59 to 23:59:60, then to 00:00:00. TAI gets it right. The UTC difference is off by 23 µs because the two dates fall in days of different lengths.
ERFA uses the same convention, and UTC Julian dates from this crate match its values
bit for bit. The rule that follows: convert to TAI or TT before you subtract, and use add_seconds on a TAI or TT value rather than a UTC one when you step across a
day that might end in a leap second.
Where 23:59:60 is allowed
A seconds value of 60 or more is accepted only in the last minute of a day that ends with a leap second, and only for UTC:
use celestial_time::scales::tai::TAI;
use celestial_time::scales::utc::{utc_from_calendar, UTC};
println!("{:?}", utc_from_calendar(2016, 12, 31, 23, 59, 60.0).is_ok());
println!("{:?}", "2026-12-31T23:59:60Z".parse::<UTC>());
println!("{:?}", "2016-12-31T23:59:60".parse::<TAI>());
// true
// Err(InvalidDate("time 23:59:60 is out of range"))
// Err(InvalidDate("time 23:59:60 is out of range"))
The other seven scales have no leap seconds, so every one of their days is 86,400 s.
The leap second table
The crate has a built-in table of TAI - UTC that starts on 1960-01-01, when UTC began.
Its last entry is 2017-01-01, after the leap second at the end of 2016.
get_tai_utc_offset reads it for a date and a fraction of the day:
use celestial_time::scales::common::get_tai_utc_offset;
for (year, month, day, fraction) in [
(1968, 6, 1, 0.5),
(1972, 1, 1, 0.0),
(2016, 12, 31, 0.0),
(2017, 1, 1, 0.0),
(2026, 3, 20, 0.0),
] {
let offset = get_tai_utc_offset(year, month, day, fraction)?;
println!("{year}-{month:02}-{day:02} + {fraction}: TAI - UTC = {offset} s");
}
// 1968-06-01 + 0.5: TAI - UTC = 6.50061 s
// 1972-01-01 + 0: TAI - UTC = 10 s
// 2016-12-31 + 0: TAI - UTC = 36 s
// 2017-01-01 + 0: TAI - UTC = 37 s
// 2026-03-20 + 0: TAI - UTC = 37 s
println!("{}", get_tai_utc_offset(1959, 12, 31, 0.0).unwrap_err());
// Invalid date: 1959-12-31: UTC is not defined before 1960-01-01
Dates before 1960 return an error, from this function and from every UTC conversion, because there was no UTC to convert to.
Before 1972, UTC worked differently. Its seconds were stretched slightly so that UTC followed the Earth’s rotation continuously, and it stepped by fractions of a second at irregular dates. TAI - UTC wasn’t a whole number, and it changed through each day. The table holds those rates for the 14 entries from 1960 to 1968, and the conversions apply them, which is why 1968-06-01 at noon gives 6.50061 s.
Every date after 2017-01-01 gets 37 s, which is still the correct value today. When the
IERS announces another leap second, the TAI_UTC_OFFSETS table in
celestial-time/src/constants.rs needs a new entry. Until it has one, UTC conversions
for dates after that leap second are off by a second. ERFA’s eraDat also returns 37 s
for late dates, and adds a warning status for years more than five after its release
year. This crate has no such warning.
Convert to and from Unix time
Unix time counts seconds since 1970-01-01 as if every day had 86,400 s. It has no value
for 23:59:60, so the timestamp after 23:59:59 on a leap second day is 00:00:00 the next
day. UTC::new and to_unix convert both ways:
use celestial_time::scales::utc::UTC;
let new_year = UTC::new(1_483_228_800, 0)?;
println!("{}", new_year.to_iso8601()?);
println!("{:?}", new_year.to_unix()?);
let leap: UTC = "2016-12-31T23:59:60.5Z".parse()?;
println!("{}", leap.to_unix().unwrap_err());
// 2017-01-01T00:00:00.000
// (1483228800, 0)
// Conversion error: 2016-12-31 86400.5 s after midnight is in the day's extra time, which Unix time cannot represent
to_unix returns an error for any instant inside a leap second rather than guessing
which neighbor you wanted. For every (seconds, nanos) that UTC::new accepts,
to_unix gives back the same pair.
Edge cases
- UT1 to UTC on 1960-01-01 still works. Its search for a nearby leap second reads the
day before as 0 s, as ERFA’s
eraUt1utcdoes, so it treats the start of UTC as a 0.94 s step. A UT1 date before 1960 returns an error. - The pre-1972 rates make
ToUTCiterate. It runs three steps, and the round trip from UTC to TAI and back comes within 20 ps. - UTC to UT1 takes TAI - UTC at 0h on the date, as ERFA does. Before 1972 that differs from the value at the time itself by up to 2.6 ms, so a UTC to UT1 to UTC round trip on those dates can be off by up to 2.6 ms, and by up to 0.11 s on a day that ends in a step. From 1972 on, the round trip is exact to within 1 ulp.
to_iso8601formats a pre-1972 day on an 86,400 s clock, even when that day ended in a fractional step.
Where next
- Sidereal time covers UT1 and DUT1, the other half of what leap seconds are about.
- Create and format times has the full parsing rules.
- Two-part Julian dates explains the
JD a + bvalues above.