Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C#でのUnixTime
public static class UnixTime
{
private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/*===========================================================================*/
/**
* 現在時刻からUnixTimeを計算する.
*
* @return UnixTime.
*/
public static long Now()
{
return ( FromDateTime( DateTime.UtcNow ) );
}
/*===========================================================================*/
/**
* UnixTimeからDateTimeに変換.
*
* @param [in] unixTime 変換したいUnixTime.
* @return 引数時間のDateTime.
*/
public static DateTime FromUnixTime( long unixTime )
{
return UNIX_EPOCH.AddSeconds( unixTime ).ToLocalTime();
}
/*===========================================================================*/
/**
* 指定時間をUnixTimeに変換する.
*
* @param [in] dateTime DateTimeオブジェクト.
* @return UnixTime.
*/
public static long FromDateTime( DateTime dateTime )
{
double nowTicks = ( dateTime.ToUniversalTime() - UNIX_EPOCH ).TotalSeconds;
return (long)nowTicks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment