pub trait Calendar {
// Required method
fn is_gregorian() -> bool;
// Provided methods
fn name() -> &'static str { ... }
fn epoch() -> Instant { ... }
fn is_year_leap(year: i32) -> bool { ... }
fn day_number(year: i32, month: u8, day: i64) -> Result<i64, Error> { ... }
fn from_day_number(day_number: i64) -> Result<(i32, u8, u8), Error> { ... }
fn month_days(month: u8, year: i32) -> u8 { ... }
}Expand description
This specifies traditional Calendar settings that use the traditional 12 months
and have leap years. This is implemented for Gregorian and Julian. It does
not handle more esoteric calendars.
Zero and Negative years are handled in accordance with ISO 8601, such that year 0 is 1 B.C., and year -1 is 2 B.C., etc. In general:
- n B.C. is represented by year 1-n
- Year -y represents year y+1 B.C. (for positive y).
Required Methods§
Sourcefn is_gregorian() -> bool
fn is_gregorian() -> bool
If the calendar is Gregorian (since we only handle Julian and Gregorian, this is all that needs to be defined to differentiate them)
Provided Methods§
Sourcefn is_year_leap(year: i32) -> bool
fn is_year_leap(year: i32) -> bool
Answers the question: is this year a leap year?
Sourcefn day_number(year: i32, month: u8, day: i64) -> Result<i64, Error>
fn day_number(year: i32, month: u8, day: i64) -> Result<i64, Error>
Converts a year, month and (month)day into a day number which counts the number
of days from the start of the calendar epoch
year may range from -2147483648 .. 2147483647 covering every possible i32.
month must be in the range 1 .. 12
day may be out of the normal bounds. It will be adjusted.
§Errors
Will return a Error::RangeError if month or day are out of range.
Sourcefn from_day_number(day_number: i64) -> Result<(i32, u8, u8), Error>
fn from_day_number(day_number: i64) -> Result<(i32, u8, u8), Error>
Converts a day number which counts the number of days from the start of the calendar epoch into a year, month and day
For the Gregorian calendar, day_number must fall in the range
-784_352_296_671 .. 784_352_295_938
which represent calendar dates -2147483648-01-01 .. 2147483647-12-31
respectively.
For the Julian calendar, day_number must fall in the range
-784_368_402_798 .. 784_368_402_065
which represent calendar dates -2147483648-01-01 .. 2147483647-12-31
respectively.
Returns a (year, month, day)
§Errors
Will return a Error::RangeError if day_number is out of range.
§Panics
Panics on assertions that should only fail if there is a bug.
Sourcefn month_days(month: u8, year: i32) -> u8
fn month_days(month: u8, year: i32) -> u8
Returns the number of days in a given month (year is required for leap year calculations)
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.