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.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) {
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.
source
share