Created
May 23, 2014 06:20
-
-
Save YuukiTsuchida/06ca3a1f0baf755651b0 to your computer and use it in GitHub Desktop.
C#でのUnixTime
This file contains hidden or 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
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