pub struct Date { /* private fields */ }Expand description
Date in the proleptic Gregorian calendar.
Range: 30. June -5879611..=12. July 5879611. Please note that year 0 does not exist. After year -1 follows year 1.
Implementations
sourceimpl Date
impl Date
sourcepub fn now() -> Self
pub fn now() -> Self
Creates a new Date instance with SystemTime::now().
let date = Date::now();
assert!(2021 < date.get(DateUnit::Year));sourcepub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>
pub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>
Creates a new Date instance from year, month and day (day of month).
Returns an OutOfRange error if the provided values are invalid.
let date = Date::from_ymd(2022, 05, 02).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));sourcepub fn from_timestamp(timestamp: i64) -> Result<Self, AstrolabeError>
pub fn from_timestamp(timestamp: i64) -> Result<Self, AstrolabeError>
Creates a new Date instance from a unix timestamp (non-leap seconds since January 1, 1970 00:00:00 UTC).
Returns an OutOfRange error if the provided timestamp would result in an out of range date.
let date = Date::from_timestamp(0).unwrap();
assert_eq!("1970/01/01", date.format("yyyy/MM/dd"));sourcepub fn from_days(days: i32) -> Self
pub fn from_days(days: i32) -> Self
Creates a new Date instance from days since January 1, 0001.
let date = Date::from_days(738276);
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));sourcepub fn as_days(&self) -> i32
pub fn as_days(&self) -> i32
Returns the number of days since January 1, 0001. (Negative if date is before)
let date = Date::from_ymd(1, 1, 1).unwrap();
assert_eq!(0, date.as_days());sourcepub fn timestamp(&self) -> i64
pub fn timestamp(&self) -> i64
Returns the number of non-leap seconds since January 1, 1970 00:00:00 UTC. (Negative if date is before)
let date = Date::from_ymd(2000, 1, 1).unwrap();
assert_eq!(946_684_800, date.timestamp());sourcepub fn between(&self, compare: &Self) -> u32
pub fn between(&self, compare: &Self) -> u32
Returns the number of days between two Date instances.
let date = Date::from_ymd(1970, 1, 1).unwrap();
let date_2 = Date::from_ymd(1970, 2, 1).unwrap();
assert_eq!(31, date.between(&date_2));
assert_eq!(31, date_2.between(&date));sourcepub fn get(&self, unit: DateUnit) -> i32
pub fn get(&self, unit: DateUnit) -> i32
Get a specific DateUnit.
let date = Date::from_ymd(2022, 5, 2).unwrap();
assert_eq!(2022, date.get(DateUnit::Year));
assert_eq!(5, date.get(DateUnit::Month));
assert_eq!(2, date.get(DateUnit::Day));sourcepub fn set(&self, value: i32, unit: DateUnit) -> Result<Self, AstrolabeError>
pub fn set(&self, value: i32, unit: DateUnit) -> Result<Self, AstrolabeError>
Creates a new Date instance with a specific DateUnit set to the provided value.
Returns an OutOfRange error if the provided value is invalid or out of range.
let mut date = Date::from_ymd(2022, 5, 2).unwrap();
date = date.set(2000, DateUnit::Year).unwrap();
date = date.set(10, DateUnit::Day).unwrap();
assert_eq!("2000/05/10", date.format("yyyy/MM/dd"));sourcepub fn apply(&self, amount: i32, unit: DateUnit) -> Result<Self, AstrolabeError>
pub fn apply(&self, amount: i32, unit: DateUnit) -> Result<Self, AstrolabeError>
Creates a new Date instance with a specified amount of time applied (added or subtracted).
Note: When using DateUnit::Month, it adds calendar months and not 30 days. See it’s documentation for examples.
Returns an OutOfRange error if the provided value would result in an out of range date.
let date = Date::from_ymd(1970, 1, 1).unwrap();
let applied = date.apply(1, DateUnit::Day).unwrap();
assert_eq!("1970/01/01", date.format("yyyy/MM/dd"));
assert_eq!("1970/01/02", applied.format("yyyy/MM/dd"));
let applied_2 = applied.apply(-1, DateUnit::Day).unwrap();
assert_eq!("1970/01/01", applied_2.format("yyyy/MM/dd"));sourcepub fn format(&self, format: &str) -> String
pub fn format(&self, format: &str) -> String
Formatting with format strings based on Unicode Date Field Symbols.
Please note that not all symbols are implemented. If you need something that is not implemented, please open an issue on GitHub describing your need.
Available Symbols:
| Field Type | Pattern | Examples | Hint |
|---|---|---|---|
| era | G..GGG | AD | |
| GGGG | Anno Domini | * | |
| GGGGG | A | ||
| year | y | 2, 20, 201, 2017, 20173 | |
| yy | 02, 20, 01, 17, 73 | ||
| yyy | 002, 020, 201, 2017, 20173 | ||
| yyyy | 0002, 0020, 0201, 2017, 20173 | ||
| yyyyy+ | … | Unlimited length, padded with zeros. | |
| quarter | q | 2 | * |
| 02 | |||
| qqq | Q2 | ||
| qqqq | 2nd quarter | ||
| qqqqq | 2 | ||
| month | M | 9, 12 | |
| MM | 09, 12 | ||
| MMM | Sep | ||
| MMMM | September | * | |
| MMMMM | S | ||
| week | w | 8, 27 | Week of year |
| ww | 08, 27 | * | |
| days | d | 1 | Day of month |
| dd | 01 | * | |
| D | 1, 24 135 | Day of year, * | |
| DD | 01, 24, 135 | ||
| DDD | 001, 024, 135 | ||
| week day | e | 3 | 1-7, 1 is Sunday, * |
| ee | 03 | 1-7, 1 is Sunday | |
| eee | Tue | ||
| eeee | Tuesday | ||
| eeeee | T | ||
| eeeeee | Tu | ||
| eeeeeee | 2 | 1-7, 1 is Monday | |
| eeeeeeee | 02 | 1-7, 1 is Monday |
* = Default
If the sequence is longer than listed in the table, the output will be the same as the default pattern for this unit (marked with *).
Surround any character with apostrophes (') to escape them.
If you want escape ', write ''.
let date = Date::from_ymd(2022, 5, 2).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));
// Escape characters
assert_eq!("2022/MM/dd", date.format("yyyy/'MM/dd'"));
assert_eq!("2022/'05/02'", date.format("yyyy/''MM/dd''"));Trait Implementations
sourceimpl PartialEq<Date> for Date
impl PartialEq<Date> for Date
impl Copy for Date
impl Eq for Date
impl StructuralEq for Date
Auto Trait Implementations
impl RefUnwindSafe for Date
impl Send for Date
impl Sync for Date
impl Unpin for Date
impl UnwindSafe for Date
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more