Ejemplo n.º 1
0
        public void TestMinMaxVsDateTime()
        {
            HdDateTime maxValue = HdDateTime.MaxValue;
            HdDateTime minValue = HdDateTime.MinValue;

            // Min/Max casts are a special case, treated like +Inf, -Inf, should be equal
            Assert.True(HdDateTime.MaxValue == DateTime.MaxValue);
            Assert.True(HdDateTime.MinValue == DateTime.MinValue);

            for (int i = 1; i < 0x400; ++i)
            // But in reality HdDateTime representable time interval is much smaller
            // and we are going to test this and also the validity of our typecasts
            {
                // Exceptions are slow, so don't test each iteration
                if (i < 120 || (i & 3) == 0)
                {
                    Assert.Throws <ArgumentOutOfRangeException>(
                        () => ToVoid((HdDateTime)DateTime.MaxValue.AddTicks(-i)));
                    Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                                ToVoid((HdDateTime)DateTime.MinValue.AddTicks(i)));

                    // Maybe there is a point in implementing comparison with out-of-range DateTime
                    // but currently this throws
                    Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                                ToVoid(maxValue < (HdDateTime)DateTime.MaxValue.AddTicks(-i)));
                    Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                                ToVoid(minValue > (HdDateTime)DateTime.MinValue.AddTicks(i)));
                }

                Assert.True(maxValue > ((HdDateTime)DateTime.MaxValue).AddNanoseconds(-i));
                Assert.True(minValue < ((HdDateTime)DateTime.MinValue).AddNanoseconds(i));
                Assert.True(maxValue > ((HdDateTime)DateTime.MaxValue).AddTicks(-i));
                Assert.True(minValue < ((HdDateTime)DateTime.MinValue).AddTicks(i));

                Assert.True(maxValue.AddNanoseconds(-i) < DateTime.MaxValue);
                Assert.True(minValue.AddNanoseconds(i) > DateTime.MinValue);
                Assert.True(maxValue.AddTicks(-i) < DateTime.MaxValue);
                Assert.True(minValue.AddTicks(i) > DateTime.MinValue);
                Assert.True(maxValue.AddMilliseconds(-i) < DateTime.MaxValue);
                Assert.True(minValue.AddMilliseconds(i) > DateTime.MinValue);

                Assert.True((DateTime)maxValue.AddNanoseconds(-i) < DateTime.MaxValue);
                Assert.True((DateTime)minValue.AddNanoseconds(i) > DateTime.MinValue);
                Assert.True((DateTime)maxValue.AddTicks(-i) < DateTime.MaxValue);
                Assert.True((DateTime)minValue.AddTicks(i) > DateTime.MinValue);
                Assert.True((DateTime)maxValue.AddMilliseconds(-i) < DateTime.MaxValue);
                Assert.True((DateTime)minValue.AddMilliseconds(i) > DateTime.MinValue);

                // Must add at least 1 nanosecond to avoid Max<=>Max Min<=>Min conversion
                Assert.True(((DateTime)maxValue.AddNanoseconds(-1)).AddTicks(-i) < DateTime.MaxValue);
                Assert.True(((DateTime)maxValue.AddNanoseconds(-1)).AddTicks(i) < DateTime.MaxValue);
                Assert.True(((DateTime)minValue.AddNanoseconds(1)).AddTicks(-i) > DateTime.MinValue);
                Assert.True(((DateTime)minValue.AddNanoseconds(1)).AddTicks(i) > DateTime.MinValue);
            }
        }
