最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use IANA timezone database in .NET app running on windows - Stack Overflow

programmeradmin5浏览0评论

I've figured out that depending on the operating system a .NET application is running on,the following code may have different outputs:

using System.Globalization; 
    
    
var zone = TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time");
var date = new DateTimeOffset( // Farvardin 1st 1367
    year: 1988, 
    month:3, 
    day: 21,0, 0, 0, TimeSpan.FromHours(4.5));
var offset = zone.GetUtcOffset(date);

Console.WriteLine(offset);
// On windows prints: 4:30
// On ubuntu prints: 3:30

I know that the difference is because Linux uses IANA timezone database whereas Windows has another database and .NET uses the database provided by OS. I want my app to use IANA time zone database instead of the one provided by default by windows. How can I achieve this?

I'm not looking to convert timezone Identifiers used by IANA and .NET, as described here: How to translate between Windows and IANA time zones?

I know that NodaTime provides IANA TZDB, however adding a big library just for this purpose, seems like not the best solution.

发布评论

评论列表(0)

  1. 暂无评论