TimeZoneInfo.ConvertTime is incredibly slow in DateTime.MinValue

I just noticed that one of our unit tests took ten seconds. After playing with him, I created a minimal linqpad example to reproduce it:

void Main()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    var timeZone = TimeZoneInfo.Local;
    for(int i=0; i<10000; i++)
    {
      //var dateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timeZone);   
      var dateTime = TimeZoneInfo.ConvertTime(DateTime.MinValue, timeZone);   
    }
    sw.Stop();
    (sw.ElapsedMilliseconds + " ms").Dump();
}

It takes 40 seconds on my PC or our PC. If I use DateTime.UtcNow, it will take 15 ms.

Any reason or workarounds for this?


Edit: as suggested in the comments, I decompiled TimeZoneInfo and there is a special case in DateTime.MinValue:

static public DateTime ConvertTime(DateTime dateTime, TimeZoneInfo destinationTimeZone) {
    // Special case to give a way clearing the cache without exposing ClearCachedData()
    if (dateTime.Ticks == 0) {
        ClearCachedData();
    }

Looks like a (test?) Code that clears the cache every time it is called using DateTime.MinValue.

The question remains why this is happening.

+4
source share
1 answer

. .NET Framework CoreCLR GitHub.

.NET 4.6 , WinRT, ClearCachedData, . .

. - . , ConvertTimeFromUtc, ConvertTime, DateTimeOffset. .

, , . , .

, DateTime.MinValue , , , 1- .

+3

Source: https://habr.com/ru/post/1691741/


All Articles