docs icon indicating copy to clipboard operation
docs copied to clipboard

[Breaking change]: TimeZoneInfo.FindSystemTimeZoneById() doesn't return new TimeZoneInfo object

Open kuldeepbamrah opened this issue 1 year ago • 1 comments

Description

The TimeZoneInfo object returned by FindSystemTimeZoneById method is a cached version of the TimeZoneInfo instead of a new one.

Version

.NET 8 GA

Previous behavior

TimeZoneInfo.FindSystemTimeZoneById() returned a new TimeZoneInfo object.

New behavior

TimeZoneInfo.FindSystemTimeZoneById() returns a cached TimeZoneInfo object.

Type of breaking change

  • [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • [ ] Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • [X] Behavioral change: Existing binaries may behave differently at run time.

Reason for change

This change aims to resolve namely 2 performance issues with FindSystemTimeZoneById ,

  1. It addresses the inefficiency observed when creating a TimeZoneInfo object with an alternative zone ID (like using IANA Id on Windows and Windows Id on Linux) using the FindSystemTimeZoneById method. By default, the object returned by GetSystemTimeZones is only cached for the zone ID matching the operating system (OS) in use. Consequently, attempts to create a new object with an alternative ID result in a cache miss, necessitating repeated system reads. Particularly on non-Windows OS, this incurs additional overhead due to file system access and probing for the zone file. To mitigate this issue, a cache for alternative IDs is introduced.
  2. The change targets the unnecessary creation of new zone objects when requested with FindSystemTimeZoneById. Despite the existence of a cached object, a new one is created upon each request. However, since zone objects lack writable properties, cloning them is unnecessary. Instead, returning the cached object circumvents unnecessary allocation and validation steps.

Recommended action

If your application uses FindSystemTimeZoneById to create a new instance of TimeZoneInfo, refactor the code to use one of these methods.

Feature area

Core .NET libraries

Affected APIs

No response

kuldeepbamrah avatar Feb 20 '24 19:02 kuldeepbamrah

Related issue in dotnet repo: https://github.com/dotnet/runtime/issues/98571

adegeo avatar Feb 21 '24 20:02 adegeo

@kuldeepbamrah thanks for submitting this. The writing LGTM.

I did a minor edit to fill the feature area an affected APIs sections

tarekgh avatar Feb 26 '24 19:02 tarekgh