jwodder / julian-rs

Convert between Julian day numbers and Julian & Gregorian calendars
MIT License
3 stars 1 forks source link
available-on-crates-io calendar gregorian-calendar julian-calendar julian-day rust

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io Minimum Supported Rust Version MIT License

GitHub | crates.io | Documentation | Issues | Changelog

julian is a Rust library for converting between Julian day numbers and dates in the Gregorian calendar (either proleptic or with the Reformation occurring at a given date) and/or the proleptic Julian calendar. There are also features for querying details about years & months in a "reforming" Gregorian calendar and how they are affected by the calendar reformation date of your choice.

Examples

Before you construct a date, you must first choose a calendar in which to reckon dates. Calendar::GREGORIAN is the proleptic Gregorian calendar, which should be both simple and useful enough for most basic purposes.

To convert a Julian day number to a date in a calendar, use the Calendar::at_jdn() method, like so:

use julian::{Calendar, Month};

let cal = Calendar::GREGORIAN;
let date = cal.at_jdn(2460065);
assert_eq!(date.year(), 2023);
assert_eq!(date.month(), Month::April);
assert_eq!(date.day(), 30);

So JDN 2460065 is April 30, 2023, in the proleptic Gregorian calendar.

To convert a date to a Julian day number, use Calendar::at_ymd() to construct the date, and then call its julian_day_number() method:

use julian::{Calendar, Month};

let cal = Calendar::GREGORIAN;
let date = cal.at_ymd(2023, Month::April, 30).unwrap();
assert_eq!(date.julian_day_number(), 2460065);

See the documentation for more things you can do!

Command

julian also provides a command of the same name for converting between Julian day numbers and dates in Julian-style calendars. To install this command on your system, run:

cargo install julian

Usage

julian [<options>] [<date> ...]

When invoked without arguments, the julian command displays the current date in the proleptic Gregorian calendar & UTC timezone along with the corresponding Julian day number.

When julian is invoked with one or more arguments, any calendar date arguments (in the form "YYYY-MM-DD" or "YYYY-JJJ") are converted to Julian day numbers, and any integer arguments are treated as Julian day numbers and converted to calendar dates. By default, dates are read & written using the proleptic Gregorian calendar, but this can be changed with the --julian or --reformation option.

julian uses astronomical year numbering, where 1 BC (the year immediately before AD 1) is denoted on input & output as year 0 (displayed as "0000"), and the year before that (normally called 2 BC) is denoted -1 (displayed as "-0001"). In addition, the start of the year is always taken as being on January 1, even though not all users of the Julian calendar throughout history have followed this convention.

Options

JSON Output

When julian is invoked with the -J/--json option, it outputs a JSON breakdown of the chosen calendar and input & output values. The output structure is an object with two keys, "calendar" and "dates".