很多时候,用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);
}
}
}