【发布时间】:2021-11-07 07:08:01
【问题描述】:
我有一个C# 程序需要从 EST 转换为 UTC。以下功能在Linux 中完美运行,除了它增加了一个小时的额外意味着而不是增加了 4 小时,而是增加了 5 小时。这是因为 DayLightSaving。
我试过了
TimeZoneInfo.Local.IsDaylightSavingTime()
但它在 Linux Ubuntu 中返回错误值 -- 它应该是 true 但返回为 false。
如何在Linux 中解决这个问题?
private DateTime ConvertToUTCHHmm(DateTime dateValue)
{
// default to date for debuging, should be overwritten
DateTime dateTimeUtc = DateTime.Now.AddYears(-120);
try
{
// Convert EST to UTC - Old Method of conversion.
TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
dateTimeUtc = TimeZoneInfo.ConvertTimeToUtc(dateValue, est);
// Check Daylight Saving Time..
DateTime thisTime = DateTime.Now;
bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);
_logger.LogDebug(String.Format(" IsDaylightSavingTime : {0}", isDaylight));
if (isDaylight) // returns as false. It supposed to be true
dateTimeUtc = dateTimeUtc.AddHours(-1);
_logger.LogDebug(String.Format("TimeZoneInfo Old Process : {0} Converted Time : {1}", est, dateTimeUtc));
}
catch (Exception ex)
{
_logger.LogError("TimeZoneInfo Old Process Error - " + ex.Message);
}
return dateTimeUtc;
}
【问题讨论】:
标签: c# asp.net linux timezone date-conversion