diff options
-rw-r--r-- | src/lib.rs | 53 | ||||
-rw-r--r-- | tests/tests.rs | 66 |
2 files changed, 60 insertions, 59 deletions
@@ -523,32 +523,6 @@ fn handle_specific_time( } else { Ok((last_moment(), last_moment())) }; - // FIXME - // if let Some(t) = moment.name("time") { - // let (hour, minute, second) = time(t); - // let period = if second.is_some() { - // Period::Second - // } else if minute.is_some() { - // Period::Minute - // } else { - // Period::Hour - // }; - // let mut t = other_time - // .with_hour(hour) - // .unwrap() - // .with_minute(minute.unwrap_or(0)) - // .unwrap() - // .with_second(second.unwrap_or(0)) - // .unwrap(); - // if before && t > *other_time { - // t = t - Duration::days(1); - // } else if !before && t < *other_time { - // t = t + Duration::days(1); - // } - // return Ok(moment_to_period(t, &period, config)); - // } else { - // unreachable!(); - // } } fn handle_one_time( @@ -605,7 +579,7 @@ fn relative_moment( if delta <= 0 { delta += 7; } - let mut d = config.now.date() - Duration::days(delta); + let mut d = other_time.date() - Duration::days(delta); if !before { d = d + Duration::days(7); } @@ -637,8 +611,29 @@ fn relative_moment( } return moment_to_period(t, &period, config); } - if let Some(day) = m.name("named_period") { - unimplemented!(); + if let Some(month) = m.name("a_month") { + let month = a_month(month); + let year = if before { + if month > other_time.month() { + other_time.year() - 1 + } else { + other_time.year() + } + } else { + if month < other_time.month() { + other_time.year() + 1 + } else { + other_time.year() + } + }; + let d = Utc.ymd(year, month, 1).and_hms(0, 0, 0); + let (d1, d2) = moment_to_period(d, &Period::Month, config); + if before && d1 >= *other_time { + return moment_to_period(d1.with_year(d1.year() - 1).unwrap(), &Period::Month, config); + } else if !before && d2 <= *other_time { + return moment_to_period(d1.with_year(d1.year() + 1).unwrap(), &Period::Month, config); + } + return (d1, d2); } unreachable!() } diff --git a/tests/tests.rs b/tests/tests.rs index 609d381..014ddcb 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -708,11 +708,7 @@ fn the_crack_of_doom() { fn friday() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 5, 2).and_hms(0, 0, 0); - let (start, end) = parse( - "Friday", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Friday", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::days(1), end); } @@ -721,11 +717,7 @@ fn friday() { fn tuesday() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 4, 29).and_hms(0, 0, 0); - let (start, end) = parse( - "Tuesday", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Tuesday", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::days(1), end); } @@ -734,11 +726,7 @@ fn tuesday() { fn monday() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 5, 5).and_hms(0, 0, 0); - let (start, end) = parse( - "Monday", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Monday", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::days(1), end); } @@ -747,11 +735,7 @@ fn monday() { fn friday_at_3_pm() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 5, 2).and_hms(15, 0, 0); - let (start, end) = parse( - "Friday at 3 pm", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Friday at 3 pm", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::hours(1), end); } @@ -760,11 +744,7 @@ fn friday_at_3_pm() { fn tuesday_at_3_pm() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 4, 29).and_hms(15, 0, 0); - let (start, end) = parse( - "Tuesday at 3 pm", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Tuesday at 3 pm", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::hours(1), end); } @@ -773,11 +753,37 @@ fn tuesday_at_3_pm() { fn monday_at_3_pm() { let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); let then = Utc.ymd(1969, 5, 5).and_hms(15, 0, 0); - let (start, end) = parse( - "Monday at 3 pm", - Some(Config::default().now(now)), - ) - .unwrap(); + let (start, end) = parse("Monday at 3 pm", Some(Config::default().now(now))).unwrap(); assert_eq!(then, start); assert_eq!(then + Duration::hours(1), end); +} + +#[test] +fn just_may() { + let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); + let d1 = Utc.ymd(1969, 5, 1).and_hms(0, 0, 0); + let d2 = Utc.ymd(1969, 6, 1).and_hms(0, 0, 0); + let (start, end) = parse("May", Some(Config::default().now(now))).unwrap(); + assert_eq!(d1, start); + assert_eq!(d2, end); +} + +#[test] +fn just_april() { + let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); + let d1 = Utc.ymd(1969, 4, 1).and_hms(0, 0, 0); + let d2 = Utc.ymd(1969, 5, 1).and_hms(0, 0, 0); + let (start, end) = parse("April", Some(Config::default().now(now))).unwrap(); + assert_eq!(d1, start); + assert_eq!(d2, end); +} + +#[test] +fn just_june() { + let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0); + let d1 = Utc.ymd(1968, 6, 1).and_hms(0, 0, 0); + let d2 = Utc.ymd(1968, 7, 1).and_hms(0, 0, 0); + let (start, end) = parse("June", Some(Config::default().now(now))).unwrap(); + assert_eq!(d1, start); + assert_eq!(d2, end); }
\ No newline at end of file |