You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda部署应用遇时区ID未找到错误,如何添加时区?

解决AWS Lambda中时区ID找不到的问题

问题原因

AWS Lambda的运行环境(如Amazon Linux 2/2023)采用IANA时区数据库,而你代码中使用的Pacific Standard Time是Windows风格的时区ID,两种格式不兼容,导致系统无法找到对应时区。

解决方案

方案1:直接改用IANA时区ID

将Windows时区ID替换为对应的IANA格式ID,比如Pacific Standard Time对应America/Los_Angeles。可以维护一个映射表处理用户输入的时区代码:

// 示例:Windows到IANA时区ID映射
var windowsToIanaMap = new Dictionary<string, string>
{
    {"Pacific Standard Time", "America/Los_Angeles"},
    // 按需添加其他时区映射
};

string ianaTimeZoneId = windowsToIanaMap[userModel.TimeZoneListCode];
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(ianaTimeZoneId);

方案2:使用时区转换包兼容Windows ID

如果必须保留Windows风格的时区ID(比如依赖现有业务逻辑),可以安装TimeZoneConverter NuGet包实现自动转换:

  1. 安装包:
    Install-Package TimeZoneConverter
    
    或在项目文件中添加:
    <PackageReference Include="TimeZoneConverter" Version="5.0.0" />
    
  2. 修改代码:
    using TimeZoneConverter;
    
    // 自动适配运行环境,解析Windows时区ID
    TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo(userModel.TimeZoneListCode);
    

方案3:验证Lambda支持的时区

可以在Lambda函数中打印所有可用时区ID,确认环境支持的时区:

foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
{
    Console.WriteLine(tz.Id);
}

运行后查看CloudWatch日志即可获取完整的时区列表。

内容的提问来源于stack exchange,提问作者Hanan Afzal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:36:24