Lucene.Net.QueryParser.Flexible.Standard.Config.NumberDateFormat: Corrected implementation to use ParseExact instead of Parse to ensure it can parse the dates it produces in all cultures (problematic with ar), and added SetDateFormat() method
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/33950f9c Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/33950f9c Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/33950f9c Branch: refs/heads/api-work Commit: 33950f9c3d4b827f517b0cc66f9c39ae654adc19 Parents: 2abbd52 Author: Shad Storhaug <[email protected]> Authored: Wed Mar 8 16:29:08 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Wed Mar 8 16:56:46 2017 +0700 ---------------------------------------------------------------------- .../Standard/Config/NumberDateFormat.cs | 27 ++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/33950f9c/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 a3e1670..c53a8b8 100644 --- a/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs +++ b/src/Lucene.Net.QueryParser/Flexible/Standard/Config/NumberDateFormat.cs @@ -1,5 +1,6 @@ using Lucene.Net.Support; using System; +using System.Collections.Generic; using System.Globalization; namespace Lucene.Net.QueryParsers.Flexible.Standard.Config @@ -42,10 +43,10 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config { //private static readonly long serialVersionUID = 964823936071308283L; - // The .NET ticks representing January 1, 1970 0:00:00, also known as the "epoch". + // The .NET ticks representing January 1, 1970 0:00:00 GMT, also known as the "epoch". public const long EPOCH = 621355968000000000; - private readonly string dateFormat; + private string dateFormat; private readonly DateFormat dateStyle; private readonly DateFormat timeStyle; private TimeZoneInfo timeZone = TimeZoneInfo.Local; @@ -93,10 +94,7 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config { // 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); - } + d = DateTime.ParseExact(source, GetDateFormat(), this.locale, DateTimeStyles.None); return (d - new DateTime(EPOCH)).TotalMilliseconds; } @@ -106,16 +104,25 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config return new DateTime(EPOCH).AddMilliseconds(Convert.ToInt64(number, CultureInfo.InvariantCulture)).ToString(GetDateFormat(), this.locale); } + public void SetDateFormat(string dateFormat) + { + this.dateFormat = dateFormat; + } + /// <summary> /// Returns the .NET date format that will be used to Format the date. - /// Note that parsing the date uses <see cref="DateTime.Parse(string, IFormatProvider)"/>, which - /// does not require a format. + /// Note that parsing the date uses <see cref="DateTime.ParseExact(string, string, IFormatProvider)"/>. /// </summary> // LUCENENET specific public string GetDateFormat() { if (dateFormat != null) return dateFormat; + return GetDateFormat(this.dateStyle, this.timeStyle, this.locale); + } + + public static string GetDateFormat(DateFormat dateStyle, DateFormat timeStyle, CultureInfo locale) + { string datePattern = "", timePattern = ""; switch (dateStyle) @@ -146,10 +153,10 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Config timePattern = locale.DateTimeFormat.LongTimePattern; break; case DateFormat.LONG: - timePattern = locale.DateTimeFormat.LongTimePattern.Replace(" K", "") + " K"; // LUCENENET specific: Time zone info not being added to match behavior of Java + timePattern = locale.DateTimeFormat.LongTimePattern.Replace("z", "").Trim() + " z"; break; case DateFormat.FULL: - 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 + timePattern = locale.DateTimeFormat.LongTimePattern.Replace("z", "").Trim() + " z"; // 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; }
