Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

Time Zone issue in Azure Function app with consumption plan (Windows)

I'm using a Function App in consumption plan (Windows) to execute a C# function, but when the line of code DateTime.Now is executed the time it returns UTC. I already have changed the WEBSITE_TIME_ZONE in the app settings to SA Pacific Standard Time based on the default time zones but it doesn't work.

I also did a test on the development tools console with the time command, but the result of the console does correspond to the expected time, contrary to the DateTime.Now statement whose result is in UTC.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is my code:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp66
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation(DateTime.UTCNow.ToString());
            return new OkObjectResult("!!!!!!!!!!!!!!!");
        }
    }
}

After set the WEBSITE_TIME_ZONE to SA Pacific Standard Time and deploy, my code works fine on azure:

enter image description here

console also no problem:

enter image description here

So your code should be no problem, you can try below things:

1, restart the function app to let the function app re-load the environment variable, and then try again.

2, try other GMT-5 TIMEZONE, from this doc, they are:

SA Pacific Standard Time

Eastern Standard Time

US Eastern Standard Time

3, convert the TIMEZONE manually in your code:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp66
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var date = System.TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time"));
            log.LogInformation(date.ToString());
            return new OkObjectResult("!!!!!!!!!!!!!!!");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...