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

Create and format times

Timestamps arrive in different shapes: an ISO 8601 string in a FITS header, a Unix timestamp from a camera driver, year-month-day fields from a log, or the system clock. Each time scale type can be built from all of them, and the scale you build is the scale the timestamp is read on. Nothing converts behind your back.

use celestial_time::scales::tt::TT;
use celestial_time::scales::utc::UTC;

let utc: UTC = "2026-03-20T04:30:00Z".parse()?;
let tt: TT = "2026-03-20T04:30:00".parse()?;

println!("{utc}");
println!("{tt}");
// UTC JD 2461119.5 + 0.1875
// TT JD 2461119.5 + 0.1875

Both hold the same Julian date, and they’re different instants: 69.184 s apart. If your string is UTC, which it almost always is, parse it as UTC and convert. See Time scales for the conversions.

Parse a string

All eight scales implement FromStr. They accept one format:

YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.fff...
±YYYYY-MM-DDTHH:MM:SS
  • The separator between date and time is T, t or a space.
  • Every field has a fixed width, with leading zeros: 03, not 3.
  • A plain year has four digits. For years before 0 or after 9999, use the ISO 8601 expanded form: a sign and four or more digits, such as -0500 or +12000. Years run from -4799 to 2147483647.
  • The fraction of a second can have any number of digits.
  • Leading and trailing whitespace is ignored.
  • UTC accepts a trailing Z or z. RFC 3339 allows either case for Z and T. The other scales reject Z, because it means UTC.
  • A seconds value of 60 is valid only for UTC, on a day that ends in a leap second.

Everything else is an error. Here’s what the parser says about common mistakes:

use celestial_time::scales::tt::TT;
use celestial_time::scales::utc::UTC;

for s in [
    "2026-03-20 04:30:00.123456789",
    "2026-3-20T04:30:00",
    "2026-03-20T04:30",
    "2026-03-20T04:30:00+02:00",
    "2026-02-30T00:00:00",
    "2026-03-20T24:00:00",
    "2026-03-20",
    "2026-03-20T04:30:00Zx",
    "-4800-01-01T00:00:00",
] {
    println!("{s:?} -> {:?}", s.parse::<UTC>().map(|utc| utc.to_string()));
}
println!("{:?}", "2026-03-20T04:30:00Z".parse::<TT>());
// "2026-03-20 04:30:00.123456789" -> Ok("UTC JD 2461119.5 + 0.18750142889802085")
// "2026-3-20T04:30:00" -> Err(ParseError("Invalid month: '3'"))
// "2026-03-20T04:30" -> Err(ParseError("Invalid time format: '04:30'. Expected HH:MM:SS"))
// "2026-03-20T04:30:00+02:00" -> Err(ParseError("Invalid time format: '04:30:00+02:00'. Expected HH:MM:SS"))
// "2026-02-30T00:00:00" -> Err(InvalidDate("2026-02-30: day is out of range"))
// "2026-03-20T24:00:00" -> Err(ParseError("Hour out of range: 24"))
// "2026-03-20" -> Err(ParseError("Invalid datetime format: '2026-03-20'. Expected YYYY-MM-DDTHH:MM:SS"))
// "2026-03-20T04:30:00Zx" -> Err(ParseError("Unexpected text 'x' after the Z suffix: '2026-03-20T04:30:00Zx'"))
// "-4800-01-01T00:00:00" -> Err(ParseError("Year -4800 is out of range: years must be from -4799 to 2147483647"))
// Err(ParseError("Only UTC takes a Z suffix: '2026-03-20T04:30:00Z'"))

Time zone offsets aren’t supported. Convert the timestamp to UTC before you parse it.

An expanded year parses on every scale, but UTC began in 1960, so only the other scales can hold the year -500:

use celestial_time::scales::tt::TT;
use celestial_time::scales::utc::UTC;

println!("{}", "-0500-03-01T00:00:00".parse::<TT>()?);
println!("{}", "-0500-03-01T00:00:00Z".parse::<UTC>().unwrap_err());
// TT JD 1538497.5 + 0
// Invalid date: -0500-03-01: UTC is not defined before 1960-01-01

Build from calendar fields

Each scale module has a *_from_calendar function that takes year, month, day, hour, minute and seconds:

use celestial_time::scales::tdb::tdb_from_calendar;
use celestial_time::scales::tt::tt_from_calendar;
use celestial_time::scales::utc::utc_from_calendar;

