public void ToMillisecondsDateTime() { // Must be UTC as we really can't rely on anything else when unit testing DateTime DateTime dt = new DateTime(2022, 2, 3, 11, 0, 0, 123, DateTimeKind.Utc); // Cast to long as we don't really care about any decimals long result = (long)UnixTimeUtils.ToMilliseconds(dt); // Do we have a match? Assert.AreEqual(1643886000123, result); }
public void CurrentMilliseconds() { double actual = UnixTimeUtils.CurrentMilliseconds; // Since Unix time is calculated back to UTC, using "DateTime.Now" and "DateTime.UtcNow" should give the // same result double expected1 = UnixTimeUtils.ToMilliseconds(DateTime.Now); double expected2 = UnixTimeUtils.ToMilliseconds(DateTime.UtcNow); Assert.AreEqual(expected2, actual); Assert.AreEqual(expected2, expected1); }
public void ToMillisecondsDateTimeOffset() { // Initialize to different DateTimeOffset representing the same point in time, but with different offsets DateTimeOffset dto1 = new DateTimeOffset(2022, 2, 3, 11, 0, 0, 123, TimeSpan.Zero); DateTimeOffset dto2 = new DateTimeOffset(2022, 2, 3, 12, 0, 0, 123, TimeSpan.FromHours(1)); // Cast to long as we don't really care about any decimals long result1 = (long)UnixTimeUtils.ToMilliseconds(dto1); long result2 = (long)UnixTimeUtils.ToMilliseconds(dto2); // Do we have a match? Assert.AreEqual(1643886000123, result1); Assert.AreEqual(1643886000123, result2); }