Struct jomini::common::Date

source ·
pub struct Date { /* private fields */ }
Expand description

A date without a time component

See RawDate for additional date / time commentary

Implementations§

source§

impl Date

source

pub fn from_ymd_opt(year: i16, month: u8, day: u8) -> Option<Self>

Create a new date from year, month, and day parts

Will return None if the date does not exist

use jomini::common::Date;
assert_eq!(Date::from_ymd_opt(1444, 11, 11), Some(Date::from_ymd(1444, 11, 11)));
assert_eq!(Date::from_ymd_opt(800, 5, 3), Some(Date::from_ymd(800, 5, 3)));
assert!(Date::from_ymd_opt(800, 0, 3).is_none());
assert!(Date::from_ymd_opt(800, 1, 0).is_none());
assert!(Date::from_ymd_opt(800, 13, 1).is_none());
assert!(Date::from_ymd_opt(800, 12, 32).is_none());
assert!(Date::from_ymd_opt(2020, 2, 29).is_none());
source

pub fn from_ymd(year: i16, month: u8, day: u8) -> Self

Create a new date from year, month, and day parts

Will panic if the date does not exist.

source

pub fn parse<T: AsRef<[u8]>>(s: T) -> Result<Self, DateError>

Parses a string and returns a new Date if valid. The expected format is either YYYY.MM.DD or a number representing of the equivalent binary representation.

use jomini::common::{Date, PdsDate};
let date = Date::parse("1444.11.11").expect("to parse date");
assert_eq!(date.year(), 1444);
assert_eq!(date.month(), 11);
assert_eq!(date.day(), 11);
source

pub fn days_until(self, other: &Date) -> i32

Returns the number of days between two dates

use jomini::common::Date;
let date = Date::parse("1400.1.2").unwrap();
let date2 = Date::parse("1400.1.3").unwrap();
let date3 = Date::parse("1401.1.2").unwrap();
let date4 = Date::parse("1401.12.31").unwrap();
assert_eq!(1, date.days_until(&date2));
assert_eq!(365, date.days_until(&date3));
assert_eq!(728, date.days_until(&date4));
assert_eq!(-728, date4.days_until(&date));
source

pub fn add_days(self, days: i32) -> Date

Return a new date that is the given number of days in the future from the current date

use jomini::common::Date;

let date = Date::parse("1400.1.2").unwrap();
let expected = Date::parse("1400.1.3").unwrap();
let expected2 = Date::parse("1400.1.1").unwrap();
assert_eq!(expected, date.add_days(1));
assert_eq!(expected2, date.add_days(-1));

Will panic on overflow or underflow.

source

pub fn from_binary(s: i32) -> Option<Self>

Decodes a date from a number that had been parsed from binary data

The hour component, if present, will be ignored

source

pub fn from_binary_heuristic(s: i32) -> Option<Self>

Decodes a date from a number that had been parsed from binary data with the added check that the date is not too far fetched. This function is useful when working with binary data and it’s not clear with an encountered integer is supposed to represent a date or a number.

We use -100 as a cut off dates for years. Antonio I (EU4) holds the record with a birth date of -58.1.1. The exception is monuments, which date back to -2500 or even farther back (mods), but this function is just a heuristic so direct any extreme dates towards Date::from_binary.

source

pub fn to_binary(self) -> i32

Converts a date into the binary representation

use jomini::common::Date;
let date = Date::from_ymd(1, 1, 1);
assert_eq!(43808760, date.to_binary());

Trait Implementations§

source§

impl Clone for Date

source§

fn clone(&self) -> Date

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Date

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Date

source§

fn deserialize<D>(deserializer: D) -> Result<Date, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl FromStr for Date

§

type Err = DateError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Date

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Date

source§

fn cmp(&self, other: &Date) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Date

source§

fn eq(&self, other: &Date) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Date

source§

fn partial_cmp(&self, other: &Date) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PdsDate for Date

source§

fn year(&self) -> i16

Year of the date

use jomini::common::{Date, PdsDate};
let date = Date::from_ymd(1444, 2, 3);
assert_eq!(date.year(), 1444);
source§

fn month(&self) -> u8

Month of the date

use jomini::common::{Date, PdsDate};
let date = Date::from_ymd(1444, 2, 3);
assert_eq!(date.month(), 2);
source§

fn day(&self) -> u8

Day of the date

use jomini::common::{Date, PdsDate};
let date = Date::from_ymd(1444, 2, 3);
assert_eq!(date.day(), 3);
source§

fn iso_8601(&self) -> PdsDateFormatter

Formats a date in the ISO 8601 format: YYYY-MM-DD

use jomini::common::{Date, PdsDate};
let date = Date::from_ymd(1400, 1, 2);
assert_eq!(date.iso_8601().to_string(), String::from("1400-01-02"));
source§

fn game_fmt(&self) -> PdsDateFormatter

Formats a date in the game format: Y.M.D

use jomini::common::{Date, PdsDate};
let date = Date::from_ymd(1400, 1, 2);
assert_eq!(date.game_fmt().to_string(), String::from("1400.1.2"));
source§

impl Serialize for Date

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Date

source§

impl Eq for Date

source§

impl StructuralPartialEq for Date

Auto Trait Implementations§

§

impl Freeze for Date

§

impl RefUnwindSafe for Date

§

impl Send for Date

§

impl Sync for Date

§

impl Unpin for Date

§

impl UnwindSafe for Date

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,