public void AssertDateRangeQueryEquals(ICommonQueryParserConfiguration cqpC, string field, string startDate, string endDate,
     DateTime endDateInclusive, DateTools.Resolution resolution)
 {
     AssertQueryEquals(cqpC, field, field + ":[" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate) + "]",
                "[" + GetDate(startDate, resolution) + " TO " + GetDate(endDateInclusive, resolution) + "]");
     AssertQueryEquals(cqpC, field, field + ":{" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate) + "}",
                "{" + GetDate(startDate, resolution) + " TO " + GetDate(endDate, resolution) + "}");
 }
Пример #2
0
        public virtual void TestRound()
        {
            // we use default locale since LuceneTestCase randomizes it
            //Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.Default);
            //cal.clear();
            DateTime cal = new GregorianCalendar().ToDateTime(2004, 2, 3, 22, 8, 56, 333); // hour, minute, second -  year=2004, month=february(!), day=3
            //cal.set(DateTime.MILLISECOND, 333);
            DateTime date = cal;

            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(date));

            DateTime dateYear = DateTools.Round(date, DateTools.Resolution.YEAR);

            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(dateYear));

            DateTime dateMonth = DateTools.Round(date, DateTools.Resolution.MONTH);

            Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(dateMonth));

            DateTime dateDay = DateTools.Round(date, DateTools.Resolution.DAY);

            Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(dateDay));

            DateTime dateHour = DateTools.Round(date, DateTools.Resolution.HOUR);

            Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(dateHour));

            DateTime dateMinute = DateTools.Round(date, DateTools.Resolution.MINUTE);

            Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(dateMinute));

            DateTime dateSecond = DateTools.Round(date, DateTools.Resolution.SECOND);

            Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(dateSecond));

            DateTime dateMillisecond = DateTools.Round(date, DateTools.Resolution.MILLISECOND);

            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(dateMillisecond));

            // long parameter:
            long dateYearLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.YEAR);

            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(new DateTime(dateYearLong)));

            long dateMillisecondLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);

            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(new DateTime(dateMillisecondLong)));
        }
Пример #3
0
        public virtual void TestStringToDate()
        {
            System.DateTime d;
            d = DateTools.StringToDate("2004");
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705");
            Assert.AreEqual("2004-07-05 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("200407050910");
            Assert.AreEqual("2004-07-05 09:10:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705091055990");
            Assert.AreEqual("2004-07-05 09:10:55:990", IsoFormat(d));

            Assert.Throws <FormatException>(() => DateTools.StringToDate("97"));                 // no date
            Assert.Throws <FormatException>(() => DateTools.StringToDate("200401011235009999")); // no date
            Assert.Throws <FormatException>(() => DateTools.StringToDate("aaaa"));               // no date
        }
Пример #4
0
        public void TestDocumentationComments()
        {
            long unixEpochDate = 1095774611000;      // javadoc appears to be wrong - this is actually what changes the GMT time as below
            long ticks         = 632313714110000000; // 2004-09-21 13:50:11

            Assert.AreEqual(ticks, DateTools.UnixTimeMillisecondsToTicks(unixEpochDate));

            long ticksOut = DateTools.Round(ticks, DateResolution.MONTH, NumericRepresentation.TICKS, NumericRepresentation.TICKS);
            long expected = 1093996800000; // javadoc appears to be wrong - this is actually what the above is converted to
            long actual   = DateTools.TicksToUnixTimeMilliseconds(ticksOut);

            Assert.AreEqual(expected, actual);

            long unixEpochDateOut = DateTools.Round(unixEpochDate, DateResolution.MONTH, NumericRepresentation.UNIX_TIME_MILLISECONDS, NumericRepresentation.UNIX_TIME_MILLISECONDS);

            Assert.AreEqual(expected, unixEpochDateOut);
        }
Пример #5
0
        public virtual void TestDateToolsUTC_TicksAsMilliseconds()
        {
            // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London
            //long time = 1130630400;
            DateTime time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Utc);
            DateTime time2 = time1.AddHours(1);

            {
                TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Europe/London");
                string       d1       = DateTools.DateToString(time1, timeZone, DateResolution.MINUTE);
                string       d2       = DateTools.DateToString(time2, timeZone, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.Ticks / TimeSpan.TicksPerMillisecond, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.Ticks / TimeSpan.TicksPerMillisecond, "later");
            }

            {
                string d1 = DateTools.DateToString(time1, DateResolution.MINUTE);
                string d2 = DateTools.DateToString(time2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.Ticks / TimeSpan.TicksPerMillisecond, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.Ticks / TimeSpan.TicksPerMillisecond, "later");
            }

            time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
            time2 = time1.AddHours(1);

            {
                string d1 = DateTools.DateToString(time1, DateResolution.MINUTE);
                string d2 = DateTools.DateToString(time2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "later");
            }

            time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Unspecified);
            time2 = time1.AddHours(1);

            {
                string d1 = DateTools.DateToString(time1, DateResolution.MINUTE);
                string d2 = DateTools.DateToString(time2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "later");
            }
        }
