Skip to content

Instantly share code, notes, and snippets.

@YuukiTsuchida
Created May 23, 2014 06:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuukiTsuchida/06ca3a1f0baf755651b0 to your computer and use it in GitHub Desktop.
Save YuukiTsuchida/06ca3a1f0baf755651b0 to your computer and use it in GitHub Desktop.
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