我有一個dotnet核心asp.net mvc應用程序,需要做UTC,中央時區和新加坡標準時間之間的時區轉換。我在Windows開發環境中使用TimeZoneInfo.FindSystemTimeZoneById
方法設置時區實例,並將該時區實例傳遞到TimeZoneInfo.ConvertTime
以獲得所需時區的時間。然而,當我部署Web應用CentOS7我得到的錯誤:dotnet core crossplatform(Windows&CentOS)時區轉換錯誤
"Could not find file '/usr/share/zoneinfo/Central Standard Time'."
什麼我需要做的就是這兩個平臺上運行?
public TimeZoneInfo TimeZoneInstance
{
get
{
if (_timeZoneInstance == null)
{
try
{
_timeZoneInstance = TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
}
catch
{
TimeZone = "Central Standard Time";
_timeZoneInstance = TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
}
}
return _timeZoneInstance;
}
private set { _timeZoneInstance = value; }
}
public DateTime? GetUserTime(DateTime? utcTime = null)
{
if (utcTime == null)
return utcTime;
return TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(utcTime.Value, DateTimeKind.Utc), TimeZoneInfo.Utc, TimeZoneInstance);
}
public DateTime? GetUtcTime(DateTime? localTime)
{
if (localTime == null)
return localTime;
return TimeZoneInfo.ConvertTime(localTime.Value, TimeZoneInstance, TimeZoneInfo.Utc);
}