1
In ./django/conf/__init__.py:126:
, TZ
environment variable is set based on settings.py
.
os.environ['TZ'] = self.TIME_ZONE
My TIME_ZONE is UTC.
Thatβs why a standalone script result is different from a snippet inside Django: when running standalone, this environment variable TZ
isnβt set.
Now, when creating a datetime object from a myfile_ctime, I just need to add tzinfo from my server (/etc/sysconfig/clock
). My code now looks like this:
import time
import pytz
import os
from datetime import datetime
myfile = SOMEWHERE
myfile_ctime = os.path.getctime(myfile)
ny = pytz.timezone("America/New_York")
d = datetime.fromtimestamp(myfile_ctime, tz=ny)
mytz = pytz.timezone(MY_TZ_WHATEVER)
myd = d.astimezone(mytz)
final_date = myd.strftime('%Y-%m-%d %H:%M:%S')
I hope this is useful to someone. As always, read the source.
1
Make sure of the Time Zone settings in settings.py, for more info about Django Time Zone Settings, check this page: https://docs.djangoproject.com/en/1.6/ref/settings/#time-zone
- [Answered ]-Django authentication : CSRF Failed
- [Answered ]-What happens to a Django website when you restart Gunicorn?
Source:stackexchange.com