pub struct Date(/* private fields */);
Expand description
A representation of a single date.
Implementations§
source§impl Date
impl Date
sourcepub const fn new(year: i16, month: u8, day: u8) -> Self
pub const fn new(year: i16, month: u8, day: u8) -> Self
Construct a new Date
from the provided year, month, and day.
§Examples
use date::Date;
let date = Date::new(2012, 4, 21);
assert_eq!(date.year(), 2012);
assert_eq!(date.month(), 4);
assert_eq!(date.day(), 21);
§Panic
This function panics if it receives “out-of-bounds” values (e.g. “March 32” or “February
30”). However, it can be convenient to be able to send such values to avoid having to handle
overflow yourself; use Date::overflowing_new
for this purpose.
sourcepub const fn from_timestamp(unix_timestamp: i64) -> Self
pub const fn from_timestamp(unix_timestamp: i64) -> Self
Construct a new Date
based on the Unix timestamp.
§Examples
use date::date;
use date::Date;
let day_one = Date::from_timestamp(0);
assert_eq!(day_one, date! { 1970-01-01 });
let later = Date::from_timestamp(15_451 * 86_400);
assert_eq!(later, date! { 2012-04-21 });
Negative timestamps are also supported:
let before_unix_era = Date::from_timestamp(-1);
assert_eq!(before_unix_era, date! { 1969-12-31 });
let hobbit_publication = Date::from_timestamp(-11_790 * 86_400);
assert_eq!(hobbit_publication, date! { 1937-09-21 });
sourcepub const fn overflowing_new(year: i16, month: u8, day: u8) -> Self
pub const fn overflowing_new(year: i16, month: u8, day: u8) -> Self
Construct a new Date
from the provided year, month, and day.
This function accepts “overflow” values that would lead to invalid dates, and canonicalizes them to correct dates, allowing for some math to be done on the inputs without needing to perform overflow checks yourself.
For example, it’s legal to send “March 32” to this function, and it will yield April 1 of the
same year. It’s also legal to send a month
or day
value of zero, and it will conform to
the month or day (respectively) prior to the first.
source§impl Date
impl Date
sourcepub const fn month(&self) -> u8
pub const fn month(&self) -> u8
Returns the month number, starting from 1.
The return value ranges from 1 to 12.
sourcepub const fn day(&self) -> u8
pub const fn day(&self) -> u8
Returns the day of the month, starting from 1.
The return value ranges from 1 to 31. (The last day of the month differs by months.)
sourcepub const fn day_of_year(&self) -> u16
pub const fn day_of_year(&self) -> u16
The day of the current year. Range: [1, 366]
source§impl Date
impl Date
sourcepub const fn timestamp(&self) -> i64
pub const fn timestamp(&self) -> i64
The Unix timestamp for this date at midnight UTC.
§Examples
assert_eq!(date! { 1969-12-31 }.timestamp(), -86_400);
assert_eq!(date! { 1970-01-01 }.timestamp(), 0);
assert_eq!(date! { 1970-01-05 }.timestamp(), 4 * 86_400);
assert_eq!(date! { 2012-04-21 }.timestamp(), 1334966400);
source§impl Date
impl Date
sourcepub fn format<'a>(&'a self, format_str: &'a str) -> FormattedDate<'_>
pub fn format<'a>(&'a self, format_str: &'a str) -> FormattedDate<'_>
Format the date according to the provided strftime
specifier.
The following date specifiers are supported:
§Year
Token | Example | Description |
---|---|---|
%C | 20 | Gregorian year divided by 100, zero-padded to 2 digits |
%Y | 2012 | Gregorian year, zero-padded to 4 digits |
%y | 12 | Gregorian year modulo 100, zero-padded to 2 digits |
§Month
Token | Example | Description |
---|---|---|
%B | December | Full English name of the month |
%b or %h | Dec | Three-letter abbreviation for the month’s English name |
%m | 07 | Month number (01 –12 ), zero-padded to 2 digits |
§Day
Token | Example | Description |
---|---|---|
%d | 21 | Day number (01 –31 ), zero-padded to 2 digits |
%a | Sat | Three-letter abbreviation for the weekday’s English name |
%A | Saturday | Full English name of the weekday |
%w | 6 | Integer representing the weekday: Sunday (0 ) to Saturday (6 ) |
%u | 6 | Integer representing the weekday: Monday (1 ) to Sunday (7 ) |
%j | 112 | Day of the year (001 –366 ), zero-padded to 3 digits |
§Full Date Shortcuts
Token | Example | Description |
---|---|---|
%D | 07/08/01 | Month-day-year format. Same as %m/%d/%y. |
%F | 2001-07-08 | Year-month-day format (ISO 8601). Same as %Y-%m-%d. |
%v | 8-Jul-2001 | Day-month-year format. Same as %e-%b-%Y. |
For tokens with default padding, the padding can be modified or suppressed with a modifier:
Token Modifier | Effect |
---|---|
- (e.g. %-d ) | Suppress padding |
_ (e.g. %_d ) | Use spaces for padding |
0 (e.g. %0d ) | Use zeroes for padding (currently the default for padded tokens) |
Additionally, the following tokens convert to plain characters:
Token | Literal |
---|---|
%n | \n (newline) |
%t | \t (tab) |
%% | % |
source§impl Date
impl Date
sourcepub fn iter_through(&self, end: Date) -> DateIterator ⓘ
pub fn iter_through(&self, end: Date) -> DateIterator ⓘ
An iterator of dates beginning with this date, and ending with the provided end date (inclusive).
Trait Implementations§
source§impl Add<DateInterval> for Date
impl Add<DateInterval> for Date
source§impl Add<MonthInterval> for Date
impl Add<MonthInterval> for Date
source§impl AddAssign<DateInterval> for Date
impl AddAssign<DateInterval> for Date
source§fn add_assign(&mut self, interval: DateInterval)
fn add_assign(&mut self, interval: DateInterval)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Date
impl<'de> Deserialize<'de> for Date
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl FromStr for Date
impl FromStr for Date
§type Err = ParseError
type Err = ParseError
source§impl Ord for Date
impl Ord for Date
source§impl PartialEq for Date
impl PartialEq for Date
source§impl PartialOrd for Date
impl PartialOrd for Date
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Sub<DateInterval> for Date
impl Sub<DateInterval> for Date
source§impl Sub<MonthInterval> for Date
impl Sub<MonthInterval> for Date
source§impl SubAssign<DateInterval> for Date
impl SubAssign<DateInterval> for Date
source§fn sub_assign(&mut self, interval: DateInterval)
fn sub_assign(&mut self, interval: DateInterval)
-=
operation. Read more