Ejemplo n.º 2
0
        static public void Scratchpad()
        {
            var        dt  = DateTime.Now;
            HdDateTime hdt = DateTime.UtcNow;
            var        ts  = TimeSpan.FromSeconds(123);
            var        hts = HdTimeSpan.FromDays(123L);

            //ts.Days
            //dt.Day
            //dt.AddDays()
            //HdTimeSpan.
            Console.WriteLine(HdDateTime.MinValue);
            Console.WriteLine(HdDateTime.MaxValue);
            Console.WriteLine(new HdDateTime(1678, 1, 1).EpochNanoseconds);
            Console.WriteLine(new HdDateTime(2262, 1, 1).EpochNanoseconds);
            Console.WriteLine(new HdDateTime(2262, 1, 1).AddNanoseconds(-1));
            Console.WriteLine(HdDateTime.MaxValue);

            Console.WriteLine(hdt);
            Console.WriteLine(hdt.AddTicks(1));
            Console.WriteLine(hdt.AddMilliseconds(1));
            Console.WriteLine(hdt.AddSeconds(1));
            Console.WriteLine(hdt.AddHours(12));
            Console.WriteLine(hdt.AddDays(123));

            int a = hdt.Millisecond, b = hdt.Second, c = hdt.Minute, d = hdt.Hour, e = hdt.Day, f = hdt.Month, g = hdt.Year, h = hdt.DayOfYear;

            hdt = hdt.Date;
            HdTimeSpan tsss = hdt.TimeOfDay;

            DayOfWeek dw = hdt.DayOfWeek;

            //dt.AddTicks();
            //hdt.AddTicks()
        }
Ejemplo n.º 3
0
        public void PrintMinMaxConstants()
        {
            HdDateTime maxValue     = HdDateTime.MaxValue;
            HdDateTime minValue     = HdDateTime.MinValue;
            var        tickNanosMin = 100 - ((DateTime)minValue.AddTicks(1) - minValue).TotalNanoseconds;
            var        tickNanosMax = (maxValue.AddTicks(-1) - (DateTime)(maxValue.AddTicks(-1))).TotalNanoseconds;

            var secondNanosMin = 1000000000 + minValue.AddSeconds(1).EpochNanoseconds % 1000000000;
            var secondNanosMax = maxValue.EpochNanoseconds % 1000000000;

            Console.WriteLine($"HDT MinValue       = {minValue.AddSeconds(1)} - 1s, {tickNanosMin} / {HdDateTime.MinValue.EpochNanoseconds}");
            Console.WriteLine($"HDT MinValue       = {minValue.RawDateTime} {secondNanosMin}");
            Console.WriteLine();
            Console.WriteLine($"HDT MaxValue       = {maxValue.AddSeconds(-1)} + 1s, {tickNanosMax} / {HdDateTime.MaxValue.EpochNanoseconds}");
            Console.WriteLine($"HDT MaxValue       = {maxValue.RawDateTime} {secondNanosMax}");
        }
Ejemplo n.º 4
0
        public void TestMinMaxConstantsExtra()
        {
            HdDateTime maxValue = HdDateTime.MaxValue;
            HdDateTime minValue = HdDateTime.MinValue;

            var nanosMin = 100 - ((DateTime)minValue.AddTicks(1) - minValue).TotalNanoseconds;
            var nanosMax = (maxValue.AddTicks(-1) - (DateTime)(maxValue.AddTicks(-1))).TotalNanoseconds;

            var secondNanosMin = 1000000000 + minValue.AddSeconds(1).EpochNanoseconds % 1000000000;
            var secondNanosMax = maxValue.EpochNanoseconds % 1000000000;

            Assert.True(nanosMax == maxValue.AddTicks(-1).TimestampModulo);
            Assert.True(nanosMin == minValue.AddTicks(1).TimestampModulo);

            Assert.True(nanosMax == secondNanosMax % 100);
            Assert.True(nanosMin == secondNanosMin % 100);

            Assert.AreEqual(minValue.EpochNanoseconds, new HdDateTime((DateTime)minValue.AddTicks(1), (Byte)nanosMin).AddTicks(-1).EpochNanoseconds);
            Assert.True(minValue == new HdDateTime((DateTime)minValue.AddTicks(1), (Byte)nanosMin).AddTicks(-1));
            Assert.AreEqual(maxValue.EpochNanoseconds, new HdDateTime((DateTime)maxValue.AddTicks(-1), (Byte)nanosMax).AddTicks(1).EpochNanoseconds);
            Assert.True(maxValue == new HdDateTime((DateTime)maxValue.AddTicks(-1), (Byte)nanosMax).AddTicks(1));

            Assert.AreEqual(maxValue.EpochNanoseconds, HdDateTime.FromEpochMilliseconds(maxValue.EpochNanoseconds / 1000000000 * 1000, secondNanosMax).EpochNanoseconds);
        }