Exemple #1
0
        public void TestMethodSubtract()
        {
            Random rnd = new Random();

            for (int i = 0; i < 1000; i++)
            {
                var        datetime = new DateTime(rnd.Next(1970, 3000), rnd.Next(1, 13), rnd.Next(1, 29), rnd.Next(0, 24), rnd.Next(0, 60), rnd.Next(0, 60), (DateTimeKind)rnd.Next(0, 3));
                UnixTime64 ut       = datetime;

                for (int n = 0; n < 10;)
                {
                    long seconds = (long)((rnd.NextDouble() * 2 - 1) * 1000 * 365 * TimeSpan.TicksPerDay / TimeSpan.TicksPerSecond);

                    try
                    {
                        var test = datetime - TimeSpan.FromSeconds(seconds);
                    }
                    catch (Exception ex)
                    {
                        ///OverflowException at TimeSpan.FromSeconds()
                        ///The added or subtracted value results in an un-representable DateTime.
                        continue;
                    }

                    var oldDT = datetime;
                    var oldUT = ut;

                    switch (n % 2)
                    {
                    case 0:
                        ut      -= seconds;
                        datetime = datetime.AddTicks(-seconds * TimeSpan.TicksPerSecond);
                        break;

                    case 1:
                        var ts = TimeSpan.FromSeconds(seconds);
                        ut      -= ts;
                        datetime = datetime - ts;
                        break;
                    }

                    n++;
                    Assert.AreEqual(datetime, (DateTime)ut);
                    Assert.AreEqual(true, ut == datetime);
                    Assert.AreEqual(true, ut == ut.TimeStamp);
                    Assert.AreEqual(false, ut != datetime);
                    Assert.AreEqual(false, ut != ut.TimeStamp);
                }
            }
        }
Exemple #2
0
        public void TestMethodParse()
        {
            Random rnd = new Random();

            for (int i = 0; i < 1000; i++)
            {
                var datetime = new DateTime(rnd.Next(1970, 2035), rnd.Next(1, 13), rnd.Next(1, 29), rnd.Next(0, 24), rnd.Next(0, 60), rnd.Next(0, 60), (DateTimeKind)rnd.Next(0, 3));
                var dts      = datetime.ToString(Cave.UnixTime.UnixTime.InterOpDateTimeFormat);
                var ut       = (UnixTime64)datetime;
                var uts      = ut.ToString();
                var utb      = UnixTime64.Parse(uts);

                Assert.AreEqual(datetime, (DateTime)ut);
                Assert.AreEqual(datetime, (DateTime)utb);
                Assert.AreEqual(dts, uts);
            }
        }
Exemple #3
0
        public void TestMethodOverflow()
        {
            var        datetime = DateTime.Now;
            UnixTime64 ut       = datetime;

            try
            {
                ut += long.MaxValue;
            }
            catch (Exception ex)
            {
                Assert.AreEqual(typeof(OverflowException), ex.GetType());
            }
            try
            {
                ut -= long.MaxValue;
            }
            catch (Exception ex)
            {
                Assert.AreEqual(typeof(OverflowException), ex.GetType());
            }

            for (int i = -9; i < 10; i++)
            {
                DateTime check = new DateTime(1970, 1, 1) + TimeSpan.FromSeconds((uint.MaxValue + 1L) * i);
                Assert.AreEqual(check, UnixTime64.Convert(0, DateTimeKind.Local, i));
                Assert.AreEqual(check, UnixTime64.Convert(0, DateTimeKind.Utc, i));
                Assert.AreEqual(check, UnixTime64.Convert(0, DateTimeKind.Unspecified, i));
            }

            for (int i = -9; i < 10; i++)
            {
                TimeSpan zone  = new TimeSpan(2, 0, 0);
                DateTime check = new DateTime(1970, 1, 1) + TimeSpan.FromSeconds((uint.MaxValue + 1L) * i) - zone;
                Assert.AreEqual(check, UnixTime64.ConvertToUTC(0, zone, i));
                Assert.AreEqual(check, UnixTime64.ConvertToUTC(0, zone, i));
                Assert.AreEqual(check, UnixTime64.ConvertToUTC(0, zone, i));
            }
        }
Exemple #4
0
        public void TestMethodStruct()
        {
            Assert.AreEqual(false, new UnixTime64().Equals(null));
            Assert.AreEqual(new UnixTime64(), new UnixTime64());
            Assert.AreEqual(0, new UnixTime64().TimeStamp);
            Assert.AreEqual((UnixTime64)0, new UnixTime64());
            Assert.AreEqual(true, 0 == new UnixTime64().TimeStamp);
            Assert.AreEqual(new DateTime(1970, 1, 1), (DateTime) new UnixTime64());

            unsafe
            {
                Assert.AreEqual(structSize, Marshal.SizeOf <UnixTime64>());
                Assert.AreEqual(structSize, sizeof(UnixTime64));
            }
            var ptr = Marshal.AllocHGlobal(100);

            Marshal.Copy(new byte[100], 0, ptr, 100);
            try
            {
                UnixTime64 now = UnixTime64.Now;
                Marshal.StructureToPtr(now, ptr, true);
                long value = (long)Marshal.ReadInt64(ptr);
                Assert.AreEqual(true, now.TimeStamp == value);
                var copy = Marshal.PtrToStructure <UnixTime64>(ptr);
                Assert.AreEqual(now.TimeStamp, copy.TimeStamp);
                Assert.AreEqual(now, copy);
                Assert.AreEqual((DateTime)now, (DateTime)copy);
                Assert.AreEqual(now.DateTime, copy.DateTime);
                Assert.AreEqual(now.GetHashCode(), copy.TimeStamp.GetHashCode());
                Assert.AreEqual(now.GetHashCode(), copy.GetHashCode());
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }