Django 1.4 - cannot compare offset dates and least time

I am migrating an application from django 1.2 to 1.4.

I have a daily task object that contains the time of day when the task should be completed:

class DailyTask(models.Model): time = models.TimeField() last_completed = models.DateTimeField() name = models.CharField(max_length=100) description = models.CharField(max_length=1000) weekends = models.BooleanField() def __unicode__(self): return '%s' % (self.name) class Meta: db_table = u'dailytask' ordering = ['name'] 

To check if there is still a task to be completed today, I have the following code:

 def getDueDailyTasks(): dueDailyTasks=[] now = datetime.datetime.now() try: dailyTasks = DailyTask.objects.all() except dailyTask.DoesNotExist: return None for dailyTask in dailyTasks: timeDue = datetime.datetime(now.year,now.month,now.day,dailyTask.time.hour,dailyTask.time.minute,dailyTask.time.second) if timeDue<now and timeDue>dailyTask.last_completed: if dailyTask.weekends==False and now.weekday()>4: pass else: dueDailyTasks.append({'id':dailyTask.id, 'due':timeDue, 'name': dailyTask.name, 'description':dailyTask.description}) return dueDailyTasks 

This worked fine under 1.2, but under 1.4 I get an error:

 can't compare offset-naive and offset-aware datetimes 

because of the line

 if timeDue<now and timeDue>dailyTask.last_completed 

and both comparison sentences throw this error.

I tried making timeDue a time zone by adding pytz.UTC as an argument, but this still causes the same error.

I have read some timezone docs, but I don’t understand if I just need to specify the timeDue timezone, or do I need to make fundamental changes to my db and existing data.

+77
python timezone django
May 18 '12 at 12:39
source share
1 answer

Check the detailed document for details.

Usually use django.utils.timezone.now to create a current datetime that supports offset

 >>> from django.utils import timezone >>> timezone.now() datetime.datetime(2012, 5, 18, 13, 0, 49, 803031, tzinfo=<UTC>) 

And django.utils.timezone.make_aware to create an offset-based date and time

 >>> timezone.make_aware(datetime.datetime.now(), timezone.get_default_timezone()) datetime.datetime(2012, 5, 18, 21, 5, 53, 266396, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>) 

Then you could compare the time with offset taking no problem.

In addition, you can convert the offset-awared datetime to an offline naive datetime by disabling the time zone information, then you can compare it with the usual datetime.datetime.now() , under utc.

 >>> t = timezone.now() # offset-awared datetime >>> t.astimezone(timezone.utc).replace(tzinfo=None) datetime.datetime(2012, 5, 18, 13, 11, 30, 705324) 

USE_TZ is True 'by default (in fact it is False by default, but the settings.py file generated by django-admin.py startproject sets it to True ), then if your database supports time zone times, the values ​​of time fields associated with with time, would be aware of the time zone. you can disable it by setting USE_TZ=False (or just delete USE_TZ=True ) in the settings.

+153
May 18 '12 at 13:01
source share



All Articles