Пример #1
0
        public void TestLuceneCompatibility()
        {
            long unixDate = 1075846136333L;

            string convertedDate = DateTools.TimeToString(unixDate, DateResolution.MILLISECOND, NumericRepresentation.UNIX_TIME_MILLISECONDS);
            string expectedDate  = "20040203220856333";

            Assert.AreEqual(expectedDate, convertedDate);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(DateTools.StringToDate(convertedDate)));
            Assert.AreEqual(unixDate, DateTools.StringToTime(convertedDate));
        }
Пример #2
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
        }
Пример #3
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
            {
            }
        }
Пример #4
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 */
            }
        }
Пример #5
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);
        }
Пример #6
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);
        }