Created
July 25, 2010 12:50
-
-
Save karno/489542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Std.Network | |
{ | |
/// <summary> | |
/// Unix epoch service | |
/// </summary> | |
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