Module date

Source
Available on crate feature date only.
Expand description

Date formatting

This module includes various Date types meant for calendar day formatting:

let date  = Date::from_ymd(2020, 12, 25).unwrap();
let nichi = Nichi::new(2020, 12, 25).unwrap();

assert_eq!(date,  "2020-12-25");
assert_eq!(nichi, "Fri, Dec 25, 2020");
assert_eq!(date.inner(), nichi.inner());

The inner “integer” type is a tuple of: (u16, u8, u8) representing the (Year, Month, Day)

  • The year must be 1000-9999
  • The month must be 1-12
  • The day must be 1-31

§Weekday

These types all have .weekday() functions that calculate the weekday given the year, month, and day.

This uses Tomohiko Sakamoto’s algorithm.

It is accurate for any Nichi or NichiFull but only accurate for Date when it has the month and day.

// US Independence day was on a Thursday.
assert_eq!(
    Nichi::new(1776, 7, 4).unwrap().weekday().as_str(),
    "Thursday"
);

// Nintendo Switch was released on a Friday.
assert_eq!(
    Nichi::new(2017, 3, 3).unwrap().weekday().as_str(),
    "Friday"
);

// Christmas in 1999 was on a Saturday.
assert_eq!(
    Nichi::new(1999, 12, 25).unwrap().weekday().as_str(),
    "Saturday"
);

// A good album was released on a Wednesday.
assert_eq!(
    Nichi::new(2018, 4, 25).unwrap().weekday().as_str(),
    "Wednesday"
);

§From other types

All types support conversion with each other using From, although Date itself is only lossless if the full year-month-day is available.

// Lossless.
let date  = Date::from_ymd(2020, 12, 25).unwrap();
let nichi = Nichi::from(date);
assert_eq!(date, (2020, 12, 25));
assert_eq!(nichi, (2020, 12, 25));

// Missing date, unknown is returned.
let date  = Date::from_ym(2020, 12).unwrap();
let nichi = Nichi::from(date);
assert_eq!(date, (2020, 12, 0));
assert_eq!(nichi, "???");
assert!(nichi.is_unknown());

§Copy

Copy is available.

let a = Date::from_str("2014-04-22").unwrap();

// Copy 'a', use 'b'.
let b = a;
assert_eq!(b, "2014-04-22");

// We can still use 'a'
assert_eq!(a, "2014-04-22");

Structs§

Date
A recent date that is in YEAR-MONTH-DAY format, similar to ISO 8601
Nichi
A date that is in Weekday, Month Day, Year format
NichiFull
Same as Nichi but with fully specified words

Traits§

SysDate
Current system date

Functions§

date
Get the current system date in the system’s timezone.
date_utc
Get the current system date in UTC locale