BUG: Lucene.Net.QueryParser.Flexible.Standard.Config.NumberDateFormat: Fixed parsing of date to first try the format that is being used to generate the date, and if it fails, then do a DateTime.Parse().
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/79bf0632 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/79bf0632 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/79bf0632 Branch: refs/heads/api-work Commit: 79bf0632322d2091d056a5ad0fad06b441af558a Parents: 58a7612 Author: Shad Storhaug <[email protected]> Authored: Wed Mar 8 10:14:15 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Wed Mar 8 16:56:44 2017 +0700 ---------------------------------------------------------------------- .../Flexible/Standard/Config/NumberDateFormat.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/79bf0632/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs b/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs index 1a856cf..a3e1670 100644 --- a/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs +++ b/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs @@ -91,7 +91,14 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config public override object Parse(string source) { - return (DateTime.Parse(source, this.locale) - new DateTime(EPOCH)).TotalMilliseconds; + // Try exact format first, if it fails, do a loose DateTime.Parse + DateTime d; + if (!DateTime.TryParseExact(source, GetDateFormat(), this.locale, DateTimeStyles.None, out d)) + { + d = DateTime.Parse(source, this.locale); + } + + return (d - new DateTime(EPOCH)).TotalMilliseconds; } public override string Format(object number) @@ -139,10 +146,10 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config timePattern = locale.DateTimeFormat.LongTimePattern; break; case DateFormat.LONG: - timePattern = locale.DateTimeFormat.LongTimePattern; // LUCENENET TODO: Time zone info not being added + timePattern = locale.DateTimeFormat.LongTimePattern.Replace(" K", "") + " K"; // LUCENENET specific: Time zone info not being added to match behavior of Java break; case DateFormat.FULL: - timePattern = locale.DateTimeFormat.LongTimePattern; // LUCENENET TODO: Time zone info not being added, but Java doc is unclear on what the difference is between this and LONG + timePattern = locale.DateTimeFormat.LongTimePattern.Replace(" K", "") + " K"; // LUCENENET TODO: Time zone info not being added to match behavior of Java, but Java doc is unclear on what the difference is between this and LONG break; }
