Skip to content

Instantly share code, notes, and snippets.

@HituziANDO
Created November 14, 2017 06:26
Show Gist options
  • Save HituziANDO/92256271b9b705ce6628a4cc39ca5981 to your computer and use it in GitHub Desktop.
Save HituziANDO/92256271b9b705ce6628a4cc39ca5981 to your computer and use it in GitHub Desktop.
DateTime <-> UnixTime(long) 変換クラス
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