Skip to content

Instantly share code, notes, and snippets.

@karno
Created March 16, 2010 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karno/333902 to your computer and use it in GitHub Desktop.
Save karno/333902 to your computer and use it in GitHub Desktop.
using System;
namespace K.Particles
{
static public class UnixEpoch
{
const long EPOCH = 621355968000000000;
/// <summary>
/// Get Unix epoch from DateTime
/// </summary>
/// <param name="dt">DateTime</param>
/// <returns>Unix epoch</returns>
public static int GetUnixEpochByDateTime(DateTime dt)
{
return (int)((dt.ToUniversalTime().Ticks - EPOCH) / 10000000);
}
/// <summary>
/// Get DateTime from Unix epoch
/// </summary>
/// <param name="epoch">Unix epoch</param>
/// <returns>DateTime</returns>
public static DateTime GetDateTimeByUnixEpoch(long epoch)
{
DateTime unix = new DateTime(1970, 1, 1);
unix = unix.AddSeconds(epoch);
return unix.ToLocalTime();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment