Skip to main content

Crate nepdate

Crate nepdate 

Source
Expand description

NepDate logo

nepdate

nepdate on crates.io docs.rs CI License: MIT

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

FeatureDefaultWhat it adds
stdyesstd::error::Error for Error, and to_string() through Display
serdenoSerialize and Deserialize for NepaliDate as "YYYY-MM-DD"
chrononoFrom<NepaliDate> for chrono::NaiveDate and TryFrom<chrono::NaiveDate>
jiffnoFrom<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) / TryParses.parse::<NepaliDate>() (strict) or NepaliDate::parse_lenient(s)
date.EnglishDatedate.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
MonthEndDaymonth_length()
DayOfWeekweekday()
ToString(format)Display (YYYY/MM/DD), short(..), long(..) and format_pattern(pattern, lang)
IsTodayis_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

TopicWhere
What changed in each versionCHANGELOG.md
Speed against C# NepDate and other cratesbenchmarks
Reporting a vulnerabilitySECURITY.md
A wrong date, holiday or tithithe hub’s data error form
The other languagessanamhub/nepdate

Structs§

DateDiff
The calendar distance between two dates (ALGORITHM §8a), from NepaliDate::diff.
Days
The days of a NepaliDateRange, from NepaliDateRange::days.
Digits
A number written in a Lang, from Lang::digits.
FiscalLabel
A fiscal year’s label in a Lang, from FiscalYear::label.
FiscalYear
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.
GregorianDate
A valid proleptic Gregorian (AD) date, for any i32 year.
LongDate
Options for NepaliDate::long (FORMATTING §4). The default matches C# ToLongDateString(): no weekday, with the year, day padded to 2 digits.
LongFormat
A date with the month name, from NepaliDate::long (FORMATTING §4).
NepaliDate
A valid Bikram Sambat date from 1901-01-01 to 2199-12-30.
NepaliDateRange
The days from start to end, both included. Never empty: start <= end.
PatternFormat
A date written by a pattern such as yyyy-MM-dd, from NepaliDate::format_pattern (FORMATTING §5).
ShortFormat
A date as three numbers, from NepaliDate::short (FORMATTING §3).

Enums§

DateOrder
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.
MonthOverflow
What NepaliDate::checked_add_months does when the day doesn’t exist in the target month.
ParseErrorKind
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.