How to get current time in Pacific Timezone when import pytz fails?

老子叫甜甜 提交于 2019-12-20 04:55:19

问题


I'm working in an environment (AWS Lambda) where import pytz doesn't work.

The environment is set to UTC.

How can I get the current time in the U.S. Pacific Timezone in this environment?

I need something simple, and low-maintenance. Somehow forcing import pytz to work would be ideal, and I hope to avoid having to copy the entire pytz library into my own script.

Details

What have I tried so far? I tried using import pytz, and it failed with module not found.

Example code? I tried this, straight from another question on SO:

import pytz

eastern = pytz.timezone('US/Eastern')
utc = pytz.utc
test = '2013-03-27 23:05'

回答1:


If you don't have access to pytz in your environment, maybe you have access to python-dateutil. In that case you can do:

import datetime
import dateutil.tz

eastern = dateutil.tz.gettz('US/Eastern')
datetime.datetime.now(tz=eastern)


来源:https://stackoverflow.com/questions/51230077/how-to-get-current-time-in-pacific-timezone-when-import-pytz-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!