[Answered ]-Does django mess up with python's datetime?

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 TZisn’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

πŸ‘€bingorabbit

Leave a comment