[][src]Struct datetime_string::common::Ymd8HyphenString

#[repr(transparent)]pub struct Ymd8HyphenString(_);

Owned string for a date in YYYY-MM-DD format, such as 2001-12-31.

This is also an RFC 3339 full-date string.

This is a fixed length string, and implements Copy trait.

To create a value of this type, use str::parse method or std::convert::TryFrom trait, or convert from &Ymd8HyphenStr.

Examples

use datetime_string::common::Ymd8HyphenStr;
use std::convert::TryFrom;

let try_from = Ymd8HyphenString::try_from("2001-12-31")?;

let parse = "2001-12-31".parse::<Ymd8HyphenString>()?;
let parse2: Ymd8HyphenString = "2001-12-31".parse()?;

let to_owned = Ymd8HyphenStr::from_str("2001-12-31")?.to_owned();
let into: Ymd8HyphenString = Ymd8HyphenStr::from_str("2001-12-31")?.into();

Implementations

impl Ymd8HyphenString[src]

pub fn from_ym0d(year: u16, month0: u8, mday: u8) -> Result<Self, Error>[src]

Creates a new Ymd8HyphenString from the given date.

Note that month0 is 0-based, i.e. January is 0, February is 1, and so on.

Examples

let date = Ymd8HyphenString::from_ym0d(2001, 11, 31)?;
assert_eq!(date.as_str(), "2001-12-31");

assert!(Ymd8HyphenString::from_ym0d(2001, 1, 29).is_err(), "2001-02-29 is invaild date");

pub fn from_ym1d(year: u16, month1: u8, mday: u8) -> Result<Self, Error>[src]

Creates a new Ymd8HyphenString from the given date.

Note that month1 is 1-based, i.e. January is 1, February is 2, and so on.

Examples

let date = Ymd8HyphenString::from_ym1d(2001, 12, 31)?;
assert_eq!(date.as_str(), "2001-12-31");

assert!(Ymd8HyphenString::from_ym1d(2001, 2, 29).is_err(), "2001-02-29 is invaild date");

#[must_use]pub fn as_deref(&self) -> &Ymd8HyphenStr[src]

Returns a &Ymd8HyphenStr for the string.

Examples

use datetime_string::common::Ymd8HyphenStr;

let date = "2001-12-31".parse::<Ymd8HyphenString>()?;

// Usually you don't need to call `as_deref()` explicitly, because
// `Deref<Target = Ymd8HyphenStr>` trait is implemented.
let _: &Ymd8HyphenStr = date.as_deref();

#[must_use]pub fn as_deref_mut(&mut self) -> &mut Ymd8HyphenStr[src]

Returns a &mut Ymd8HyphenStr for the string.

Examples

use datetime_string::common::Ymd8HyphenStr;

let mut date = "2001-12-31".parse::<Ymd8HyphenString>()?;

// Usually you don't need to call `as_deref_mut()` explicitly, because
// `DerefMut` trait is implemented.
let _: &mut Ymd8HyphenStr = date.as_deref_mut();

Methods from Deref<Target = Ymd8HyphenStr>

pub fn assign(&mut self, v: &Self)[src]

Assigns the given value.

Examples

let mut buf: [u8; 10] = *b"1999-12-31";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "1999-12-31");

let newdate = Ymd8HyphenStr::from_str("2000-01-01")?;

date.assign(newdate);
assert_eq!(date.as_str(), "2000-01-01");
assert_eq!(buf, *b"2000-01-01");

#[must_use]pub fn as_str(&self) -> &str[src]

Returns a string slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.as_str(), "2001-12-31");

#[must_use]pub fn as_bytes(&self) -> &[u8][src]

Returns a byte slice.

If you want to use indexed access, prefer as_bytes_fixed_len.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.as_bytes(), b"2001-12-31");

#[must_use]pub fn as_bytes_fixed_len(&self) -> &[u8; 10][src]

Returns a fixed length byte slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

let fixed_len: &[u8; 10] = date.as_bytes_fixed_len();
assert_eq!(fixed_len, b"2001-12-31");

