2018-11-03 21:21:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Roadie.Library.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class DateTimeExt
|
|
|
|
|
{
|
|
|
|
|
public static DateTime? FormatDateTime(this DateTime? value)
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
if (!value.HasValue) return null;
|
|
|
|
|
if (value.Value.Year == 0 || value.Value.Year == 1) return null;
|
2018-11-03 21:21:36 +00:00
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime FromUnixTime(this long unixTime)
|
|
|
|
|
{
|
|
|
|
|
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
|
return epoch.AddSeconds(unixTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static long ToUnixTime(this DateTime date)
|
|
|
|
|
{
|
|
|
|
|
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
|
return Convert.ToInt64((date - epoch).TotalSeconds);
|
|
|
|
|
}
|
2019-06-08 22:32:15 +00:00
|
|
|
|
|
|
|
|
|
public static int ToUnixTimeSinceEpoch(this DateTime dateTime)
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
var utcDateTime = dateTime.ToUniversalTime();
|
2019-06-08 22:32:15 +00:00
|
|
|
|
var jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0);
|
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
var timeSinceJan1St1970 = utcDateTime.Subtract(jan1St1970);
|
2019-06-08 22:32:15 +00:00
|
|
|
|
return (int)timeSinceJan1St1970.TotalSeconds;
|
|
|
|
|
}
|
2018-11-03 21:21:36 +00:00
|
|
|
|
}
|
2018-11-04 20:33:37 +00:00
|
|
|
|
}
|