Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Two-part Julian dates

Astronomers count time in days. The Julian date (JD) is the number of days since noon on January 1, 4713 BC, in the Julian calendar, so a date this century is around 2.46 million. That’s a convenient number for arithmetic and a bad one for a 64-bit float. An f64 near 2.46 million can only step in increments of about 40 microseconds, and anything smaller rounds away:

use celestial_time::julian::JulianDate;

let start = JulianDate::from_calendar(2026, 3, 20, 14, 46, 0.0)?;
println!("{start}");

let later = start.add_seconds(1.0e-6);
println!("two parts:  {:.3} µs", (later - start) * 86400.0e6);

let single = start.to_f64();
let single_later = single + 1.0e-6 / 86400.0;
println!("single f64: {:.3} µs", (single_later - single) * 86400.0e6);

let spacing = f64::from_bits(single.to_bits() + 1) - single;
println!("f64 spacing: {:.3} µs", spacing * 86400.0e6);
// JD 2461119.5 + 0.6152777777777778
// two parts:  1.000 µs
// single f64: 0.000 µs
// f64 spacing: 40.233 µs

The single f64 lost the microsecond entirely. JulianDate kept it, because it holds the date as two f64 values, jd1 and jd2, whose sum is the Julian date. The large part stays put and the small part carries the time of day with full precision. ERFA and SOFA use the same representation, and every time scale in this crate stores its instant this way.

How a date is split

Any split is valid: JD 2461120.0 is (2461120.0, 0.0), (2461119.5, 0.5) or (2400000.5, 61119.5). Precision comes from keeping the large, round number in one part and the small, detailed one in the other. Three splits are common:

SplitExampleMade by
Midnight plus time of day(2461119.5, 0.6152777777777778)from_calendar, *_from_calendar, parsing
MJD zero point plus MJD(2400000.5, 61132.3125)from_julian_year
J2000.0 plus days(2451545.0, 9570.115)you, with JulianDate::new

MJD_ZERO_POINT (2400000.5) and J2000_JD (2451545.0) are in celestial_core::constants.

The models in celestial-core never add the two parts before subtracting J2000. They compute (jd1 - J2000) + jd2, which keeps the low bits of jd2. Conversions between scales change only the smaller part, so the split you start with survives a chain like UTC to TAI to TT to TDB.

Create and read a date

use celestial_time::julian::JulianDate;

let a = JulianDate::new(2451545.0, 9570.115);
let b = JulianDate::from_calendar(2026, 3, 20, 14, 46, 0.0)?;
let c = JulianDate::from_julian_year(2026.25);

println!("{} {}", a.jd1(), a.jd2());
println!("{}", b.to_f64());
println!("{}", c);
println!("{}", b.to_julian_year());
// 2451545 9570.115
// 2461120.115277778
// JD 2400000.5 + 61132.3125
// 2026.2152369001444

to_f64() adds the parts. Use it for display or for code that only takes one number, and expect to lose precision below about 40 µs.

j2000() returns JD 2451545.0 and unix_epoch() returns JD 2440587.5, the start of 1970-01-01.

from_calendar reads the date as Gregorian, with astronomical year numbering: year 0 is 1 BC and year -1 is 2 BC. It doesn’t apply leap seconds. Use it, or the scale-specific tt_from_calendar and friends, for every scale except UTC, and utc_from_calendar for UTC. Create and format times covers those.

Arithmetic

add_days and add_seconds return a new date. Whole days go into jd1 and the fraction into jd2, so jd2 stays small after many steps:

use celestial_time::julian::JulianDate;

let start = JulianDate::new(2461119.5, 0.75);
println!("{}", start.add_days(1.5));
println!("{}", start.add_seconds(3600.0));
// JD 2461121.5 + 0.25
// JD 2461119.5 + 0.7916666666666666

Subtracting one JulianDate from another gives the difference in days as an f64. It subtracts part by part, so a short interval between two large dates keeps its precision.

Equality compares the dates, not the splits. Two values are equal when jd1 + jd2 is the same number:

use celestial_time::julian::JulianDate;

println!("{}", JulianDate::new(2461119.5, 0.5) == JulianDate::new(2461120.0, 0.0));
// true

From a date to a time scale

A bare JulianDate doesn’t say which scale it’s on. The scale types wrap one and add that meaning:

use celestial_time::julian::JulianDate;
use celestial_time::scales::tt::TT;

let jd = JulianDate::new(2451545.0, 9570.115);
let tt = TT::from_julian_date(jd);
let same: TT = jd.into();

println!("{tt}");
println!("{}", tt == same);
println!("{}", tt.to_julian_date().jd2());
// TT JD 2451545 + 9570.115
// true
// 9570.115

Every scale has from_julian_date, From<JulianDate>, to_julian_date, add_days and add_seconds. To hand a date to a celestial-core model, read the parts back with tt.to_julian_date().jd1() and .jd2().

Edge cases

  • JulianDate::new accepts any f64, including NaN. Conversions, sidereal time and TT::centuries_since_j2000 return a TimeError for a date with a non-finite part.
  • julian_to_calendar works for JD -68569.5 to 1e9, which reaches back to -4900-03-01. Building a date from calendar fields needs a year of -4799 or later. Both are ERFA’s limits (eraJd2cal and eraCal2jd), so a date in the years -4900 to -4800 converts to a calendar date but not back.
  • On a UTC day that ends in a leap second, a UTC Julian date counts fractions of an 86,401 s day. UTC and leap seconds explains why to subtract in TAI.

Where next