Fork me on GitHub

11/18/2009

Unix Time 轉 .Net DateTime

透過 Facebook API 拿到 Stream 當中的時間單位是 Unix Time 格式
因此對應到 .Net 上必須轉換,這樣搜尋時才會 locate 到正確的時間點
參考資料: http://www.snippetit.com/2009/04/c-unix-time-to-net-datetime-and-vice-versa/

using System;

namespace snippetit.sample
{
struct UnixTime
{
private static DateTime BEGIN_UTC = new DateTime(1970, 1, 1);
private long utValue;

public UnixTime(long seconds)
{
utValue = seconds;
}

public UnixTime(DateTime dateTime)
{
this.DateTime = dateTime;
}

public long Value
{
get { return utValue; }
set { utValue = value; }
}

public DateTime DateTime
{
get { return BEGIN_UTC.AddSeconds((long)utValue); }
set { utValue = (long)((TimeSpan)(value - BEGIN_UTC)).TotalSeconds; }
}

public override string ToString()
{
return DateTime.ToString("yyy-MM-dd HH:mm:ss"); ;
}
}
}

將 UnixTime 轉成 long: http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Convert-Unix-timestamp.html

No comments:

Post a Comment