优化DateTimeUtil时间戳转DateTime接口时区的问题

This commit is contained in:
monitor1394
2024-04-16 22:30:04 +08:00
parent 58e6108bb2
commit e2120b3da6

View File

@@ -6,8 +6,7 @@ namespace XCharts.Runtime
{
public static class DateTimeUtil
{
//private static readonly DateTime k_DateTime1970 = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
private static readonly DateTime k_DateTime1970 = new DateTime(1970, 1, 1);
private static readonly DateTime k_DateTime1970 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
public static readonly int ONE_SECOND = 1;
public static readonly int ONE_MINUTE = ONE_SECOND * 60;
public static readonly int ONE_HOUR = ONE_MINUTE * 60;
@@ -34,10 +33,21 @@ namespace XCharts.Runtime
return (int)(time - k_DateTime1970).TotalSeconds;
}
public static int GetTimestamp(string dateTime)
{
try
{
return GetTimestamp(DateTime.Parse(dateTime));
}
catch (Exception e)
{
throw e;
}
}
public static DateTime GetDateTime(int timestamp)
{
long span = ((long)timestamp) * 10000000;
return k_DateTime1970.Add(new TimeSpan(span));
return k_DateTime1970.AddSeconds(timestamp);
}
internal static string GetDateTimeFormatString(DateTime dateTime, double range)