dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.07k stars 4.69k forks source link

Wrong time zones México, System.TimeZoneInfo.FindSystemTimeZoneById #101069

Open universe83 opened 5 months ago

universe83 commented 5 months ago

Description

October 26th, 2022, order from the Mexican Government, changes in time zones. https://techcommunity.microsoft.com/t5/daylight-saving-time-time-zone/mexico-2023-time-zone-updates-now-available/ba-p/3749684

System.TimeZoneInfo.FindSystemTimeZoneById return wrong time zone info for the following IANA ids: "America/Ojinaga", "America/Chihuahua", "America/Ciudad_Juarez"

Reproduction Steps

namespace ConsoleAppTest {
    internal class Program {
        static void Main(string[] args) {
            Console.WriteLine($"-- {GetTimeZone("America/Matamoros")}");
            Console.WriteLine($"-- {GetTimeZone("America/Ojinaga")}");
            Console.WriteLine($"-- {GetTimeZone("America/Monterrey")}");
            Console.WriteLine($"-- {GetTimeZone("America/Mexico_City")}");
            Console.WriteLine($"-- {GetTimeZone("America/Chihuahua")}");
            Console.WriteLine($"-- {GetTimeZone("America/Bahia_Banderas")}");
            Console.WriteLine($"-- {GetTimeZone("America/Merida")}");
            Console.WriteLine($"-- {GetTimeZone("America/Cancun")}");
            try {
                Console.WriteLine($"-- {GetTimeZone("America/Ciudad_Juarez")}"); //New Zone
            } catch (Exception e) { Console.WriteLine($"-- {e}"); }
            Console.WriteLine($"-- {GetTimeZone("America/Mazatlan")}");
            Console.WriteLine($"-- {GetTimeZone("America/Tijuana")}");
            Console.WriteLine($"-- {GetTimeZone("America/Hermosillo")}");
        }
        public static string GetTimeZone(string id) {
            const string dateFormat = "yyyy/MM/dd HH:mm";
            DateTime currentDateUTC = System.DateTime.UtcNow;
            var timeZoneInfo = System.TimeZoneInfo.FindSystemTimeZoneById(id);
            DateTime currentDateTimeZone = System.TimeZoneInfo.ConvertTimeFromUtc(currentDateUTC, timeZoneInfo);
            return $"currentDateUTC:{currentDateUTC.ToString(dateFormat)}, currentDateTimeZone:{currentDateTimeZone.ToString(dateFormat)}, TimeZoneIdentifier:{timeZoneInfo.Id}, WindowsTimeZoneId:{GetWindowsTimeZoneId(id)}, IsDaylightSavingTime:{timeZoneInfo.IsDaylightSavingTime(currentDateTimeZone)}, BaseUtcOffset:{timeZoneInfo.BaseUtcOffset}, TimeZoneUtcOffset:{timeZoneInfo.GetUtcOffset(currentDateUTC)}";
        }
        public static string GetWindowsTimeZoneId(string ianaid) {
            if (System.TimeZoneInfo.TryConvertIanaIdToWindowsId(ianaid, out string? winId)) {
                return winId;
            }
            throw new System.TimeZoneNotFoundException($"No Windows time zone found for IANA Id {ianaid}.");
        }
    }
}

Expected behavior

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

Other information

No response

dotnet-policy-service[bot] commented 5 months ago

Tagging subscribers to this area: @dotnet/area-system-datetime See info in area-owners.md if you want to be subscribed.