如果存储在数据库中的时间是 UTC,那么你应该像这样处理你的时间:
namespace TimeProcessing
{
using System;
using System.Diagnostics;
using System.Globalization;
class Program
{
static void Main(string[] args)
{
double timeStamp = 1000000000; // here you read real UNIX timespamp
// get UTC time
DateTime utc = ConvertFromUnixTimestamp(timeStamp);
Console.WriteLine("UTC time is: {0}", utc.ToString("o", CultureInfo.CurrentCulture));
// get local time
DateTime local = TimeZone.CurrentTimeZone.ToLocalTime(utc);
Console.WriteLine("Local time is: {0}", local.ToString("o", CultureInfo.CurrentCulture));
if(Debugger.IsAttached)
{
Console.WriteLine("Press any key to exit..");
Console.ReadKey();
}
}
// here you implement your timestamp processing algorithm
public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return origin.AddSeconds(timestamp);
}
}
}
所有 DTS 更改将自动应用。如果您需要将时间转换为其他时区(非本地),算法将相同,但您需要使用TimeZoneInfo类来创建您想要转换 UTC 时间的时区。