/// <summary> /// Returns the date in int format based on an Epoch (defaults to unix epoch of 1/1/1970) /// </summary> /// <param name="Date">Date to convert</param> /// <param name="Epoch">Epoch to use (defaults to unix epoch of 1/1/1970)</param> /// <returns>The date in Unix format</returns> public static int To(this DateTime Date, DateTime Epoch = default(DateTime)) { Epoch = Epoch.Check(x => x != default(DateTime), () => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); return (int)((Date.ToUniversalTime() - Epoch).Ticks / TimeSpan.TicksPerSecond); }
/// <summary> /// Returns the date in DateTime format based on an Epoch (defaults to unix epoch of 1/1/1970) /// </summary> /// <param name="Date">Date to convert</param> /// <param name="Epoch">Epoch to use (defaults to unix epoch of 1/1/1970)</param> /// <returns>The Unix Date in DateTime format</returns> public static DateTime To(this long Date, DateTime Epoch = default(DateTime)) { Epoch = Epoch.Check(x => x != default(DateTime), () => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); return new DateTime((Date * TimeSpan.TicksPerSecond) + Epoch.Ticks, DateTimeKind.Utc); }
public void NullCheck() { object TestObject = new DateTime(1999, 1, 1); Assert.Equal(TestObject, TestObject.Check()); Assert.Same(TestObject, TestObject.Check()); TestObject = null; Assert.Equal(new DateTime(1999, 1, 2), TestObject.Check(new DateTime(1999, 1, 2))); Assert.Equal(new DateTime(1999, 1, 2), TestObject.Check(x => x != null, new DateTime(1999, 1, 2))); }