Exemplo n.º 1
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.º 2
0
        public virtual void TestDateToolsUTC_UnixEpoch()
        {
            // 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), DateTools.TicksToUnixTimeMilliseconds(time1.Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2), DateTools.TicksToUnixTimeMilliseconds(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), DateTools.TicksToUnixTimeMilliseconds(time1.Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2), DateTools.TicksToUnixTimeMilliseconds(time2.Ticks), "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), DateTools.TicksToUnixTimeMilliseconds(time1.ToUniversalTime().Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2), DateTools.TicksToUnixTimeMilliseconds(time2.ToUniversalTime().Ticks), "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), DateTools.TicksToUnixTimeMilliseconds(time1.ToUniversalTime().Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2), DateTools.TicksToUnixTimeMilliseconds(time2.ToUniversalTime().Ticks), "later");
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public virtual void TestDateToolsUTC_DateTimeOffset_UnixEpoch()
        {
            // 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.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(time1.Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(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.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(time1.Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(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.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(time1.ToUniversalTime().Ticks), "midnight");
                Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.UNIX_TIME_MILLISECONDS), DateTools.TicksToUnixTimeMilliseconds(time2.ToUniversalTime().Ticks), "later");
            }
        }
Exemplo n.º 5
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 = new DateTime(2004, 2, 3, 22, 8, 56, 333, DateTimeKind.Utc);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // long parameter:

            // ticks:

            long dateYearLong = DateTools.Round(date.Ticks, DateResolution.YEAR, NumericRepresentation.TICKS, NumericRepresentation.TICKS);

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

            long dateMillisecondLong = DateTools.Round(date.Ticks, DateResolution.MILLISECOND, NumericRepresentation.TICKS, NumericRepresentation.TICKS);

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

            // unix epoch:

            dateYearLong = DateTools.Round(DateTools.TicksToUnixTimeMilliseconds(date.Ticks), DateResolution.YEAR, NumericRepresentation.UNIX_TIME_MILLISECONDS, NumericRepresentation.TICKS);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(new DateTime(dateYearLong)));

            dateMillisecondLong = DateTools.Round(DateTools.TicksToUnixTimeMilliseconds(date.Ticks), DateResolution.MILLISECOND, NumericRepresentation.UNIX_TIME_MILLISECONDS, NumericRepresentation.TICKS);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(new DateTime(dateMillisecondLong)));

            // ticks as ms:

            dateYearLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateResolution.YEAR, NumericRepresentation.TICKS_AS_MILLISECONDS, NumericRepresentation.TICKS);
            Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(new DateTime(dateYearLong)));

            dateMillisecondLong = DateTools.Round(date.Ticks / TimeSpan.TicksPerMillisecond, DateResolution.MILLISECOND, NumericRepresentation.TICKS_AS_MILLISECONDS, NumericRepresentation.TICKS);
            Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(new DateTime(dateMillisecondLong)));
        }
Exemplo n.º 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(GregorianCalendarTypes.Localized).ToDateTime(2004, 2, 3, 22, 8, 56, 333);
            DateTime cal = new DateTime(2004, 2, 3, 22, 8, 56, 333, DateTimeKind.Utc);


            /*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, DateResolution.YEAR);

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

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

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

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

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

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

            dateString = DateTools.DateToString(cal, DateResolution.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);
            cal        = new DateTime(1961, 3, 5, 23, 9, 51, 444, DateTimeKind.Utc);
            dateString = DateTools.DateToString(cal, DateResolution.MILLISECOND);
            Assert.AreEqual("19610305230951444", dateString);
            Assert.AreEqual("1961-03-05 23:09:51:444", IsoFormat(DateTools.StringToDate(dateString)));

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

            // timeToString:

            // ticks:

            //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);
            cal        = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            dateString = DateTools.TimeToString(cal.Ticks, DateResolution.MILLISECOND, NumericRepresentation.TICKS);
            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, DateResolution.MILLISECOND, NumericRepresentation.TICKS);
            Assert.AreEqual("19700101010203000", dateString);

            // unix epoch:

            cal        = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            dateString = DateTools.TimeToString(DateTools.TicksToUnixTimeMilliseconds(cal.Ticks), DateResolution.MILLISECOND, NumericRepresentation.UNIX_TIME_MILLISECONDS);
            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(DateTools.TicksToUnixTimeMilliseconds(cal.Ticks), DateResolution.MILLISECOND, NumericRepresentation.UNIX_TIME_MILLISECONDS);
            Assert.AreEqual("19700101010203000", dateString);

            // ticks as ms:

            cal        = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateResolution.MILLISECOND, NumericRepresentation.TICKS_AS_MILLISECONDS);
            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, DateResolution.MILLISECOND, NumericRepresentation.TICKS_AS_MILLISECONDS);
            Assert.AreEqual("19700101010203000", dateString);
        }