let utc = utc_from_calendar(2026, 3, 20, 4, 30, 0.0)?;
let tt = tt_from_calendar(2026, 3, 20, 4, 30, 0.0)?;
let tdb = tdb_from_calendar(2026, 3, 20, 4, 30, 0.0)?;

The others are tai_from_calendar, gps_from_calendar, ut1_from_calendar, tcg_from_calendar and tcb_from_calendar. Each reads the fields on its own scale.

The year uses astronomical numbering, where year 0 is 1 BC. Dates are Gregorian, back to the year -4799. utc_from_calendar also rejects dates before 1960-01-01.

utc_from_calendar is the one that knows about leap seconds. It accepts 23:59:60 on a leap second day and stretches that day to 86,401 s. The other seven read every day as 86,400 s.

Build from a Unix timestamp

new(seconds, nanos) takes a count of seconds and nanoseconds since 1970-01-01:

use celestial_time::scales::tai::TAI;
use celestial_time::scales::utc::UTC;

let utc = UTC::new(1_774_000_000, 250_000_000)?;
println!("{}", utc.to_iso8601()?);
println!("{:?}", utc.to_unix()?);

let tai = TAI::new(1_774_000_000, 250_000_000);
println!("{tai}");
// 2026-03-20T09:46:40.250
// (1774000000, 250000000)
// TAI JD 2461119.5 + 0.40741030092592595

For UTC, this is Unix time: every day counts as 86,400 s, and the count skips leap seconds. UTC::new returns an error if nanos is a billion or more. to_unix() converts back, and UTC and leap seconds covers what happens inside a leap second.

For the other scales, new reads the count on that scale and can’t fail. TAI::new(0, 0) is 1970-01-01 00:00:00 TAI, not the Unix epoch. Use it when your source counts seconds on that scale, and use UTC::new for an ordinary Unix timestamp.

Read the system clock

use celestial_time::scales::utc::UTC;

let now = UTC::now()?;
println!("{}", now.to_iso8601()?);

UTC::now() reads SystemTime and builds the value with UTC::new. It’s only as accurate as your computer’s clock. System clocks count Unix time, and operating systems handle a leap second in different ways, so a reading taken near one can be off by up to a second.

Format a time

UTC::to_iso8601() writes the calendar date and time, rounded to the millisecond, and shows a leap second as 60:

use celestial_time::scales::utc::UTC;

for s in ["2026-03-20T04:30:00.9996Z", "2016-12-31T23:59:60.25Z", "2026-12-31T23:59:59.9996Z"] {
    let utc: UTC = s.parse()?;
    println!("{}", utc.to_iso8601()?);
}
// 2026-03-20T04:30:01.000
// 2016-12-31T23:59:60.250
// 2027-01-01T00:00:00.000

The output has no Z, and no other scale has to_iso8601. A year after 9999 comes out in the expanded form, such as +10000-01-01T00:00:00.000, so the parser reads every string to_iso8601 writes. Every scale implements Display, which prints the scale name and the two parts of the Julian date. For the calendar date on another scale, julian_to_calendar returns the year, month, day and fraction of the day:

use celestial_time::scales::conversions::utc_tai::julian_to_calendar;
use celestial_time::scales::conversions::{ToTAI, ToTT};
use celestial_time::scales::utc::UTC;

let utc: UTC = "2026-03-20T04:30:00Z".parse()?;
let tt = utc.to_tai()?.to_tt()?;
println!("{tt}");

let jd = tt.to_julian_date();
let (year, month, day, fraction) = julian_to_calendar(jd.jd1(), jd.jd2())?;
println!("{year}-{month:02}-{day:02} + {:.6} s", fraction * 86400.0);
// TT JD 2461119.5 + 0.18830074074074074
// 2026-03-20 + 16269.184000 s

With the serde feature, JulianDate, every scale type and the sidereal types implement Serialize and Deserialize. A date serializes as its two parts.

Errors

Every fallible call returns TimeResult<T>, an alias for Result<T, TimeError>. Both are at the crate root. TimeError has five variants:

VariantReturned for
ParseErrora string that doesn’t match the format
InvalidDatea date or time that doesn’t exist, such as February 30 or 23:59:60 on a normal day, or a UTC date before 1960
ConversionErrora non-finite argument, a Julian date out of the calendar range, or a leap second sent to Unix time
InvalidEpocha non-finite Julian date, or a TT date beyond the IAU models’ 20-century range
CalculationErrora failure reported by a celestial-core model

Where next