rename from_ymwd -> from_weekday_of_month

This commit is contained in:
Brandon W Maister 2019-12-23 17:33:44 -05:00
parent 892bbf3f3a
commit 5d16648df4
2 changed files with 15 additions and 15 deletions

View File

@ -12,7 +12,7 @@ Versions with only mechnical changes will be omitted from the following list.
### Features
* Added `NaiveDate::from_ymwd{,_opt}` for getting eg. the 2nd Friday of March 2017.
* Added `NaiveDate::from_weekday_of_month{,_opt}` for getting eg. the 2nd Friday of March 2017.
## 0.4.5

View File

@ -409,7 +409,7 @@ impl NaiveDate {
/// Makes a new `NaiveDate` by counting the number of occurances of a particular day-of-week
/// since the beginning of the given month. For instance, if you want the 2nd Friday of March
/// 2017, you would use `NaiveDate::from_ymwd(2017, 3, Weekday::Fri, 2)`.
/// 2017, you would use `NaiveDate::from_weekday_of_month(2017, 3, Weekday::Fri, 2)`.
///
/// # Panics
///
@ -423,32 +423,32 @@ impl NaiveDate {
/// ~~~~
/// use chrono::{NaiveDate, Weekday};
///
/// let from_ymwd = NaiveDate::from_ymwd;
/// let from_weekday_of_month = NaiveDate::from_weekday_of_month;
/// let from_ymd = NaiveDate::from_ymd;
///
/// assert_eq!(from_ymwd(2018, 8, Weekday::Wed, 1), from_ymd(2018, 8, 1));
/// assert_eq!(from_ymwd(2018, 8, Weekday::Fri, 1), from_ymd(2018, 8, 3));
/// assert_eq!(from_ymwd(2018, 8, Weekday::Tue, 2), from_ymd(2018, 8, 14));
/// assert_eq!(from_ymwd(2018, 8, Weekday::Fri, 4), from_ymd(2018, 8, 24));
/// assert_eq!(from_ymwd(2018, 8, Weekday::Fri, 5), from_ymd(2018, 8, 31));
/// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Wed, 1), from_ymd(2018, 8, 1));
/// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 1), from_ymd(2018, 8, 3));
/// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Tue, 2), from_ymd(2018, 8, 14));
/// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 4), from_ymd(2018, 8, 24));
/// assert_eq!(from_weekday_of_month(2018, 8, Weekday::Fri, 5), from_ymd(2018, 8, 31));
/// ~~~~
pub fn from_ymwd(year: i32, month: u32, weekday: Weekday, n: u8) -> NaiveDate {
NaiveDate::from_ymwd_opt(year, month, weekday, n).expect("out-of-range date")
pub fn from_weekday_of_month(year: i32, month: u32, weekday: Weekday, n: u8) -> NaiveDate {
NaiveDate::from_weekday_of_month_opt(year, month, weekday, n).expect("out-of-range date")
}
/// Makes a new `NaiveDate` by counting the number of occurances of a particular day-of-week
/// since the beginning of the given month. For instance, if you want the 2nd Friday of March
/// 2017, you would use `NaiveDate::from_ymwd(2017, 3, Weekday::Fri, 2)`. `n` is 1-indexed.
/// 2017, you would use `NaiveDate::from_weekday_of_month(2017, 3, Weekday::Fri, 2)`. `n` is 1-indexed.
///
/// ~~~~
/// use chrono::{NaiveDate, Weekday};
/// assert_eq!(NaiveDate::from_ymwd_opt(2017, 3, Weekday::Fri, 2),
/// assert_eq!(NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
/// NaiveDate::from_ymd_opt(2017, 3, 10))
/// ~~~~
///
/// Returns `None` if `n` out-of-range; ie. if `n` is larger than the number of `weekday` in
/// `month` (eg. the 6th Friday of March 2017), or if `n == 0`.
pub fn from_ymwd_opt(year: i32, month: u32, weekday: Weekday, n: u8) -> Option<NaiveDate> {
pub fn from_weekday_of_month_opt(year: i32, month: u32, weekday: Weekday, n: u8) -> Option<NaiveDate> {
if n == 0 { return None; }
let first = NaiveDate::from_ymd(year, month, 1).weekday();
let first_to_dow = (7 + weekday.number_from_monday() - first.number_from_monday()) % 7;
@ -1873,8 +1873,8 @@ mod tests {
}
#[test]
fn test_date_from_ymwd_opt() {
let ymwd = |y,m,w,n| NaiveDate::from_ymwd_opt(y,m,w,n);
fn test_date_from_weekday_of_month_opt() {
let ymwd = |y,m,w,n| NaiveDate::from_weekday_of_month_opt(y,m,w,n);
assert_eq!(ymwd(2018, 8, Weekday::Tue, 0), None);
assert_eq!(ymwd(2018, 8, Weekday::Wed, 1), Some(NaiveDate::from_ymd(2018, 8, 1)));
assert_eq!(ymwd(2018, 8, Weekday::Thu, 1), Some(NaiveDate::from_ymd(2018, 8, 2)));