Expand description
nepdate
Bikram Sambat (BS, the Nepali calendar) dates for Rust: conversion to and from the Gregorian
calendar (AD), arithmetic, fiscal years, formatting in English or Nepali, and parsing. no_std,
no dependencies by default, no heap allocation, no unsafe.
Tithi, public holidays and events are in the companion crate
nepdate-patro, so this crate carries no patro data.
Status: 0.1.0-beta.1, the first public pre-release. The API may still change before 0.1.0.
§Quick start
[dependencies]
nepdate = "=0.1.0-beta.1"use nepdate::{Lang, LongDate, MonthOverflow, NepaliDate, Weekday};
// BS to AD.
let date = NepaliDate::new(2081, 4, 15)?;
let ad = date.to_gregorian();
assert_eq!((ad.year(), ad.month(), ad.day()), (2024, 7, 30));
assert_eq!(date.weekday(), Weekday::Tuesday);
// AD to BS.
assert_eq!(NepaliDate::from_gregorian(2024, 7, 30)?, date);
// Arithmetic never panics: out of range gives an error.
assert_eq!(date.checked_add_days(1000)?.to_string(), "2084/01/13");
assert_eq!(
date.checked_add_months(13, MonthOverflow::Clamp)?.to_string(),
"2082/05/15"
);
// Formatting and parsing.
assert_eq!(date.to_string(), "2081/04/15");
assert_eq!(
date.long(LongDate::default(), Lang::En).to_string(),
"Shrawan 15, 2081"
);
assert_eq!("2081/04/15".parse::<NepaliDate>()?, date);
assert_eq!(NepaliDate::parse_lenient("15 Shrawan 2081")?, date);Every formatter (short, long, format_pattern, Lang::digits, FiscalYear::label) has a
write_to method that writes into any core::fmt::Write without allocating; to_string() needs
std.
Two runnable examples are in the repository:
convert
(cargo run --example convert --features jiff -- 2081/04/15) and
month_calendar
(cargo run --example month_calendar -- 2081 4).
§Supported range
BS 1901-01-01 to 2199-12-30 (AD 1844-04-11 to 2143-04-15). Every function returns
Error::OutOfRange outside it. Month lengths for years beyond the officially published
calendar are projections inherited from NepDate and may change in a later release
(known data questions).
§Features
| Feature | Default | What it adds |
|---|---|---|
std | yes | std::error::Error for Error, and to_string() through Display |
serde | no | Serialize and Deserialize for NepaliDate as "YYYY-MM-DD" |
chrono | no | From<NepaliDate> for chrono::NaiveDate and TryFrom<chrono::NaiveDate> |
jiff | no | From<NepaliDate> for jiff::civil::Date, TryFrom<jiff::civil::Date>, NepaliDate::today() (the date in Asia/Kathmandu), today_in(&TimeZone) and is_today(). Turns on std |
Without default features the crate builds for wasm32-unknown-unknown and other targets with no
standard library.
§Coming from C# NepDate
| C# | Rust |
|---|---|
new NepaliDate(y, m, d) (throws) | NepaliDate::new(y, m, d) returns Result<NepaliDate, Error> and is a const fn |
NepaliDate.Parse(s) / TryParse | s.parse::<NepaliDate>() (strict) or NepaliDate::parse_lenient(s) |
date.EnglishDate | date.to_gregorian(), or chrono::NaiveDate::from(date) with feature chrono |
DateTime.ToNepaliDate() | NepaliDate::from_gregorian(y, m, d), or NepaliDate::try_from(naive_date) |
AddDays(n) | checked_add_days(n) |
AddMonths(n, awayFromMonthEnd) | checked_add_months(n, MonthOverflow::Clamp) or MonthOverflow::Overflow |
MonthEndDay | month_length() |
DayOfWeek | weekday() |
ToString(format) | Display (YYYY/MM/DD), short(..), long(..) and format_pattern(pattern, lang) |
IsToday | is_today() with feature jiff |
The places where this crate deliberately behaves differently from C# NepDate are listed in PARITY.md.
§Minimum supported Rust version
Rust 1.85 (edition 2024). Raising it is a minor version bump.
§Data and license
The calendar data and conversion algorithm come from
NepDate by Raju Prasai and contributors, used under the
MIT License. Its notice ships in this crate as LICENSE-THIRD-PARTY. The shared data and the
specification are maintained in sanamhub/nepdate.
This crate is MIT licensed (LICENSE). It is not affiliated with the Government of Nepal.
§More
| Topic | Where |
|---|---|
| What changed in each version | CHANGELOG.md |
| Speed against C# NepDate and other crates | benchmarks |
| Reporting a vulnerability | SECURITY.md |
| A wrong date, holiday or tithi | the hub’s data error form |
| The other languages | sanamhub/nepdate |
Structs§
- Date
Diff - The calendar distance between two dates (ALGORITHM §8a), from
NepaliDate::diff. - Days
- The days of a
NepaliDateRange, fromNepaliDateRange::days. - Digits
- A number written in a
Lang, fromLang::digits. - Fiscal
Label - A fiscal year’s label in a
Lang, fromFiscalYear::label. - Fiscal
Year - A fiscal year, named by the BS year it starts in: FY 2082/83 is
FiscalYear::new(2082)and runs from 2082-04-01 to the last day of 2083-03. - Gregorian
Date - A valid proleptic Gregorian (AD) date, for any
i32year. - Long
Date - Options for
NepaliDate::long(FORMATTING §4). The default matches C#ToLongDateString(): no weekday, with the year, day padded to 2 digits. - Long
Format - A date with the month name, from
NepaliDate::long(FORMATTING §4). - Nepali
Date - A valid Bikram Sambat date from 1901-01-01 to 2199-12-30.
- Nepali
Date Range - The days from
starttoend, both included. Never empty:start <= end. - Pattern
Format - A date written by a pattern such as
yyyy-MM-dd, fromNepaliDate::format_pattern(FORMATTING §5). - Short
Format - A date as three numbers, from
NepaliDate::short(FORMATTING §3).
Enums§
- Date
Order - The order of year, month and day in
NepaliDate::short. Same order as C#DateFormats. - Error
- Why a date couldn’t be built, converted or parsed.
- Lang
- Output language for names and digits.
- Month
- A BS month. The discriminant is the month number, 1 to 12.
- Month
Overflow - What
NepaliDate::checked_add_monthsdoes when the day doesn’t exist in the target month. - Parse
Error Kind - Why a text couldn’t be parsed (PARSING).
- Quarter
- A quarter of the fiscal year: Q1 is Shrawan to Ashoj, Q4 is Baishakh to Ashad (D-08).
- Separator
- The character between the numbers of
NepaliDate::short. - Weekday
- A day of the week. The discriminant counts from Sunday = 0, as in Nepal’s week.