summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordfhoughton <dfhoughton@gmail.com>2018-12-28 17:39:39 -0500
committerdfhoughton <dfhoughton@gmail.com>2018-12-28 17:39:39 -0500
commit3f677ffec9d205cb2340ca5b4f27ed841372f288 (patch)
treec7e554e4d12d74d6852b22613d84933b1ae1049d
parent3201de663d1922846181cb252732c2de81aee393 (diff)
downloadtwo-timer-3f677ffec9d205cb2340ca5b4f27ed841372f288.zip
more two-time expressions working
-rw-r--r--src/lib.rs24
-rw-r--r--tests/tests.rs46
2 files changed, 64 insertions, 6 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a3d0719..f993435 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -219,7 +219,8 @@ pub fn parse(
if specific(last) {
return match specific_moment(first, &config) {
Ok((d1, _)) => match specific_moment(last, &config) {
- Ok((_, d2)) => {
+ Ok((d2, d3)) => {
+ let d2 = pick_terminus(d2, d3);
if d1 <= d2 {
Ok((d1, d2))
} else {
@@ -233,7 +234,8 @@ pub fn parse(
} else {
return match specific_moment(first, &config) {
Ok((d1, _)) => {
- let (_, d2) = relative_moment(last, &config, &d1, false);
+ let (d2, d3) = relative_moment(last, &config, &d1, false);
+ let d2 = pick_terminus(d2, d3);
Ok((d1, d2))
}
Err(s) => Err(s),
@@ -241,7 +243,8 @@ pub fn parse(
}
} else if specific(last) {
return match specific_moment(last, &config) {
- Ok((_, d2)) => {
+ Ok((d2, d3)) => {
+ let d2 = pick_terminus(d2, d3);
let (d1, _) = relative_moment(first, &config, &d2, true);
Ok((d1, d2))
}
@@ -251,13 +254,24 @@ pub fn parse(
// the first moment is assumed to be before now
let (d1, _) = relative_moment(first, &config, &config.now, true);
// the second moment is necessarily after the first momentß
- let (_, d2) = relative_moment(last, &config, &d1, false);
+ let (d2, d3) = relative_moment(last, &config, &d1, false);
+ let d2 = pick_terminus(d2, d3);
return Ok((d1, d2));
}
}
unreachable!();
}
+// for the end time, if the span is less than a day, use the first, otherwise use the second
+// e.g., Monday through Friday at 3 PM should end at 3 PM, but Monday through Friday should end at the end of Friday
+fn pick_terminus(d1: DateTime<Utc>, d2: DateTime<Utc>) -> DateTime<Utc> {
+ if d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year() {
+ d1
+ } else {
+ d2
+ }
+}
+
fn first_moment() -> DateTime<Utc> {
chrono::MIN_DATE.and_hms_milli(0, 0, 0, 0)
}
@@ -575,7 +589,7 @@ fn relative_moment(
if let Some(day) = m.name("a_day") {
let wd = weekday(day.as_str());
let mut delta =
- config.now.weekday().num_days_from_sunday() as i64 - wd.num_days_from_sunday() as i64;
+ other_time.weekday().num_days_from_sunday() as i64 - wd.num_days_from_sunday() as i64;
if delta <= 0 {
delta += 7;
}
diff --git a/tests/tests.rs b/tests/tests.rs
index 014ddcb..9280c82 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -786,4 +786,48 @@ fn just_june() {
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
+}
+
+#[test]
+fn monday_through_friday() {
+ let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0);
+ let d1 = Utc.ymd(1969, 5, 5).and_hms(0, 0, 0);
+ let d2 = Utc.ymd(1969, 5, 10).and_hms(0, 0, 0);
+ let (start, end) = parse("Monday through Friday", Some(Config::default().now(now))).unwrap();
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+}
+
+#[test]
+fn tuesday_through_friday() {
+ let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0);
+ let d1 = Utc.ymd(1969, 4, 29).and_hms(0, 0, 0);
+ let d2 = Utc.ymd(1969, 5, 3).and_hms(0, 0, 0);
+ let (start, end) = parse("Tuesday through Friday", Some(Config::default().now(now))).unwrap();
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+}
+
+#[test]
+fn tuesday_through_3_pm_on_friday() {
+ let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0);
+ let d1 = Utc.ymd(1969, 4, 29).and_hms(0, 0, 0);
+ let d2 = Utc.ymd(1969, 5, 2).and_hms(15, 0, 0);
+ let (start, end) = parse(
+ "Tuesday through 3 PM on Friday",
+ Some(Config::default().now(now)),
+ )
+ .unwrap();
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+}
+
+#[test]
+fn this_year_through_today() {
+ let now = Utc.ymd(1969, 5, 6).and_hms(0, 0, 0);
+ let d1 = Utc.ymd(1969, 1, 1).and_hms(0, 0, 0);
+ let d2 = Utc.ymd(1969, 5, 7).and_hms(0, 0, 0);
+ let (start, end) = parse("this year through today", Some(Config::default().now(now))).unwrap();
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+}