Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 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

question from:https://stackoverflow.com/questions/65834877/time-zone-issue-in-azure-function-app-with-consumption-plan-windows

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

1 Reply

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("!!!!!!!!!!!!!!!");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...