Exemplo n.º 1
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)));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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));
        }