Search code examples
c#azure-functionstimezone.net-6.0iana

TimeZoneInfo.TryConvertIanaIdToWindowsId returns only False in .NET 6 Azure function


My app stores user time zone selection as IANA Time Zone Id values. It calculates local user times by converting these to Windows Time Zone Id values and using TimeZoneInfo methods to calculate times. The conversion uses TimeZoneInfo.TryConvertIanaIdToWindowsId(). The Azure Function app function targets .NET 6. TryConvertIanaIdToWindowsId returns True and correctly populates outvalues values locally, but NOT when running on Azure.

This is the c# Azure function code:

    public static class HttpTriggerGetTimeZoneFromIana
    {
        [FunctionName("HttpTriggerGetTimeZoneFromIana")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string responseMessage = string.Empty;
            string? windowsTimeZoneId = string.Empty;
            string IanaTimeZoneId = req.Query["IanaTimeZoneId"]; // EXAMPLE: "America/Chicago";

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            IanaTimeZoneId = IanaTimeZoneId ?? data?.IanaTimeZoneId;

            if (TimeZoneInfo.TryConvertIanaIdToWindowsId(IanaTimeZoneId, out windowsTimeZoneId))
                responseMessage = windowsTimeZoneId;
            else
                responseMessage = $"No Windows time zone found for IanaTimeZoneId [{IanaTimeZoneId}];";

            return new OkObjectResult(responseMessage);
        }
    }

executed locally http://localhost:7296/api/HttpTriggerGetTimeZoneFromIana?IanaTimeZoneId=America/Chicago result: Central Standard Time

execute on azure http://xxxxxx.azurewebsites.net/api/HttpTriggerGetTimeZoneFromIana?IanaTimeZoneId=America/Chicago result: No Windows time zone found for IanaTimeZoneId [America/Chicago];


Solution

  • The easiest work-around for me was to store the Windows time zone Id in my user entity. I display the IANA Id in the UI, but use the Windows Id when calculating the local times.