Пример #6
0
        public virtual void TestStringtoTime()
        {
            long time = DateTools.StringToTime("197001010000");

            // we use default locale since LuceneTestCase randomizes it
            //Calendar cal = new GregorianCalendar(TimeZone.GetTimeZone("GMT"), Locale.Default);
            //cal.Clear();

            DateTime cal = new GregorianCalendar().ToDateTime(1970, 1, 1, 0, 0, 0, 0); // hour, minute, second -  year=1970, month=january, day=1

            //cal.set(DateTime.MILLISECOND, 0);
            Assert.AreEqual(cal.Ticks, time);

            cal = new GregorianCalendar().ToDateTime(1980, 2, 2, 11, 5, 0, 0); // hour, minute, second -  year=1980, month=february, day=2
            //cal.set(DateTime.MILLISECOND, 0);
            time = DateTools.StringToTime("198002021105");
            Assert.AreEqual(cal.Ticks, time);
        }
Пример #7
0
        public virtual void TestStringtoTime_UnixEpoch()
        {
            long time = DateTools.StringToTime("197001010000", NumericRepresentation.UNIX_TIME_MILLISECONDS);

            // we use default locale since LuceneTestCase randomizes it
            //Calendar cal = new GregorianCalendar(TimeZone.GetTimeZone("GMT"), Locale.Default);
            //cal.Clear();

            DateTime cal = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); //new GregorianCalendar().ToDateTime(1970, 1, 1, 0, 0, 0, 0); // hour, minute, second -  year=1970, month=january, day=1

            //cal.set(DateTime.MILLISECOND, 0);
            Assert.AreEqual(DateTools.TicksToUnixTimeMilliseconds(cal.Ticks), time);

            cal = new DateTime(1980, 2, 2, 11, 5, 0, 0, DateTimeKind.Utc); //new GregorianCalendar().ToDateTime(1980, 2, 2, 11, 5, 0, 0); // hour, minute, second -  year=1980, month=february, day=2
            //cal.set(DateTime.MILLISECOND, 0);
            time = DateTools.StringToTime("198002021105", NumericRepresentation.UNIX_TIME_MILLISECONDS);
            Assert.AreEqual(DateTools.TicksToUnixTimeMilliseconds(cal.Ticks), time);
        }
Пример #8
0
        public virtual void TestStringToDate()
        {
            DateTime d = default(DateTime);

            d = DateTools.StringToDate("2004");
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705");
            Assert.AreEqual("2004-07-05 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("200407050910");
            Assert.AreEqual("2004-07-05 09:10:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705091055990");
            Assert.AreEqual("2004-07-05 09:10:55:990", IsoFormat(d));

            try
            {
                d = DateTools.StringToDate("97"); // no date
                Assert.Fail();
            } // expected exception
#pragma warning disable 168
            catch (Exception e)
#pragma warning restore 168
            {
            }
            try
            {
                d = DateTools.StringToDate("200401011235009999"); // no date
                Assert.Fail();
            } // expected exception
#pragma warning disable 168
            catch (Exception e)
#pragma warning restore 168
            {
            }
            try
            {
                d = DateTools.StringToDate("aaaa"); // no date
                Assert.Fail();
            } // expected exception
#pragma warning disable 168
            catch (Exception e)
#pragma warning restore 168
            {
            }
        }
Пример #9
0
        public virtual void  TestStringToDate()
        {
            System.DateTime d;
            d = DateTools.StringToDate("2004");
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705");
            Assert.AreEqual("2004-07-05 00:00:00:000", IsoFormat(d));
            d = DateTools.StringToDate("200407050910");
            Assert.AreEqual("2004-07-05 09:10:00:000", IsoFormat(d));
            d = DateTools.StringToDate("20040705091055990");
            Assert.AreEqual("2004-07-05 09:10:55:990", IsoFormat(d));

            try
            {
                d = DateTools.StringToDate("97");                 // no date
                Assert.Fail();
            }
            catch (System.FormatException e)
            {
                /* expected exception */
            }
            try
            {
                d = DateTools.StringToDate("200401011235009999");                 // no date
                Assert.Fail();
            }
            catch (System.FormatException e)
            {
                /* expected exception */
            }
            try
            {
                d = DateTools.StringToDate("aaaa");                 // no date
                Assert.Fail();
            }
            catch (System.FormatException e)
            {
                /* expected exception */
            }
        }
Пример #10
0
        public virtual void TestDateToolsUTC()
        {
            /*// Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London
             * long time = 1130630400;
             * try
             * {
             *  TimeZone.Default = TimeZone.getTimeZone("Europe/London"); // "GMT"
             *  string d1 = DateTools.DateToString(new DateTime(time * 1000), DateTools.Resolution.MINUTE);
             *  string d2 = DateTools.DateToString(new DateTime((time+3600) * 1000), DateTools.Resolution.MINUTE);
             *  Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
             *  Assert.AreEqual(DateTools.StringToTime(d1), time * 1000, "midnight");
             *  Assert.AreEqual(DateTools.StringToTime(d2), (time+3600) * 1000, "later");
             * }
             * finally
             * {
             *  TimeZone.Default = null;
             * }*/

            // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London
            //long time = 1130630400;
            DateTime time1 = new DateTime(2005, 10, 30);
            DateTime time2 = time1.AddHours(1);

            try
            {
                //TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); // {{Aroush-2.0}} need porting 'java.util.TimeZone.getTimeZone'
                System.DateTime tempAux  = time1;
                System.String   d1       = DateTools.DateToString(tempAux, DateTools.Resolution.MINUTE);
                System.DateTime tempAux2 = time2;
                System.String   d2       = DateTools.DateToString(tempAux2, DateTools.Resolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1), time1.Ticks, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2), time2.Ticks, "later");
            }
            finally
            {
                //TimeZone.SetDefault(null);    // {{Aroush-2.0}} need porting 'java.util.TimeZone.setDefault'
            }
        }
Пример #11
0
        public virtual void  TestRound()
        {
            System.DateTime date = new System.DateTime(2004, 2, 3, 22, 8, 56, 333, new System.Globalization.GregorianCalendar());
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(date));

            System.DateTime dateYear = DateTools.Round(date, DateTools.Resolution.YEAR);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(dateYear));

            System.DateTime dateMonth = DateTools.Round(date, DateTools.Resolution.MONTH);
            Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(dateMonth));

            System.DateTime dateDay = DateTools.Round(date, DateTools.Resolution.DAY);
            Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(dateDay));

            System.DateTime dateHour = DateTools.Round(date, DateTools.Resolution.HOUR);
            Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(dateHour));

            System.DateTime dateMinute = DateTools.Round(date, DateTools.Resolution.MINUTE);
            Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(dateMinute));

            System.DateTime dateSecond = DateTools.Round(date, DateTools.Resolution.SECOND);
            Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(dateSecond));

            System.DateTime dateMillisecond = DateTools.Round(date, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(dateMillisecond));

            // long parameter:
            long dateYearLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.YEAR);

            System.DateTime tempAux = new System.DateTime(dateYearLong);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(tempAux));

            long dateMillisecondLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);

            System.DateTime tempAux2 = new System.DateTime(dateMillisecondLong);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(tempAux2));
        }
