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: u16Year: four digits
month: u8Month: 1 to 12
day: u8Day: 1 to {28, 29, 30, 31} (based on month & year)
Implementations§
Source§impl Date
impl Date
Sourcepub fn parse_str_rfc3339(str: &str) -> Result<Self, ParseError>
pub fn parse_str_rfc3339(str: &str) -> Result<Self, ParseError>
Sourcepub fn parse_str(str: &str) -> Result<Self, ParseError>
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");Sourcepub fn parse_str_with_config(
str: &str,
config: &DateConfig,
) -> Result<Self, ParseError>
pub fn parse_str_with_config( str: &str, config: &DateConfig, ) -> Result<Self, ParseError>
As with Date::parse_str but with a DateConfig.
Sourcepub fn parse_bytes_rfc3339(bytes: &[u8]) -> Result<Self, ParseError>
pub fn parse_bytes_rfc3339(bytes: &[u8]) -> Result<Self, ParseError>
Sourcepub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError>
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");Sourcepub fn parse_bytes_with_config(
bytes: &[u8],
config: &DateConfig,
) -> Result<Self, ParseError>
pub fn parse_bytes_with_config( bytes: &[u8], config: &DateConfig, ) -> Result<Self, ParseError>
Same as Date::parse_bytes but with a DateConfig.
Sourcepub fn from_timestamp(
timestamp: i64,
require_exact: bool,
config: &DateConfig,
) -> Result<Self, ParseError>
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,000is2603-10-1120,000,000,001is1970-08-20-62,167,219,200,001gives an error -DateTooSmallas it would be before 0000-01-01-20,000,000,001is1969-05-14-20,000,000,000is1336-03-23
§Arguments
timestamp- timestamp in either seconds or millisecondsrequire_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");Sourcepub fn timestamp(&self) -> i64
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);Sourcepub fn timestamp_ms(&self) -> i64
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);Sourcepub fn today(tz_offset: i32) -> Result<Self, ParseError>
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 than86_400
§Example
use speedate::Date;
let d = Date::today(0).unwrap();
println!("The date today is: {}", d)Sourcepub fn ordinal_day(&self) -> u16
pub fn ordinal_day(&self) -> u16
Day of the year, starting from 1.