Created
November 14, 2017 06:26
-
-
Save HituziANDO/92256271b9b705ce6628a4cc39ca5981 to your computer and use it in GitHub Desktop.
DateTime <-> UnixTime(long) 変換クラス
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; | |
namespace MyApplication { | |
public class TimeUtils { | |
private static DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); | |
private TimeUtils() { | |
} | |
/// <summary> | |
/// Convert DateTime to the unix time[s]. | |
/// </summary> | |
/// <returns>The unix time.</returns> | |
/// <param name="t">DateTime.</param> | |
public static long ToUnixTime(DateTime t) { | |
return (long) (t - unixEpoch).TotalSeconds; | |
} | |
/// <summary> | |
/// Convert the unix time[s] to DateTime. | |
/// </summary> | |
/// <returns>DateTime.</returns> | |
/// <param name="unixTime">The unix time.</param> | |
public static DateTime ToDateTime(long unixTime) { | |
return unixEpoch.AddSeconds(unixTime); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment