pub struct DateStr { /* private fields */ }
Expand description
The date struct
Months and years are 1-indexed, meaning they start at ONE (1). So January would be 1, as written normally, and December is 12.
Called DateStr because it comes from a String
Implementations§
Source§impl DateStr
impl DateStr
Sourcepub fn from_iso_str<T: ToString>(string: T) -> DateStr
pub fn from_iso_str<T: ToString>(string: T) -> DateStr
Parse a string to a DateStr struct
Parses a string (or any type implementing the ToString trait) to a DateStr struct.
The given date must be in ISO-8601 format, that is: YYYY-MM-DD.
I’d recommend using crate::DateStr::try_from_iso_str when unsure what the input string will be, since it returns a Result with understandable errors.
§Examples
let date_string: String = String::from("2022-12-31");
let new_date_from_string: DateStr = DateStr::from_iso_str(date_string);
let new_date_from_str: DateStr = DateStr::from_iso_str("2022-12-31");
assert_eq!(new_date_from_str, new_date_from_string);
Sourcepub fn try_from_iso_str<T: ToString>(string: T) -> Result<DateStr, DateErrors>
pub fn try_from_iso_str<T: ToString>(string: T) -> Result<DateStr, DateErrors>
Parse a string to a DateStr struct
Parses a string (or any type implementing the ToString trait) to a DateStr struct. This function returns a Result enum.
The given date must be in ISO-8601 format, that is: YYYY-MM-DD.
§Examples
let date_string: String = String::from("2022-12-31");
let date_from_string: Result<DateStr, errors::DateErrors> = DateStr::try_from_iso_str(date_string);
assert!(date_from_string.is_ok());;
§Errors
Since it checks for month first, it will return a DateErrors::InvalidMonth even if the day is wrong too, in wich it would return a DateErrors::InvalidDay.
Source§impl DateStr
impl DateStr
Sourcepub fn format(&self, fmt: DateFormat) -> String
pub fn format(&self, fmt: DateFormat) -> String
Format the date with a DateFormat
Pass a DateFormat. Will output a String with the date formatted how you wanted.
Use crate::DateStr::try_format for easy error handling
§Example
let a_date: DateStr = DateStr::from_iso_str("2022-12-29");
let a_fmtr: DateFormat = DateFormat::from_string("dd_mm_yyyy", Some('_')).unwrap();
let formatted_date: String = a_date.format(a_fmtr);
println!("{}", formatted_date);
Above code will output 29-12-2022.
§Panics
This function will panic when an invalid DateFormat is passed.
To use errors see crate::DateStr::try_format()
Sourcepub fn try_format(&self, fmt: DateFormat) -> Result<String, DateErrors>
pub fn try_format(&self, fmt: DateFormat) -> Result<String, DateErrors>
Try to format the date with a custom formatter
Safe function using the Result enum. Receives a DateFormat struct.
§Example:
let a_date: DateStr = DateStr::from_iso_str("2022-12-29");
let some_formatter: DateFormat = DateFormat::from_string("dd-mm-yyyy", None).unwrap();
let formatted_date: String = a_date.try_format(some_formatter).unwrap();
println!("{}", formatted_date);
Will output 29-12-2022
Trait Implementations§
Source§impl Display for DateStr
Display trait implementation for DateStr
impl Display for DateStr
Display trait implementation for DateStr
Prints the date in ISO-8601 format (YYYY-MM-DD)
Source§impl Into<DateStr> for String
Implementation of ToDateStr for String
impl Into<DateStr> for String
Implementation of ToDateStr for String
Source§fn to_datestr(&self) -> DateStr
fn to_datestr(&self) -> DateStr
Source§fn try_to_datestr(&self) -> Result<DateStr, DateErrors>
fn try_to_datestr(&self) -> Result<DateStr, DateErrors>
Source§impl Into<DateStr> for str
Implementation of ToDateStr for &str
impl Into<DateStr> for str
Implementation of ToDateStr for &str