带消费计划的 Azure Function 应用中的时区问题 (Windows)

时间:2021-01-21 19:57:02

标签: azure azure-functions azure-function-app azure-app-service-plans

我在消费计划 (Windows) 中使用函数应用程序来执行 C# 函数,但是当执行代码行 DateTime.Now 时,它返回 UTC。我已经根据 the default time zones 将应用设置中的 WEBSITE_TIME_ZONE 更改为 SA Pacific Standard Time,但它不起作用。

我也在开发工具控制台上用 time 命令做了一个测试,但是控制台的结果确实与预期的时间相对应,这与结果为 UTC 的 DateTime.Now 语句相反。

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我的代码:

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

WEBSITE_TIME_ZONE 设置为 SA Pacific Standard Time 并部署后,我的代码在 azure 上运行良好:

enter image description here

控制台也没问题:

enter image description here

所以你的代码应该没问题,你可以尝试以下事情:

1、重启函数应用,让函数应用重新加载环境变量,然后重试。

2、尝试其他 GMT-5 TIMEZONE,从这个 doc,它们是:

SA Pacific Standard Time

Eastern Standard Time

US Eastern Standard Time

3、在你的代码中手动转换TIMEZONE:

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("!!!!!!!!!!!!!!!");
        }
    }
}
相关问题