问题
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