Repository navigation
Description
Description
After updating the OS-level time zone database (tzdb) to reflect Paraguay’s permanent switch to UTC-3, .NET TimeZoneInfo incorrectly treats historically invalid times as valid and returns conflicting UTC offsets.
Paraguay switched to DST exactly "before midnight", thus skipping midnight:
- Prior to the tz db update,
tz.IsInvalidTime(new DateTime(2024, 10, 6, 0, 0, 0))was true. - After the tz db update,
tz.IsInvalidTime(new DateTime(2024, 10, 6, 0, 0, 0))is false.
Also for historical dates tz.IsInvalidTime should return correct results to ensure correctness. Since there are also discussions about abolishing DST in the US and EU, we should also ensure that historical calculations there won't be affected by future timezone updates.
Impact:
- Data Integrity: Historical dates are incorrectly validated, leading to errors in scheduling, auditing, or compliance.
Offset Mismatches: DateTime and DateTimeOffset yield conflicting UTC offsets for the same logical time.(Edit: not reproducible)
Reproduction Steps
using System;
public class Program
{
public static void Main()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("America/Asuncion");
var shouldBeInvalid = new DateTime(2024, 10, 6, 0, 0, 0);
// as a bonus show how offsets are handled incorrectly when passing invalid dates to tz.GetUtcOffset
var utcOffsetFromDateTime = tz.GetUtcOffset(shouldBeInvalid);
var dateTimeOffset = new DateTimeOffset(shouldBeInvalid, utcOffsetFromDateTime);
var utcOffsetFromDateTimeOffset = tz.GetUtcOffset(shouldBeInvalid);
Console.WriteLine($"Invalid: {tz.IsInvalidTime(shouldBeInvalid)}");
Console.WriteLine($"UtcOffset: {utcOffsetFromDateTime}");
Console.WriteLine($"UtcOffset: {utcOffsetFromDateTimeOffset}");
}
}Expected behavior
Should print (still does no .NET fiddle as of writing this issue https://dotnetfiddle.net/Ws9LR7):
Invalid: True
UtcOffset: -04:00:00
UtcOffset: -04:00:00
Actual behavior
On azure build agent with ubuntu-latest (agent version '4.253.0' , i.e. Ubuntu 24.04 with tzdata=2025b-0ubuntu0.24.04) it will print:
Invalid: True
UtcOffset: -04:00:00
UtcOffset: -03:00:00
Regression?
Works still on .NET fiddle and did work on Azure build agent version 20250323.1.0 (i.e. Ubuntu 24.04 with tzdata=2024a-2ubuntu1)
Known Workarounds
- use NodaTime
- use the inconsistency between
GetUtcOffset(DateTime)andGetUtcOffset(DateTimeOffset)to determine whether a time is invalid
Configuration
Reproducible on Azure build agents "ubuntu-latest", agent version "4.253.0".
Other information
Changes to tzdb probably affecting this: eggert/tz@486e1e8
Activity
dotnet-policy-service commentedon Apr 10, 2025
Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.
tarekgh commentedon Apr 10, 2025
@michaelamaura I have tried running the code on Ubuntu
25.04using the latesttzdatapackage version 2025 and I am seeing the result.Invalid: FalseLooks wrong and will be interesting to investigate.For
GetUtcOffset()we don't throw on invalid time there, instead we just return the value of the BaseUtcOffset which is-3:00:00.Could you please run the following commands on your machine that show the problem and send the results to know what is going on there?
I guess somehow you have wrong installed
tzdatapackage.dotnet-policy-service commentedon Apr 10, 2025
This issue has been marked
needs-author-actionand may be missing some important information.michaelamaura commentedon Apr 11, 2025
Regarding the offset inconsistency, I was not able to reproduce it any more. Sorry for that.
The issue with
IsInvalidTimepersists however.I ran the commands anyway. The build agent is a Ubuntu 24.04.2:
michaelamaura commentedon Apr 11, 2025
Running with tzdata=2024a-2ubuntu1: works as it should
Just downgraded the tzdata
And ran the program again:
So I can confirm it's working as it should with 2024a-2ubuntu1
tarekgh commentedon Apr 11, 2025
Thanks @michaelamaura for the info.
Yes, looks it is corner case showing a bug in the calculation done in
runtime/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs
Line 1344 in 20b579b
2024a-2ubuntu1to2025b-0ubuntu0looks the following two rules causing the difference. The other rules in the same year are similar. Anyway, this needs to be investigated.michaelamaura commentedon Apr 18, 2025
Cause of issue in
GetIsInvalidTime?To me it seems like an issue here:
runtime/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs
Lines 1781 to 1799 in 20b579b
The comments
/* FUTURE: + rule.StandardDelta; */and/* FUTURE: - rule.StandardDelta; */are a giveaway (and have there been since ages). Because these are exactly the places that would need to be adjusted - additional to a few other ones.At the moment
GetIsInvalidTimejust is ignoring changes ofBaseUtcOffset.To me it also looks like we would need to know the
BaseUtcOffsetfrom the next and/or previousAdjustmentRulein this place to decide whether there is an invalid time.Analyzation of Differences between
2025b-0ubuntu0.24.04and2024a-2ubuntu1I also had a look at how .NET is interpreting the timezone data, as to exclude we have an issue there. So I just wrote a simple program to output the
AdjustmentRules:The output with
2025b-0ubuntu0.24.04will be:With
2024a-2ubuntu1it will be:So, with the old tzdata we will not have any
BaseUtcOffsetDeltachanges. With the new tzdata, all clock changes are handled using theBaseUtcOffsetDelta- which is ignored by the code inTimeZoneInfo.Investigating the time change in Portugal 1995
The following program:
Will print:
The latter one is wrong, since the clocks were set forward for only one hour: https://www.timeanddate.com/time/change/portugal?year=1995
The issue is similar to Paraguay, since Portugal switched from UTC+1 to UTC+0 as default offset in 1996 (after having switch the reverse way in 1992). Thus the daylightDelta alone is not enough to determine whether a time is invalid.
The following program:
will print:
It's a bit funny though, that .NET defines
DaylightDeltaas 02:00 for the DST time in 1995, but theBaseUtcOffsetDeltaas 00:00. You could argue that this is clearly wrong for this time since theDaylightDeltahas always been 01:00 in Portugal, but the UTC offset has changed.I did not investigate the parsing of the tzdate so far - but since the
BaseUtcOffsetDeltawould need to be taken into account anyway this also seems unnecessary for the bug regarding theIsInvalidTIme.michaelamaura commentedon Apr 19, 2025
Similar issue in
TimeZoneInfo.IsAmbiguousTimePortugal has switched its UTC offset twice in the 1990ies:
So, additional to the example above, the following snippet should print
false, but it does printtrue:I created a repository with Unit test cases here: https://github.com/michaelamaura/dotnet-timezoneinfo-fix
tarekgh commentedon Oct 5, 2025
Should be addressed by #119662