ToDateTime() public static method

Converts Unix time (UTC) into a DateTime instance that represents the same time in local time with a maximum precision of a milliseconds.
This method works with time expressed up through 23:59:59, December 31, 3000, UTC.
public static ToDateTime ( double time ) : System.DateTime
time double
return System.DateTime
Example #1
0
        public void IntegralConversions()
        {
            DateTime   t1 = new DateTime(2006, 7, 17, 10, 56, 56);
            const long u1 = 1153133816;

            DateTime   t2 = new DateTime(2006, 7, 17, 11, 00, 44);
            const long u2 = 1153134044;

            Assert.AreEqual(t1, UnixTime.ToDateTime(u1).ToUniversalTime());
            Assert.AreEqual(t2, UnixTime.ToDateTime(u2).ToUniversalTime());

            Assert.AreEqual(u1, UnixTime.ToInt64(t1.ToLocalTime()));
            Assert.AreEqual(u2, UnixTime.ToInt64(t2.ToLocalTime()));
        }
Example #2
0
        public void Milliseconds()
        {
            DateTime     t1 = new DateTime(2006, 7, 17, 10, 56, 56, 456);
            const double u1 = 1153133816.456;

            DateTime     t2 = new DateTime(2006, 7, 17, 11, 00, 44, 567);
            const double u2 = 1153134044.567;

            DateTime ut1 = UnixTime.ToDateTime(u1).ToUniversalTime();

            Assert.AreEqual(t1, ut1, "ms expected = {0}, ms actual = {1}", t1.Millisecond, ut1.Millisecond);

            DateTime ut2 = UnixTime.ToDateTime(u2).ToUniversalTime();

            Assert.AreEqual(t2, ut2, "ms expected = {0}, ms actual = {1}", t2.Millisecond, ut2.Millisecond);

            Assert.AreEqual(u1, UnixTime.ToDouble(t1.ToLocalTime()), 0.0001);
            Assert.AreEqual(u2, UnixTime.ToDouble(t2.ToLocalTime()), 0.0001);
        }
Example #3
0
        public void Milliseconds()
        {
            DateTime     t1 = new DateTime(2006, 7, 17, 10, 56, 56, 456);
            const double u1 = 1153133816.456;

            DateTime     t2 = new DateTime(2006, 7, 17, 11, 00, 44, 567);
            const double u2 = 1153134044.567;

            DateTime ut1 = UnixTime.ToDateTime(u1).ToUniversalTime();

            Assert.AreEqual(t1, ut1, "ms expected = {0}, ms actual = {1}", t1.Millisecond, ut1.Millisecond);

            DateTime ut2 = UnixTime.ToDateTime(u2).ToUniversalTime();

            Assert.AreEqual(t2, ut2, "ms expected = {0}, ms actual = {1}", t2.Millisecond, ut2.Millisecond);

            Assert.AreEqual(u1, UnixTime.ToDouble(t1.ToLocalTime()), 0.0001);
            Assert.AreEqual(u2, UnixTime.ToDouble(t2.ToLocalTime()), 0.0001);

            Assert.AreEqual(new DateTime(1098, 7, 6, 5, 43, 21, 234), UnixTime.ToDateTime(-27501531398.766).ToUniversalTime());
            Assert.AreEqual(UnixTime.EpochUtc.AddMilliseconds(+123), UnixTime.ToDateTime(0, +123).ToUniversalTime());
            Assert.AreEqual(UnixTime.EpochUtc.AddMilliseconds(-123), UnixTime.ToDateTime(0, -123).ToUniversalTime());
        }
Example #4
0
 public void CannotOverflowMilliseconds()
 {
     UnixTime.ToDateTime(0, 1000);
 }
Example #5
0
 public void Negative()
 {
     Assert.AreEqual(new DateTime(1098, 7, 6, 5, 43, 21, 234), UnixTime.ToDateTime(-27501531398, -766).ToUniversalTime());
 }
Example #6
0
 public void CannotSpecifyNegativeTimeWithPositiveMilliseconds()
 {
     UnixTime.ToDateTime(-123, 456);
 }
Example #7
0
 public void Maximum()
 {
     Assert.AreEqual(new DateTime(3000, 12, 31, 23, 59, 59),
                     UnixTime.ToDateTime(32535215999L).ToUniversalTime());
 }
Example #8
0
 public void CannotOverflowNegativeMilliseconds()
 {
     UnixTime.ToDateTime(0, -1000);
 }
Example #9
0
 public void CannotSpecifyNegativeMilliseconds()
 {
     UnixTime.ToDateTime(0, -1);
 }