Пример #12
0
        public virtual void TestDateToolsUTC_DateTimeOffset_Ticks()
        {
            // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London
            //long time = 1130630400;
            DateTimeOffset time1 = new DateTimeOffset(2005, 10, 30, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset time2 = time1.AddHours(1);

            {
                TimeZoneInfo   timeZone = TZConvert.GetTimeZoneInfo("Europe/London");
                DateTimeOffset tempAux  = TimeZoneInfo.ConvertTime(time1, timeZone);
                string         d1       = DateTools.DateToString(tempAux, DateResolution.MINUTE);
                DateTimeOffset tempAux2 = TimeZoneInfo.ConvertTime(time2, timeZone);
                string         d2       = DateTools.DateToString(tempAux2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.Ticks, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.Ticks, "later");
            }

            {
                string d1 = DateTools.DateToString(time1, DateResolution.MINUTE);
                string d2 = DateTools.DateToString(time2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.Ticks, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.Ticks, "later");
            }

            time1 = new DateTimeOffset(2005, 10, 30, 0, 0, 0, TimeSpan.Zero).ToLocalTime();
            time2 = time1.AddHours(1);

            {
                string d1 = DateTools.DateToString(time1, DateResolution.MINUTE);
                string d2 = DateTools.DateToString(time2, DateResolution.MINUTE);
                Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times");
                Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.ToUniversalTime().Ticks, "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.ToUniversalTime().Ticks, "later");
            }
        }
Пример #13
0
 public override void SetDateResolution(ICommonQueryParserConfiguration cqpC,
     string field, DateTools.Resolution value)
 {
     Debug.Assert(cqpC is StandardQueryParser);
     StandardQueryParser qp = (StandardQueryParser)cqpC;
     qp.DateResolutionMap.Put(field, value);
 }
Пример #14
0
        public virtual void TestDateAndTimetoString()
        {
            // we use default locale since LuceneTestCase randomizes it
            //Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.Default);
            DateTime cal = new GregorianCalendar().ToDateTime(2004, 2, 3, 22, 8, 56, 333);

            /*cal.clear();
             * cal = new DateTime(2004, 1, 3, 22, 8, 56); // hour, minute, second -  year=2004, month=february(!), day=3
             * cal.set(DateTime.MILLISECOND, 333);*/

            string dateString = DateTools.DateToString(cal, DateTools.Resolution.YEAR);

            Assert.AreEqual("2004", dateString);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.MONTH);
            Assert.AreEqual("200402", dateString);
            Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.DAY);
            Assert.AreEqual("20040203", dateString);
            Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.HOUR);
            Assert.AreEqual("2004020322", dateString);
            Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.MINUTE);
            Assert.AreEqual("200402032208", dateString);
            Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.SECOND);
            Assert.AreEqual("20040203220856", dateString);
            Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("20040203220856333", dateString);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(DateTools.StringToDate(dateString)));

            // date before 1970:
            cal = new GregorianCalendar().ToDateTime(1961, 3, 5, 23, 9, 51, 444); // hour, minute, second -  year=1961, month=march(!), day=5
            //cal.set(DateTime.MILLISECOND, 444);
            dateString = DateTools.DateToString(cal, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19610305230951444", dateString);
            Assert.AreEqual("1961-03-05 23:09:51:444", IsoFormat(DateTools.StringToDate(dateString)));

            dateString = DateTools.DateToString(cal, DateTools.Resolution.HOUR);
            Assert.AreEqual("1961030523", dateString);
            Assert.AreEqual("1961-03-05 23:00:00:000", IsoFormat(DateTools.StringToDate(dateString)));

            // timeToString:
            cal = new GregorianCalendar().ToDateTime(1970, 1, 1, 0, 0, 0, 0); // hour, minute, second -  year=1970, month=january, day=1
            //cal.set(DateTime.MILLISECOND, 0);
            dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19700101000000000", dateString);

            cal = new GregorianCalendar().ToDateTime(1970, 1, 1, 1, 2, 3, 0); // hour, minute, second -  year=1970, month=january, day=1
            //cal.set(DateTime.MILLISECOND, 0);
            dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19700101010203000", dateString);
        }
        /// <summary>
        /// This method generate the fields for indexing documents in lucene from the values.
        /// Given a name and a value, it has the following behavior:
        /// * If the value is enumerable, index all the items in the enumerable under the same field name
        /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE'
        /// * If the value is string or was set to not analyzed, create a single field with the supplied name
        /// * If the value is date, create a single field with millisecond precision with the supplied name
        /// * If the value is numeric (int, long, double, decimal, or float) will create two fields:
        ///		1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries
        ///		2. with the name: name +'_Range', containing the numeric value in a form that allows range queries
        /// </summary>
        private static IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name");
            }

            if (char.IsLetter(name[0]) == false &&
                name[0] != '_')
            {
                name = "_" + name;
            }

            if (value == null)
            {
                yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                       Field.Index.NOT_ANALYZED));

                yield break;
            }
            if (value is DynamicNullObject)
            {
                if (((DynamicNullObject)value).IsExplicitNull)
                {
                    yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                           Field.Index.NOT_ANALYZED));
                }
                yield break;
            }

            if (value is AbstractField)
            {
                yield return((AbstractField)value);

                yield break;
            }


            var itemsToIndex = value as IEnumerable;

            if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
            {
                yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                foreach (var itemToIndex in itemsToIndex)
                {
                    foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage))
                    {
                        yield return(field);
                    }
                }
                yield break;
            }

            if (indexDefinition.GetIndex(name, null) == Field.Index.NOT_ANALYZED)// explicitly not analyzed
            {
                if (value is DateTime)
                {
                    var val = (DateTime)value;
                    yield return(new Field(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage),
                                           indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
                }
                else if (value is DateTimeOffset)
                {
                    var val = (DateTimeOffset)value;
                    yield return(new Field(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage),
                                           indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
                }
                else
                {
                    yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                           indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
                }
                yield break;
            }
            if (value is string)
            {
                var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED);
                yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       index));

                yield break;
            }

            if (value is DateTime)
            {
                yield return(new Field(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND),
                                       indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is DateTimeOffset)
            {
                yield return(new Field(name, DateTools.DateToString(((DateTimeOffset)value).DateTime, DateTools.Resolution.MILLISECOND),
                                       indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is bool)
            {
                yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is IConvertible)            // we need this to store numbers in invariant format, so JSON could read them
            {
                var convert = ((IConvertible)value);
                yield return(new Field(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is DynamicJsonObject)
            {
                var inner = ((DynamicJsonObject)value).Inner;
                yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(new Field(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else
            {
                yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(new Field(name, RavenJToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }


            var numericField = new NumericField(name + "_Range", indexDefinition.GetStorage(name, defaultStorage), true);

            if (value is int)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue((int)value));
                }
                else
                {
                    yield return(numericField.SetIntValue((int)value));
                }
            }
            if (value is long)
            {
                yield return(numericField
                             .SetLongValue((long)value));
            }
            if (value is decimal)
            {
                yield return(numericField
                             .SetDoubleValue((double)(decimal)value));
            }
            if (value is float)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((float)value));
                }
                else
                {
                    yield return(numericField.SetFloatValue((float)value));
                }
            }
            if (value is double)
            {
                yield return(numericField
                             .SetDoubleValue((double)value));
            }
        }
Пример #16
0
 public abstract void SetDateResolution(ICommonQueryParserConfiguration cqpC, string field, DateTools.Resolution value);
Пример #17
0
        /// <summary>
        /// This method generate the fields for indexing documents in lucene from the values.
        /// Given a name and a value, it has the following behavior:
        /// * If the value is enumerable, index all the items in the enumerable under the same field name
        /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE'
        /// * If the value is string or was set to not analyzed, create a single field with the supplied name
        /// * If the value is date, create a single field with millisecond precision with the supplied name
        /// * If the value is numeric (int, long, double, decimal, or float) will create two fields:
        ///		1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries
        ///		2. with the name: name +'_Range', containing the numeric value in a form that allows range queries
        /// </summary>
        public IEnumerable <AbstractField> CreateFields(string name, object value, Field.Store defaultStorage, bool nestedArray = false)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name");
            }

            if (char.IsLetter(name[0]) == false &&
                name[0] != '_')
            {
                name = "_" + name;
            }

            var storage = indexDefinition.GetStorage(name, defaultStorage);

            if (value == null)
            {
                yield return(CreateFieldWithCaching(name, Constants.NullValue, storage,
                                                    Field.Index.NOT_ANALYZED_NO_NORMS));

                yield break;
            }
            if (Equals(value, string.Empty))
            {
                yield return(CreateFieldWithCaching(name, Constants.EmptyString, storage,
                                                    Field.Index.NOT_ANALYZED_NO_NORMS));

                yield break;
            }
            var dynamicNullObject = value as DynamicNullObject;

            if (ReferenceEquals(dynamicNullObject, null) == false)
            {
                if ((dynamicNullObject).IsExplicitNull)
                {
                    yield return(CreateFieldWithCaching(name, Constants.NullValue, storage,
                                                        Field.Index.NOT_ANALYZED_NO_NORMS));
                }
                yield break;
            }
            var boostedValue = value as BoostedValue;

            if (boostedValue != null)
            {
                foreach (var field in CreateFields(name, boostedValue.Value, storage))
                {
                    field.SetBoost(boostedValue.Boost);
                    field.SetOmitNorms(false);
                    yield return(field);
                }
                yield break;
            }

            var abstractField = value as AbstractField;

            if (abstractField != null)
            {
                yield return(abstractField);

                yield break;
            }
            var bytes = value as byte[];

            if (bytes != null)
            {
                yield return(CreateBinaryFieldWithCaching(name, bytes, storage));

                yield break;
            }

            var itemsToIndex = value as IEnumerable;

            if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
            {
                if (nestedArray == false && !Equals(storage, Field.Store.NO))
                {
                    yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                }
                int count = 1;
                foreach (var itemToIndex in itemsToIndex)
                {
                    multipleItemsSameFieldCount.Add(count++);
                    foreach (var field in CreateFields(name, itemToIndex, storage, nestedArray: true))
                    {
                        yield return(field);
                    }
                    multipleItemsSameFieldCount.RemoveAt(multipleItemsSameFieldCount.Count - 1);
                }
                yield break;
            }

            var fieldIndexingOptions = indexDefinition.GetIndex(name, null);

            if (Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED) ||
                Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED_NO_NORMS))            // explicitly not analyzed
            {
                if (value is DateTime)
                {
                    var val     = (DateTime)value;
                    var postFix = val.Kind == DateTimeKind.Utc ? "Z" : "";
                    yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite) + postFix, storage,
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                else if (value is DateTimeOffset)
                {
                    var val = (DateTimeOffset)value;
                    yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeOffsetFormatsToWrite), storage,
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                else
                {
                    yield return(CreateFieldWithCaching(name, value.ToString(), storage,
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                yield break;
            }
            if (value is string)
            {
                var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED);
                yield return(CreateFieldWithCaching(name, value.ToString(), storage,
                                                    index));

                yield break;
            }

            if (value is DateTime)
            {
                yield return(CreateFieldWithCaching(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND),
                                                    storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is DateTimeOffset)
            {
                yield return(CreateFieldWithCaching(name, DateTools.DateToString(((DateTimeOffset)value).UtcDateTime, DateTools.Resolution.MILLISECOND),
                                                    storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is bool)
            {
                yield return(new Field(name, ((bool)value) ? "true" : "false", storage,
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is IConvertible)            // we need this to store numbers in invariant format, so JSON could read them
            {
                var convert = ((IConvertible)value);
                yield return(CreateFieldWithCaching(name, convert.ToString(CultureInfo.InvariantCulture), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is IDynamicJsonObject)
            {
                var inner = ((IDynamicJsonObject)value).Inner;
                yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(CreateFieldWithCaching(name, inner.ToString(), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else
            {
                yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(CreateFieldWithCaching(name, RavenJToken.FromObject(value).ToString(), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }


            foreach (var numericField in CreateNumericFieldWithCaching(name, value, storage))
            {
                yield return(numericField);
            }
        }
Пример #18
0
 public void assertDateRangeQueryEquals(PrecedenceQueryParser qp, String field,
     String startDate, String endDate, DateTime endDateInclusive,
     DateTools.Resolution resolution)
 {
     assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate)
         + " TO " + escapeDateString(endDate) + "]", "["
         + getDate(startDate, resolution) + " TO "
         + getDate(endDateInclusive, resolution) + "]");
     assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate)
         + " TO " + escapeDateString(endDate) + "}", "{"
         + getDate(startDate, resolution) + " TO "
         + getDate(endDate, resolution) + "}");
 }
Пример #19
0
 public void AssertDateRangeQueryEquals(StandardQueryParser qp,
     String field, String startDate, String endDate, DateTime endDateInclusive,
     DateTools.Resolution resolution)
 {
     AssertQueryEquals(qp, field, field + ":[" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate)
         + "]", "[" + GetDate(startDate, resolution) + " TO "
         + GetDate(endDateInclusive, resolution) + "]");
     AssertQueryEquals(qp, field, field + ":{" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate)
         + "}", "{" + GetDate(startDate, resolution) + " TO "
         + GetDate(endDate, resolution) + "}");
 }
Пример #20
0
 /** for testing DateTools support */
 private String GetDate(DateTime d, DateTools.Resolution resolution)
 {
     return DateTools.DateToString(d, resolution);
 }
Пример #21
0
        /** for testing DateTools support */
        private String GetDate(String s, DateTools.Resolution resolution)

        {
            // we use the default Locale since LuceneTestCase randomizes it
            //DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
            //return getDate(df.parse(s), resolution);

            return GetDate(DateTime.Parse(s), resolution); // TODO: Locale...
        }
Пример #22
0
 public override void SetDateResolution(ICommonQueryParserConfiguration cqpC, string field, DateTools.Resolution value)
 {
     Debug.Assert(cqpC is QueryParser);
     QueryParser qp = (QueryParser)cqpC;
     qp.SetDateResolution(field, value);
 }
Пример #23
0
        /// <summary>
        /// This method generate the fields for indexing documents in lucene from the values.
        /// Given a name and a value, it has the following behavior:
        /// * If the value is enumerable, index all the items in the enumerable under the same field name
        /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE'
        /// * If the value is string or was set to not analyzed, create a single field with the supplied name
        /// * If the value is date, create a single field with millisecond precision with the supplied name
        /// * If the value is numeric (int, long, double, decimal, or float) will create two fields:
        ///		1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries
        ///		2. with the name: name +'_Range', containing the numeric value in a form that allows range queries
        /// </summary>
        private IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name");
            }

            if (char.IsLetter(name[0]) == false &&
                name[0] != '_')
            {
                name = "_" + name;
            }

            if (value == null)
            {
                yield return(CreateFieldWithCaching(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                                    Field.Index.NOT_ANALYZED_NO_NORMS));

                yield break;
            }
            if (Equals(value, string.Empty))
            {
                yield return(CreateFieldWithCaching(name, Constants.EmptyString, indexDefinition.GetStorage(name, defaultStorage),
                                                    Field.Index.NOT_ANALYZED_NO_NORMS));

                yield break;
            }
            if (value is DynamicNullObject)
            {
                if (((DynamicNullObject)value).IsExplicitNull)
                {
                    yield return(CreateFieldWithCaching(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                                        Field.Index.NOT_ANALYZED_NO_NORMS));
                }
                yield break;
            }

            if (value is AbstractField)
            {
                yield return((AbstractField)value);

                yield break;
            }


            var itemsToIndex = value as IEnumerable;

            if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
            {
                yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                int count = 1;
                foreach (var itemToIndex in itemsToIndex)
                {
                    multipleItemsSameFieldCount.Add(count++);
                    foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage))
                    {
                        yield return(field);
                    }
                    multipleItemsSameFieldCount.RemoveAt(multipleItemsSameFieldCount.Count - 1);
                }
                yield break;
            }

            var fieldIndexingOptions = indexDefinition.GetIndex(name, null);

            if (fieldIndexingOptions == Field.Index.NOT_ANALYZED || fieldIndexingOptions == Field.Index.NOT_ANALYZED_NO_NORMS)            // explicitly not analyzed
            {
                if (value is DateTime)
                {
                    var val = (DateTime)value;
                    yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage),
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                else if (value is DateTimeOffset)
                {
                    var val = (DateTimeOffset)value;
                    yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage),
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                else
                {
                    yield return(CreateFieldWithCaching(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
                }
                yield break;
            }
            if (value is string)
            {
                var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED);
                yield return(CreateFieldWithCaching(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                                    index));

                yield break;
            }

            if (value is DateTime)
            {
                yield return(CreateFieldWithCaching(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND),
                                                    indexDefinition.GetStorage(name, defaultStorage),
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is DateTimeOffset)
            {
                yield return(CreateFieldWithCaching(name, DateTools.DateToString(((DateTimeOffset)value).DateTime, DateTools.Resolution.MILLISECOND),
                                                    indexDefinition.GetStorage(name, defaultStorage),
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is bool)
            {
                yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is IConvertible)            // we need this to store numbers in invariant format, so JSON could read them
            {
                var convert = ((IConvertible)value);
                yield return(CreateFieldWithCaching(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage),
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else if (value is IDynamicJsonObject)
            {
                var inner = ((IDynamicJsonObject)value).Inner;
                yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(CreateFieldWithCaching(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }
            else
            {
                yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(CreateFieldWithCaching(name, RavenJToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS)));
            }


            foreach (var numericField in CreateNumericFieldWithCaching(name, value, defaultStorage))
            {
                yield return(numericField);
            }
        }
Пример #24
0
        /// <summary>
        /// Sets the date resolution used by RangeQueries for a specific field.
        /// </summary>
        /// <param name="fieldName">field for which the date resolution is to be set</param>
        /// <param name="dateResolution">date resolution to set</param>
        public virtual void SetDateResolution(string fieldName, DateTools.Resolution dateResolution)
        {
            if (string.IsNullOrEmpty(fieldName))
            {
                throw new ArgumentNullException("fieldName cannot be null or empty string.");
            }

            if (fieldToDateResolution == null)
            {
                // lazily initialize Dictionary
                fieldToDateResolution = new Dictionary<string, DateTools.Resolution>();
            }

            fieldToDateResolution[fieldName] = dateResolution;
        }
Пример #25
0
		/// <summary> Sets the default date resolution used by RangeQueries for fields for which no
		/// specific date resolutions has been set. Field specific resolutions can be set
		/// with {@link #SetDateResolution(String, DateTools.Resolution)}.
		/// 
		/// </summary>
		/// <param name="dateResolution">the default date resolution to set
		/// </param>
		public virtual void  SetDateResolution(DateTools.Resolution dateResolution)
		{
			this.dateResolution = dateResolution;
		}
Пример #26
0
		/// <summary> Sets the date resolution used by RangeQueries for a specific field.
		/// 
		/// </summary>
		/// <param name="field">field for which the date resolution is to be set 
		/// </param>
		/// <param name="dateResolution">date resolution to set
		/// </param>
		public virtual void  SetDateResolution(System.String fieldName, DateTools.Resolution dateResolution)
		{
			if (fieldName == null)
			{
				throw new System.ArgumentException("Field cannot be null.");
			}
			
			if (fieldToDateResolution == null)
			{
				// lazily initialize HashMap
				fieldToDateResolution = new System.Collections.Hashtable();
			}
			
			fieldToDateResolution[fieldName] = dateResolution;
		}
Пример #27
0
        public virtual void  TestDateAndTimetoString()
        {
            System.DateTime cal = new System.DateTime(2004, 2, 3, 22, 8, 56, 333, new System.Globalization.GregorianCalendar());

            System.String   dateString;
            System.DateTime tempAux = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux, DateTools.Resolution.YEAR);
            Assert.AreEqual("2004", dateString);
            System.DateTime tempAux2 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(tempAux2));

            System.DateTime tempAux3 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux3, DateTools.Resolution.MONTH);
            Assert.AreEqual("200402", dateString);
            System.DateTime tempAux4 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(tempAux4));

            System.DateTime tempAux5 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux5, DateTools.Resolution.DAY);
            Assert.AreEqual("20040203", dateString);
            System.DateTime tempAux6 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(tempAux6));

            System.DateTime tempAux7 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux7, DateTools.Resolution.HOUR);
            Assert.AreEqual("2004020322", dateString);
            System.DateTime tempAux8 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(tempAux8));

            System.DateTime tempAux9 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux9, DateTools.Resolution.MINUTE);
            Assert.AreEqual("200402032208", dateString);
            System.DateTime tempAux10 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(tempAux10));

            System.DateTime tempAux11 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux11, DateTools.Resolution.SECOND);
            Assert.AreEqual("20040203220856", dateString);
            System.DateTime tempAux12 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(tempAux12));

            System.DateTime tempAux13 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux13, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("20040203220856333", dateString);
            System.DateTime tempAux14 = DateTools.StringToDate(dateString);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(tempAux14));

            // date before 1970:
            cal = new System.DateTime(1961, 3, 5, 23, 9, 51, 444, new System.Globalization.GregorianCalendar());
            System.DateTime tempAux15 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux15, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19610305230951444", dateString);
            System.DateTime tempAux16 = DateTools.StringToDate(dateString);
            Assert.AreEqual("1961-03-05 23:09:51:444", IsoFormat(tempAux16));

            System.DateTime tempAux17 = new System.DateTime(cal.Ticks);
            dateString = DateTools.DateToString(tempAux17, DateTools.Resolution.HOUR);
            Assert.AreEqual("1961030523", dateString);
            System.DateTime tempAux18 = DateTools.StringToDate(dateString);
            Assert.AreEqual("1961-03-05 23:00:00:000", IsoFormat(tempAux18));

            // timeToString:
            cal        = new System.DateTime(1970, 1, 1, 0, 0, 0, 0, new System.Globalization.GregorianCalendar());
            dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19700101000000000", dateString);

            cal        = new System.DateTime(1970, 1, 1, 1, 2, 3, 0, new System.Globalization.GregorianCalendar());
            dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND);
            Assert.AreEqual("19700101010203000", dateString);
        }
Пример #28
0
        /// <summary>for testing DateTools support</summary>
        private string GetDate(string s, DateTools.Resolution resolution)
        {
            // TODO: Is this the correct way to parse the string?
            DateTime d = DateTime.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
            return GetDate(d, resolution);

            //// we use the default Locale since LuceneTestCase randomizes it
            //DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
            //return GetDate(df.Parse(s), resolution);      
        }