#[must_use]pub fn year_str(&self) -> &str[src]

Returns the year as a string slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.year_str(), "2001");

#[must_use]pub fn year_bytes_fixed_len(&self) -> &[u8; 4][src]

Returns the year as a fixed length byte slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

let year_fixed_len: &[u8; 4] = date.year_bytes_fixed_len();
assert_eq!(year_fixed_len, b"2001");

#[must_use]pub fn year(&self) -> u16[src]

Returns the year as an integer.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.year(), 2001);

#[must_use]pub fn month_str(&self) -> &str[src]

Returns the month as a string slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.month_str(), "12");

#[must_use]pub fn month_bytes_fixed_len(&self) -> &[u8; 2][src]

Returns the month as a fixed length byte slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

let month_fixed_len: &[u8; 2] = date.month_bytes_fixed_len();
assert_eq!(month_fixed_len, b"12");

#[must_use]pub fn month1(&self) -> u8[src]

Returns the 1-based month as an integer.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.month1(), 12);

#[must_use]pub fn month0(&self) -> u8[src]

Returns the 0-based month as an integer.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.month0(), 11);

#[must_use]pub fn mday_str(&self) -> &str[src]

Returns the day of month as a string slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.mday_str(), "31");

#[must_use]pub fn mday_bytes_fixed_len(&self) -> &[u8; 2][src]

Returns the day of month as a fixed length byte slice.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

let mday_fixed_len: &[u8; 2] = date.mday_bytes_fixed_len();
assert_eq!(mday_fixed_len, b"31");

#[must_use]pub fn mday(&self) -> u8[src]

Returns the day of month as an integer.

Examples

let date = Ymd8HyphenStr::from_str("2001-12-31")?;

assert_eq!(date.mday(), 31);

pub fn set_year(&mut self, year: u16) -> Result<(), Error>[src]

Sets the given year to the string.

Failures

  • Fails if year is greater than 9999.
  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2000-02-29";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2000-02-29");

date.set_year(2004)?;
assert_eq!(date.as_str(), "2004-02-29");

assert!(date.set_year(2001).is_err(), "2001-02-29 is invalid");

pub fn set_month0(&mut self, month0: u8) -> Result<(), Error>[src]

Sets the given 0-based month value to the string.

Failures

  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-12-31";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-12-31");

date.set_month0(7)?;
assert_eq!(date.as_str(), "2001-08-31");

assert!(date.set_month0(8).is_err(), "2001-09-31 is invalid");

pub fn set_month1(&mut self, month1: u8) -> Result<(), Error>[src]

Sets the given 1-based month value to the string.

Failures

  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-12-31";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-12-31");

date.set_month1(8)?;
assert_eq!(date.as_str(), "2001-08-31");

assert!(date.set_month1(9).is_err(), "2001-09-31 is invalid");

pub fn set_mday(&mut self, mday: u8) -> Result<(), Error>[src]

Sets the given day of month value to the string.

Failures

  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-02-28";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-02-28");

date.set_mday(3)?;
assert_eq!(date.as_str(), "2001-02-03");

assert!(date.set_mday(29).is_err(), "2001-02-29 is invalid");

pub fn set_month0_mday(&mut self, month0: u8, mday: u8) -> Result<(), Error>[src]

Sets the given 0-based month and 1-based day of month values to the string.

Failures

  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-02-28";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-02-28");

date.set_month0_mday(3, 23)?;
assert_eq!(date.as_str(), "2001-04-23");

assert!(date.set_month0_mday(1, 29).is_err(), "2001-02-29 is invalid");

pub fn set_month1_mday(&mut self, month1: u8, mday: u8) -> Result<(), Error>[src]

Sets the given 1-based month and day of month values to the string.

Failures

  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-02-28";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-02-28");

date.set_month1_mday(4, 23)?;
assert_eq!(date.as_str(), "2001-04-23");

assert!(date.set_month1_mday(2, 29).is_err(), "2001-02-29 is invalid");

