新纪元时间(Epoch Time)与公元时间

很多时候,用Epoch Time来存储比其他时间格式会带来很多优势。但在客户应用端来讲,Epoch Time确是晦涩让人不之所以。 再次给出Epoch Time 到 公元时间的转换。
ContractedBlock.gif ExpandedBlockStart.gif 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(197011000, 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);
        }

    }
}

转载于:https://www.cnblogs.com/joechen/archive/2009/08/24/1552860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值