From ca739278fa60d7f8d8fe2db612f4a1a1f7c0fa16 Mon Sep 17 00:00:00 2001 From: Rami Abughazaleh Date: Mon, 23 Jan 2017 13:44:43 -0800 Subject: [PATCH] Added support for "and" instead of "+" in ArithmeticTimePlugin Fixed issue #1 --- Parser/Plugins/ArithmeticTimePlugin.cs | 2 +- README.md | 1 + Tests/Plugins/ArithmeticTimePluginTests.cs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Parser/Plugins/ArithmeticTimePlugin.cs b/Parser/Plugins/ArithmeticTimePlugin.cs index c168a08..980cb36 100644 --- a/Parser/Plugins/ArithmeticTimePlugin.cs +++ b/Parser/Plugins/ArithmeticTimePlugin.cs @@ -14,7 +14,7 @@ public class ArithmeticTimePlugin : IParseTimeStrings, IApplyTimeTokens ** Properties *********/ /// The regular expression that matches the date tokens in the input expression. - protected readonly Regex ParsePattern = new Regex(@"^(?\s*((?[\+\-]{0,1})\s*(?\d*))?\s*\b(?\w+)\b(?(\s*\bago\b)?)\s*)+", RegexOptions.Compiled | RegexOptions.ExplicitCapture); + protected readonly Regex ParsePattern = new Regex(@"^(?\s*((?[\+\-and]{0,3})\s*(?\d*))?\s*\b(?\w+)\b(?(\s*\bago\b)?)\s*)+", RegexOptions.Compiled | RegexOptions.ExplicitCapture); /********* diff --git a/README.md b/README.md index 2c955cf..3af70c1 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ You can also chain relative units: * `1 year 2 months` (14 months from now) * `1 year -2 fortnights` (almost 11 months from now) * `1 year ago 1 year` (today; equivalent to `-1 year +1 year`) +* `1 year ago and 5 days ago` (5 days ago last year; equivalent to `-1 year -5 days`) ### Integrated with template engines ##### DotLiquid diff --git a/Tests/Plugins/ArithmeticTimePluginTests.cs b/Tests/Plugins/ArithmeticTimePluginTests.cs index 5845768..4f0abff 100644 --- a/Tests/Plugins/ArithmeticTimePluginTests.cs +++ b/Tests/Plugins/ArithmeticTimePluginTests.cs @@ -86,6 +86,7 @@ public string Tokenize_IsCaseInsensitive(string format) [TestCase("15 years 3 months 2 hours", Result = "[Years:15][Months:3][Hours:2]")] [TestCase("15 years -12 months 2 fortnights 3 weeks -17 days ago -hours 2 minutes secs", Result = "[Years:15][Months:-12][Fortnights:2][Weeks:3][Days:17][Hours:-1][Minutes:2][Seconds:1]")] [TestCase("15 years -months +months ago -2 fortnights 3 weeks -17 days ago -hours 2 minutes secs", Result = "[Years:15][Months:-1][Months:-1][Fortnights:-2][Weeks:3][Days:17][Hours:-1][Minutes:2][Seconds:1]")] + [TestCase("1 year ago and 5 days ago", Result = "[Years:-1][Days:-5]")] public string Tokenize_CanChainUnits(string format) { return this.Tokenize(new ArithmeticTimePlugin(), format);