pub fn set_ym0d(&mut self, year: u16, month0: u8, mday: u8) -> Result<(), Error>[src]

Sets the given 1-based year, 0-based month, and 1-based day of month values to the string.

Failures

  • Fails if year is greater than 9999.
  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-02-28";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-02-28");

date.set_ym0d(1999, 3, 23)?;
assert_eq!(date.as_str(), "1999-04-23");

assert!(date.set_ym0d(1999, 1, 29).is_err(), "1999-02-29 is invalid");

pub fn set_ym1d(&mut self, year: u16, month1: u8, mday: u8) -> Result<(), Error>[src]

Sets the given 1-based year, month, and day of month values to the string.

Failures

  • Fails if year is greater than 9999.
  • Fails if the datetime after modification is invalid.

Examples

let mut buf: [u8; 10] = *b"2001-02-28";
let date = Ymd8HyphenStr::from_bytes_mut(&mut buf[..])?;
assert_eq!(date.as_str(), "2001-02-28");

date.set_ym1d(1999, 4, 23)?;
assert_eq!(date.as_str(), "1999-04-23");

assert!(date.set_ym1d(1999, 2, 29).is_err(), "1999-02-29 is invalid");

pub fn yday0(&self) -> u16[src]

Returns the 0-based day of the year, i.e. days since January 1 of the year.

Note that this value is 0-based. January 1 is 0 days since January 1 of the year.

Examples

let date = Ymd8HyphenStr::from_str("1970-01-01")?;
assert_eq!(date.yday0(), 0, "0 for the 1st day of the year, because this is 0-based value");

let date2 = Ymd8HyphenStr::from_str("1970-12-31")?;
assert_eq!(date2.yday0(), 364);

let leap_last = Ymd8HyphenStr::from_str("2000-12-31")?;
assert_eq!(leap_last.yday0(), 365, "2000-02-29 exists");

pub fn yday1(&self) -> u16[src]

Returns the 1-based day of the year.

Note that this value is 1-based. January 1 is 1st day of the year.

Examples

let date = Ymd8HyphenStr::from_str("1970-01-01")?;
assert_eq!(date.yday1(), 1, "1 for the 1st day of the year, because this is 1-based value");

let date2 = Ymd8HyphenStr::from_str("1970-12-31")?;
assert_eq!(date2.yday1(), 365);

let leap_last = Ymd8HyphenStr::from_str("2000-12-31")?;
assert_eq!(leap_last.yday1(), 366, "2000-02-29 exists");

pub fn days_since_epoch(&self) -> i32[src]

Returns the days from the epoch (1970-01-01).

Examples

let date = Ymd8HyphenStr::from_str("1971-01-01")?;
assert_eq!(date.days_since_epoch(), 365);

let date2 = Ymd8HyphenStr::from_str("1969-01-01")?;
assert_eq!(date2.days_since_epoch(), -365);

let epoch = Ymd8HyphenStr::from_str("1970-01-01")?;
assert_eq!(epoch.days_since_epoch(), 0);

Trait Implementations

impl AsMut<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl AsRef<[u8]> for Ymd8HyphenString[src]

impl AsRef<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl AsRef<str> for Ymd8HyphenString[src]

impl Borrow<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl BorrowMut<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl Clone for Ymd8HyphenString[src]

impl Copy for Ymd8HyphenString[src]

impl Debug for Ymd8HyphenString[src]

impl Deref for Ymd8HyphenString[src]

type Target = Ymd8HyphenStr

The resulting type after dereferencing.

impl DerefMut for Ymd8HyphenString[src]

impl<'de> Deserialize<'de> for Ymd8HyphenString[src]

impl Display for Ymd8HyphenString[src]

impl Eq for Ymd8HyphenString[src]

impl<'_> From<&'_ Ymd8HyphenStr> for Ymd8HyphenString[src]

impl From<Ymd8HyphenString> for Vec<u8>[src]

impl From<Ymd8HyphenString> for String[src]

impl FromStr for Ymd8HyphenString[src]

type Err = Error

The associated error which can be returned from parsing.

impl Hash for Ymd8HyphenString[src]

