1

I am trying to set a DateTimeField for one of my django models. My code looks like this:

dt = datetime.fromtimestamp(mktime(parsed_feed.updated_parsed))
feed.updated = dt

updated_parsed is struct in the format :

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=26, tm_hour=8, tm_min=20, tm_sec=43, tm_wday=1, tm_yday=117, tm_isdst=0)

And updated is obviously the django DateTimeField. I am trying to convert the struct into a datetime object, and then set it to my field. Everything works nicely, and the correct date and time is set, however, I get this error (warning) in my console:

RuntimeWarning: DateTimeField Feed.updated received a naive datetime (2016-04-26 08:25:08) while time zone support is active.
  RuntimeWarning)

How do I integrate timezone support into the datetime object (dt)?

darkhorse
  • 8,192
  • 21
  • 72
  • 148

2 Answers2

1

As explained in this answer:

The following line creates a naive (non-timezone aware) datetime:

creationDate = datetime.datetime.now()

Try changing that line to:

creationDate = timezone.now()

Don't forget to import timezone at the beginning of your code:

from django.utils import timezone
Community
  • 1
  • 1
pyjavo
  • 1,598
  • 2
  • 23
  • 41
-1

Use the pytz package. So import pytz into your views.

t = pytz.timezone('Europe/Warsaw').localize(
    datetime(2013, 5, 11, hour=11, minute=0))

Your timezone should be set in your settings.py
datetime and timezone conversion with pytz - mind blowing behaviour

Community
  • 1
  • 1
Georgina S
  • 467
  • 2
  • 9