【问题标题】:Python timezone using only modules available in AWS Lambda?Python 时区仅使用 AWS Lambda 中可用的模块?
【发布时间】:2019-08-08 06:27:49
【问题描述】:

我正在尝试运行这段改编自 this answer 的代码。

from datetime import datetime
from pytz import timezone
import pytz

def lambda_handler(event, context):

    date_format='%m/%d/%Y %H:%M:%S %Z'
    date = datetime.now(tz=pytz.utc)
    print('Current date & time is:', date.strftime(date_format))

    date = date.astimezone(timezone('US/Pacific'))

    print('Local date & time is  :', date.strftime(date_format))

我在 AWS Lambda 上运行。我收到此错误:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'pytz'",
  "errorType": "Runtime.ImportModuleError"
}

我该如何解决这个问题,只使用 AWS Lambda 中可用的导入?我想保持代码非常简约,以便可以将其直接复制粘贴到 AWS 控制台中,而无需添加有关如何设置软件包等的说明。

【问题讨论】:

标签: amazon-web-services aws-lambda python-3.7


【解决方案1】:

如果您在 AWS Lambda 中无权访问 pytz,则可以使用 python-dateutil
在这种情况下,您可以:

import datetime
import dateutil

date_format='%m/%d/%Y %H:%M:%S %Z'

IST = dateutil.tz.gettz('Asia/Kolkata')
print('Current date & time is : ',datetime.datetime.now(tz=IST).strftime(date_format))

or

eastern = dateutil.tz.gettz('US/Eastern')
print('Current date & time is : ',datetime.datetime.now(tz=eastern).strftime(date_format))

这是预览:

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-12-02
    • 2019-03-13
    • 2018-07-03
    • 2019-02-23
    • 2020-06-16
    相关资源
    最近更新 更多