impl Ord for Ymd8HyphenString[src]

impl<'_> PartialEq<&'_ [u8]> for Ymd8HyphenString[src]

impl<'_> PartialEq<&'_ Ymd8HyphenStr> for Ymd8HyphenString[src]

impl<'_> PartialEq<&'_ Ymd8HyphenString> for Ymd8HyphenString[src]

impl<'_> PartialEq<&'_ Ymd8HyphenString> for str[src]

impl<'_> PartialEq<&'_ Ymd8HyphenString> for [u8][src]

impl<'_> PartialEq<&'_ str> for Ymd8HyphenString[src]

impl PartialEq<[u8]> for Ymd8HyphenString[src]

impl<'_> PartialEq<[u8]> for &'_ Ymd8HyphenString[src]

impl PartialEq<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl PartialEq<Ymd8HyphenString> for Ymd8HyphenString[src]

impl<'_> PartialEq<Ymd8HyphenString> for &'_ Ymd8HyphenString[src]

impl PartialEq<Ymd8HyphenString> for Ymd8HyphenStr[src]

impl<'_> PartialEq<Ymd8HyphenString> for &'_ Ymd8HyphenStr[src]

impl PartialEq<Ymd8HyphenString> for str[src]

impl<'_> PartialEq<Ymd8HyphenString> for &'_ str[src]

impl PartialEq<Ymd8HyphenString> for [u8][src]

impl<'_> PartialEq<Ymd8HyphenString> for &'_ [u8][src]

impl PartialEq<str> for Ymd8HyphenString[src]

impl<'_> PartialEq<str> for &'_ Ymd8HyphenString[src]

impl<'_> PartialOrd<&'_ [u8]> for Ymd8HyphenString[src]

impl<'_> PartialOrd<&'_ Ymd8HyphenStr> for Ymd8HyphenString[src]

impl<'_> PartialOrd<&'_ Ymd8HyphenString> for Ymd8HyphenString[src]

impl<'_> PartialOrd<&'_ Ymd8HyphenString> for str[src]

impl<'_> PartialOrd<&'_ Ymd8HyphenString> for [u8][src]

impl<'_> PartialOrd<&'_ str> for Ymd8HyphenString[src]

impl PartialOrd<[u8]> for Ymd8HyphenString[src]

impl<'_> PartialOrd<[u8]> for &'_ Ymd8HyphenString[src]

impl PartialOrd<Ymd8HyphenStr> for Ymd8HyphenString[src]

impl PartialOrd<Ymd8HyphenString> for Ymd8HyphenString[src]

impl<'_> PartialOrd<Ymd8HyphenString> for &'_ Ymd8HyphenString[src]

impl PartialOrd<Ymd8HyphenString> for Ymd8HyphenStr[src]

impl<'_> PartialOrd<Ymd8HyphenString> for &'_ Ymd8HyphenStr[src]

impl PartialOrd<Ymd8HyphenString> for str[src]

impl<'_> PartialOrd<Ymd8HyphenString> for &'_ str[src]

impl PartialOrd<Ymd8HyphenString> for [u8][src]

impl<'_> PartialOrd<Ymd8HyphenString> for &'_ [u8][src]

impl PartialOrd<str> for Ymd8HyphenString[src]

impl<'_> PartialOrd<str> for &'_ Ymd8HyphenString[src]

impl Serialize for Ymd8HyphenString[src]

impl StructuralEq for Ymd8HyphenString[src]

impl StructuralPartialEq for Ymd8HyphenString[src]

impl<'_> TryFrom<&'_ [u8]> for Ymd8HyphenString[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ NaiveDate> for Ymd8HyphenString[src]

type Error = Error

The type returned in the event of a conversion error.

fn try_from(v: &NaiveDate) -> Result<Self, Self::Error>[src]

Converts the given date into Ymd8HyphenString.

Failures

Fails if the year is less than 0 or greater than 9999.

impl<'_> TryFrom<&'_ str> for Ymd8HyphenString[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<[u8; 10]> for Ymd8HyphenString[src]

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.