| Crates.io | astronomical-calculator |
| lib.rs | astronomical-calculator |
| version | 0.4.0 |
| created_at | 2025-12-26 05:29:52.058109+00 |
| updated_at | 2026-01-18 03:41:05.365952+00 |
| description | High-precision astronomical calculations for solar position, sunrise/sunset times, and related phenomena |
| homepage | |
| repository | https://github.com/dickermoshe/astronomical-calculator |
| max_upload_size | |
| id | 2005294 |
| size | 387,550 |
A high-precision Rust library for calculating solar position, sunrise/sunset times, and related astronomical phenomena.
This library calculates the position of the Sun with high precision for any given time and location on Earth. It provides accurate solar position calculations (zenith and azimuth angles), sunrise and sunset times, solar transit (solar noon), solar midnight, and various twilight events (civil, nautical, and astronomical dawn/dusk).
The library accounts for atmospheric refraction, parallax, nutation, aberration, and other astronomical phenomena that affect solar position calculations. It is based on VSOP87 theory and uses the same algorithms as the freespa library, which this is a Rust port of.
The library is no_std compatible, making it suitable for embedded systems and other constrained environments. It has been extensively tested using property-based testing with thousands of randomized inputs, and validated against both the original C implementation (freespa) and the Skyfield Python library to ensure accuracy and reliability.
no_std Compatible: Works in embedded and constrained environmentsuse astronomical_calculator::{AstronomicalCalculator, Refraction};
use chrono::NaiveDateTime;
// Create a datetime (UTC)
let dt = NaiveDateTime::parse_from_str("2024-06-21 12:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
// Create calculator for London (51.5°N, 0°E)
let mut calc = AstronomicalCalculator::new(
dt,
None, // Calculate ΔT automatically
0.0, // ΔUT1 (use 0.0 if unknown)
0.0, // longitude in degrees (0° = Greenwich)
51.5, // latitude in degrees (positive = North)
0.0, // elevation in meters
20.0, // temperature in Celsius
1013.25, // pressure in millibars
None, // geometric dip (None = standard horizon)
Refraction::ApSolposBennetNA, // refraction model
).unwrap();
// Get current solar position
let position = calc.get_solar_position();
println!("Zenith: {:.2}°", position.zenith.to_degrees());
println!("Azimuth: {:.2}°", position.azimuth.to_degrees());
// Get sunrise and sunset times
use astronomical_calculator::SolarEventResult;
match calc.get_sunrise().unwrap() {
SolarEventResult::Occurs(timestamp) => {
println!("Sunrise at Unix timestamp: {}", timestamp);
}
SolarEventResult::AllDay => println!("Sun never sets (midnight sun)"),
SolarEventResult::AllNight => println!("Sun never rises (polar night)"),
}
Important: When calculating solar events (sunrise, sunset, twilight), the input time should be close to local noon for the given location. This ensures that events are calculated for the correct solar day. Using times far from noon may result in events being calculated for the wrong day.
Add the library to your Cargo.toml:
cargo add astronomical-calculator chrono
Or add it manually:
[dependencies]
astronomical-calculator = "0.1.0"
chrono = { version = "0.4.42", default-features = false }
Note: The chrono dependency is required for datetime handling. If you're using this in a no_std environment, ensure chrono is configured without the std feature.
The library provides high-precision calculations based on:
The library is valid for dates from 2000 BC to 6000 CE. For dates within the historical range (1657-2024), ΔT values are interpolated from historical data. For dates outside this range, polynomial approximations are used.
This library is extensively tested using property-based testing with thousands of randomized inputs. The test suite validates results against:
Testing covers diverse scenarios including:
For detailed API documentation, usage examples, and advanced topics, see the documentation on docs.rs.
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.
get_sunrise_offset_by_degrees and get_sunset_offset_by_degrees do not use the provided geometric dip, instead they use the geometric horizon.Debug and Clone to AstronomicalCalculatorgeometric parameter to get_sunrise_offset_by_degrees and get_sunset_offset_by_degrees