roadie/Roadie.Api.Library/Extensions/DateTimeExt.cs

35 lines
1.1 KiB
C#
Raw Normal View History

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);
}
public static int ToUnixTimeSinceEpoch(this DateTime dateTime)
{
2019-07-03 16:21:29 +00:00
var utcDateTime = dateTime.ToUniversalTime();
var jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0);
2019-07-03 16:21:29 +00:00
var timeSinceJan1St1970 = utcDateTime.Subtract(jan1St1970);
return (int)timeSinceJan1St1970.TotalSeconds;
}
2018-11-03 21:21:36 +00:00
}
2018-11-04 20:33:37 +00:00
}