新纪元时间(Epoch Time)与公元时间
2009-08-24 14:14
330 查看
很多时候,用Epoch Time来存储比其他时间格式会带来很多优势。但在客户应用端来讲,Epoch Time确是晦涩让人不之所以。 再次给出Epoch Time 到 公元时间的转换。
Code
namespace Framework
{
/// <summary>
/// To convert from epoch UTC (which should be 1/1/1970 00:00:00 UTC) to a human readable time,
/// you'll need to find the number of ticks between the DateTime class' base time (1/1/0001 00:00:00) to epoch time.
/// You multiply your epoch time by the tick resolution (100 nanoseconds / tick) and
/// add your base ticks (epoch time - base time). Then you pass the ticks into the DateTime
/// constructor and get a nice human-readable result.
///
/// long baseTicks = 621355968000000000;
/// long tickResolution = 10000000;
/// long epoch = 1225815911;
/// long epochTicks = (epoch * tickResolution) + baseTicks;
/// var date = new DateTime(epochTicks, DateTimeKind.Utc);
public class TimeConvert
{
/// </summary>
/// <param name="epochTime"></param>
/// <returns></returns>
public static string ConverEpochTimeToDateTime(long epochTime)
{
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long baseTicks = baseTime.Ticks;
long epochTicks = epochTime * 10000000 + baseTicks;
DateTime time = new DateTime(epochTicks);
return time.ToString("MMM dd yyyy hh:mm");
}
public static string ConvertDurationToTimeStr(long lDuration)
{
string format = "{0:D2}:{1:D2}:{2:D2}";
int hour, minute, second;
try
{
int duration = int.Parse(lDuration.ToString());
hour = duration / 3600;
minute = (duration - hour * 3600) / 60;
second = duration - hour * 3600 - minute * 60;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return string.Empty;
}
return string.Format(format, hour, minute, second);
}
}
}
Code
namespace Framework
{
/// <summary>
/// To convert from epoch UTC (which should be 1/1/1970 00:00:00 UTC) to a human readable time,
/// you'll need to find the number of ticks between the DateTime class' base time (1/1/0001 00:00:00) to epoch time.
/// You multiply your epoch time by the tick resolution (100 nanoseconds / tick) and
/// add your base ticks (epoch time - base time). Then you pass the ticks into the DateTime
/// constructor and get a nice human-readable result.
///
/// long baseTicks = 621355968000000000;
/// long tickResolution = 10000000;
/// long epoch = 1225815911;
/// long epochTicks = (epoch * tickResolution) + baseTicks;
/// var date = new DateTime(epochTicks, DateTimeKind.Utc);
public class TimeConvert
{
/// </summary>
/// <param name="epochTime"></param>
/// <returns></returns>
public static string ConverEpochTimeToDateTime(long epochTime)
{
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long baseTicks = baseTime.Ticks;
long epochTicks = epochTime * 10000000 + baseTicks;
DateTime time = new DateTime(epochTicks);
return time.ToString("MMM dd yyyy hh:mm");
}
public static string ConvertDurationToTimeStr(long lDuration)
{
string format = "{0:D2}:{1:D2}:{2:D2}";
int hour, minute, second;
try
{
int duration = int.Parse(lDuration.ToString());
hour = duration / 3600;
minute = (duration - hour * 3600) / 60;
second = duration - hour * 3600 - minute * 60;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return string.Empty;
}
return string.Format(format, hour, minute, second);
}
}
}
相关文章推荐
- Linux时间 新纪元 epoch time
- perl将字符串时间转换成 epoch time
- [已解决]请教:时间格式“2008-10-9-公元 10:43:27,AM”,这种时间格式是该怎么恢复默认设置
- PostgreSQL: epoch 新纪元时间的使用
- python中Unix及Linux的新纪元时间
- perl将字符串时间转换成epoch time
- 关于DateTime在远程服务器使用以及日期与时间格式的问题
- C#获取Gif图片每一帧的时间
- SQL_Server_2005_日期和时间函数(描述及实例)
- 关于linux系统和tomcat时间不一致的问题
- 日期类的时间从为什么是从1970年1月1日
- C++:获取当前系统时间并返回
- MySql Java 时间类型
- 【管理心得之五】我被时间欺骗过
- django中模板models设置时间
- 内排序与外排序+内排时间复杂度等性能总结
- MONGO 删除制定时间前 的shell 脚本 模版
- 当前时间代码
- SQL中取得当前时间
- linux下时间戳转换