Date

Struct Date 

Source
pub struct Date {
    pub year: u16,
    pub month: u8,
    pub day: u8,
}
Expand description

A Date

Allowed formats:

  • YYYY-MM-DD

Leap years are correct calculated according to the Gregorian calendar. Thus 2000-02-29 is a valid date, but 2001-02-29 is not.

§Comparison

Date supports equality (==) and inequality (>, <, >=, <=) comparisons.

use speedate::Date;

let d1 = Date::parse_str("2022-01-01").unwrap();
let d2 = Date::parse_str("2022-01-02").unwrap();
assert!(d2 > d1);

Fields§

§year: u16

Year: four digits

§month: u8

Month: 1 to 12

§day: u8

Day: 1 to {28, 29, 30, 31} (based on month & year)

Implementations§

Source§

impl Date

Source

pub fn parse_str_rfc3339(str: &str) -> Result<Self, ParseError>

Parse a date from a string using RFC 3339 format

§Arguments
  • str - The string to parse
§Examples
use speedate::Date;

let d = Date::parse_str_rfc3339("2020-01-01").unwrap();
assert_eq!(
    d,
    Date {
        year: 2020,
        month: 1,
        day: 1
    }
);
assert_eq!(d.to_string(), "2020-01-01");
Source

pub fn parse_str(str: &str) -> Result<Self, ParseError>

Parse a date from a string using RFC 3339 format, or a unix timestamp.

In the input is purely numeric, then the number is interpreted as a unix timestamp, using Date::from_timestamp.

§Arguments
  • str - The string to parse
§Examples
use speedate::Date;

let d = Date::parse_str("2020-01-01").unwrap();
assert_eq!(d.to_string(), "2020-01-01");
let d = Date::parse_str("1577836800").unwrap();
assert_eq!(d.to_string(), "2020-01-01");
Source

pub fn parse_str_with_config( str: &str, config: &DateConfig, ) -> Result<Self, ParseError>

As with Date::parse_str but with a DateConfig.

Source

pub fn parse_bytes_rfc3339(bytes: &[u8]) -> Result<Self, ParseError>

Parse a date from bytes using RFC 3339 format

§Arguments
  • bytes - The bytes to parse
§Examples
use speedate::Date;

let d = Date::parse_bytes_rfc3339(b"2020-01-01").unwrap();
assert_eq!(
    d,
    Date {
        year: 2020,
        month: 1,
        day: 1
    }
);
assert_eq!(d.to_string(), "2020-01-01");
Source

pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError>

Parse a date from bytes using RFC 3339 format, or a unix timestamp.

In the input is purely numeric, then the number is interpreted as a unix timestamp, using Date::from_timestamp.

§Arguments
  • bytes - The bytes to parse
§Examples
use speedate::Date;

let d = Date::parse_bytes(b"2020-01-01").unwrap();
assert_eq!(d.to_string(), "2020-01-01");

let d = Date::parse_bytes(b"1577836800").unwrap();
assert_eq!(d.to_string(), "2020-01-01");
Source

pub fn parse_bytes_with_config( bytes: &[u8], config: &DateConfig, ) -> Result<Self, ParseError>

Same as Date::parse_bytes but with a DateConfig.

Source

pub fn from_timestamp( timestamp: i64, require_exact: bool, config: &DateConfig, ) -> Result<Self, ParseError>

Create a date from a Unix Timestamp in seconds or milliseconds

(“Unix Timestamp” means number of seconds or milliseconds since 1970-01-01)

Input must be between -62,167,219,200,000 (0000-01-01) and 253,402,300,799,000 (9999-12-31) inclusive.

If the absolute value is > 2e10 (20,000,000,000) it is interpreted as being in milliseconds.

That means:

  • 20,000,000,000 is 2603-10-11
  • 20,000,000,001 is 1970-08-20
  • -62,167,219,200,001 gives an error - DateTooSmall as it would be before 0000-01-01
  • -20,000,000,001 is 1969-05-14
  • -20,000,000,000 is 1336-03-23
§Arguments
  • timestamp - timestamp in either seconds or milliseconds
  • require_exact - if true, then the timestamp must be exactly at midnight, otherwise it will be rounded down
§Examples
use speedate::{Date, DateConfig};

let d = Date::from_timestamp(1_654_560_000, true, &DateConfig::default()).unwrap();
assert_eq!(d.to_string(), "2022-06-07");
Source

pub fn timestamp(&self) -> i64

Unix timestamp in seconds (number of seconds between self and 1970-01-01)

§Example
use speedate::Date;

let d = Date::parse_str("2022-06-07").unwrap();
assert_eq!(d.timestamp(), 1_654_560_000);
Source

pub fn timestamp_ms(&self) -> i64

Unix timestamp in milliseconds (number of milliseconds between self and 1970-01-01)

§Example
use speedate::Date;

let d = Date::parse_str("2022-06-07").unwrap();
assert_eq!(d.timestamp_ms(), 1_654_560_000_000);
Source

pub fn today(tz_offset: i32) -> Result<Self, ParseError>

Current date. Internally, this uses DateTime::now.

§Arguments
  • tz_offset - timezone offset in seconds, meaning as per DateTime::now, must be less than 86_400
§Example
use speedate::Date;

let d = Date::today(0).unwrap();
println!("The date today is: {}", d)
Source

pub fn ordinal_day(&self) -> u16

Day of the year, starting from 1.

Trait Implementations§

Source§

impl Clone for Date

Source§

fn clone(&self) -> Date

Returns a duplicate 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 Display for Date

Source§

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

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

impl FromStr for Date

Source§

type Err = ParseError

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 PartialEq for Date

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.