2 stable releases
Uses new Rust 2024
| 1.0.1 | Sep 14, 2025 |
|---|
#321 in Math
33KB
549 lines
PrayTime - Prayer Times Calculator (Rust)
A Rust implementation of the PrayTimes.org library for calculating Islamic prayer times. This crate provides accurate prayer time calculations based on astronomical formulas and supports multiple calculation methods used around the world.
Features
- Multiple Calculation Methods: Supports 10 different calculation methods including ISNA, MWL, Egypt, Makkah, Karachi, Tehran, Jafari, France, Russia, and Singapore
- Flexible Location Support: Works with latitude/longitude coordinates
- Timezone Support: Full timezone support using chrono-tz or UTC offset
- Multiple Time Formats: 24-hour, 12-hour with/without AM/PM, and Unix timestamps
- High Latitude Adjustments: Special handling for locations at high latitudes where normal calculations may not work
- Customizable Parameters: Tune individual prayer times, adjust calculation parameters
- ASR Methods: Support for both Standard and Hanafi ASR calculation methods
- Serde Support: Serialize and deserialize prayer times
- Clean API: Builder pattern for easy configuration
Installation
Add this to your Cargo.toml:
[dependencies]
praytime-rs = "1.0.0"
Quick Start
use praytime_rs::PrayTime;
let mut praytime = PrayTime::new("ISNA");
praytime.location(43.6532, -79.3832) // Toronto coordinates
.timezone("America/Toronto");
let times = praytime.get_times(None);
println!("Fajr: {}", times.fajr);
println!("Dhuhr: {}", times.dhuhr);
println!("Asr: {}", times.asr);
println!("Maghrib: {}", times.maghrib);
println!("Isha: {}", times.isha);
Calculation Methods
| Method | Description | Fajr Angle | Isha Angle/Time |
|---|---|---|---|
| ISNA | Islamic Society of North America | 15° | 15° |
| MWL | Muslim World League | 18° | 17° |
| Egypt | Egyptian General Authority of Survey | 19.5° | 17.5° |
| Makkah | Umm Al-Qura University, Makkah | 18.5° | 90 min |
| Karachi | University of Islamic Sciences, Karachi | 18° | 18° |
| Tehran | Institute of Geophysics, University of Tehran | 17.7° | 4.5° + Jafari |
| Jafari | Shia Ithna-Ashari (Jafari) | 16° | 4° + Jafari |
| France | France | 12° | 12° |
| Russia | Russia | 16° | 15° |
| Singapore | Singapore | 20° | 18° |
Advanced Usage
Custom Date and Location
use praytime_rs::{PrayTime, TimeFormat};
use chrono::NaiveDate;
let mut praytime = PrayTime::new("ISNA");
praytime.location(21.4225, 39.8262) // Makkah coordinates
.timezone("Asia/Riyadh")
.format(TimeFormat::Hour12);
let date = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
let times = praytime.get_times(Some(date));
Using UTC Offset
let mut praytime = PrayTime::new("MWL");
praytime.location(40.7128, -74.0060) // New York coordinates
.utc_offset(-300); // -5 hours in minutes
Hanafi ASR Method
use praytime_rs::{PrayTime, AsrMethod};
let mut praytime = PrayTime::new("ISNA");
praytime.location(43.6532, -79.3832)
.asr_method(AsrMethod::Hanafi);
High Latitude Adjustments
use praytime_rs::{PrayTime, HighLatitudeRule};
let mut praytime = PrayTime::new("ISNA");
praytime.location(59.3293, 18.0686) // Stockholm coordinates
.high_latitude_rule(HighLatitudeRule::NightMiddle);
Time Adjustments (Tuning)
use praytime_rs::TuneAdjustments;
// Add 2 minutes to Fajr, subtract 1 minute from Isha
praytime.tune_with(TuneAdjustments {
fajr: 2.0,
sunrise: 0.0,
dhuhr: 0.0,
asr: 0.0,
sunset: 0.0,
maghrib: 0.0,
isha: -1.0,
midnight: 0.0,
});
Different Time Formats
use praytime_rs::{TimeFormat, RoundingMethod};
praytime.format(TimeFormat::Hour12)
.rounding(RoundingMethod::Nearest);
API Reference
Main Struct
PrayTime::new(method: &str)- Create new instance with calculation methodlocation(lat: f64, lng: f64)- Set coordinatestimezone(tz: &str)- Set timezone (e.g., "America/Toronto")utc_offset(minutes: i32)- Set UTC offset in minutesget_times(date: Option<NaiveDate>)- Calculate prayer times
Configuration Methods
format(format: TimeFormat)- Set time formatrounding(method: RoundingMethod)- Set rounding methodasr_method(method: AsrMethod)- Set ASR calculation methodhigh_latitude_rule(rule: HighLatitudeRule)- Set high latitude adjustmenttune_with(TuneAdjustments)- Fine-tune individual prayer times
Enums
TimeFormat
Hour24- 24-hour format (default)Hour12- 12-hour with AM/PMHour12NoSuffix- 12-hour without AM/PMTimestamp- Unix timestamp
RoundingMethod
Nearest- Round to nearest minute (default)Up- Round upDown- Round downNone- No rounding
AsrMethod
Standard- Shafi, Maliki, Hanbali (default)Hanafi- Hanafi school
HighLatitudeRule
NightMiddle- Middle of night (default)OneSeventh- 1/7th of nightAngleBased- Angle-based methodNone- No adjustment
Examples
Run the example to see the library in action:
cargo run --example basic_usage
Testing
Run the test suite:
cargo test
License
This project is licensed under the Code Waqf License (Lisensi Wakaf Kode) - a custom Islamic license that permits free use while prohibiting usage for activities that contradict Islamic principles.
The Code Waqf License template is available at: https://gitlab.com/GwNih/templat-lisensi-wakaf
See the LICENSE file for the complete license text.
Credits
- Original JavaScript library: PrayTimes.org by Hamid Zarrabi-Zadeh
- Rust conversion: Gandi Wibowo
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
Version 1.0.0
- Initial Rust implementation
- Full feature parity with JavaScript version 3.2
- Serde support for serialization
- Comprehensive test suite
- Multiple examples and documentation
- Licensed under Code Waqf License (Lisensi Wakaf Kode)
Dependencies
~9.5MB
~118K SLoC