using System; using System.Linq; namespace uEvents.Meetup { public static class DateTimeExtensions { private const string InvalidUnixEpochErrorMessage = "Unix epoc starts January 1st, 1970"; /// /// Convert a long into a DateTime /// public static DateTime FromUnixTime(this long self) { var ret = new DateTime(1970, 1, 1); return ret.AddMilliseconds(self); } /// /// Convert a DateTime into a long /// public static long ToUnixTime(this DateTime self) { if (self == DateTime.MinValue) { return 0; } var epoc = new DateTime(1970, 1, 1); var delta = self - epoc; if (delta.TotalMilliseconds < 0) throw new ArgumentOutOfRangeException(InvalidUnixEpochErrorMessage); return (long)delta.TotalMilliseconds; } } }