58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace DTS.Common.Utilities
|
|
{
|
|
/// <summary>
|
|
/// Convert a DateTime to Unix Time. No attempt is made to correct for time zone/localities.
|
|
/// </summary>
|
|
public class Time
|
|
{
|
|
public static UInt32 DateTimeToUnixTimestamp(DateTime dateTime)
|
|
{
|
|
var start = new DateTime(1970, 1, 1);
|
|
var span = dateTime - start;
|
|
|
|
return (UInt32)span.TotalSeconds;
|
|
}
|
|
|
|
public static UInt32 CurrentUtcUnixTimestamp()
|
|
{
|
|
var span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
|
|
|
|
return (UInt32)span.TotalSeconds;
|
|
}
|
|
|
|
public static UInt32 CurrentLocalUnixTimestamp()
|
|
{
|
|
var span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local));
|
|
|
|
return (UInt32)span.TotalSeconds;
|
|
}
|
|
|
|
public static DateTime ToDateTime(UInt32 seconds)
|
|
{
|
|
return new DateTime(1970, 1, 1).AddSeconds(seconds);
|
|
}
|
|
|
|
public static UInt32 ToUnixTime(DateTime dateTime)
|
|
{
|
|
var start = new DateTime(1970, 1, 1);
|
|
var span = dateTime - start;
|
|
|
|
return (UInt32)span.TotalSeconds;
|
|
}
|
|
|
|
public static bool IsBeginningOfTime(DateTime dateTime)
|
|
{
|
|
var start = new DateTime(1970, 1, 1);
|
|
var span = dateTime - start;
|
|
|
|
return (0 == span.TotalSeconds);
|
|
}
|
|
|
|
}
|
|
}
|