3 ms·
I'm just bitten by this today: I was trying to get the time zone offset in Python to print the local time for an app. Doing date +%z in console gives me
by riobard 15y ago
I'm just bitten by this today: I was trying to get the time zone offset in Python to print the local time for an app.
Doing
date +%z
in console gives me correct offset
+0800
of China (we do not have DST anymore) while doing
import time
time.strftime('%z', time.localtime())
gives me
+0706
WTF??!!! I can understand Python might not be able to get the correct timezone info from the system, but giving this kind of obviously incorrect offset is just dumb. Still searching for a solution other than manually configure Python code. If anyone knows why, I would be really glad for your help. Thanks in advance!
- wulczer 15y agoUse pytz. import pytz from datetime import datetime tz = pytz.timezone('Asia/Shanghai') tz.normalize(pytz.utc.localize(datetime.utcnow()).astimezone(tz))
- riobard 15y agoThanks for the response! I'm looking for a solution such that Python will go and load system's time zone and use that---it's a nightmare to deploy an app to multiple severs located in different timezone and I have to change the timezone in Python manually.