Permalink
Browse files

update_index: use tz-aware datetime where applicable

This will allow Django 1.4 users with USE_TZ=True to use update_index with time
windowing as expected - otherwise the timezone offset needs to be manually
included in the value passed to -a
  • Loading branch information...
1 parent 1fde37a commit cacd868f761d425634c53c340bf6a632199f9b9a @acdha acdha committed Nov 16, 2012
Showing with 11 additions and 3 deletions.
  1. +11 −3 haystack/management/commands/update_index.py
@@ -1,17 +1,25 @@
-import datetime
+from datetime import timedelta
+from optparse import make_option
import os
import warnings
-from optparse import make_option
+
from django import db
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import LabelCommand
from django.db import reset_queries
from django.utils.encoding import smart_str
+
from haystack import connections as haystack_connections
from haystack.constants import DEFAULT_ALIAS
from haystack.query import SearchQuerySet
+try:
+ from django.utils.timezone import now
+except ImportError:
+ from datetime import datetime
+ now = datetime.now
+
DEFAULT_BATCH_SIZE = None
DEFAULT_AGE = None
@@ -135,7 +143,7 @@ def handle(self, *items, **options):
end_date = options.get('end_date')
if age is not None:
- self.start_date = datetime.datetime.now() - datetime.timedelta(hours=int(age))
+ self.start_date = now() - timedelta(hours=int(age))
if start_date is not None:
from dateutil.parser import parse as dateutil_parse

0 comments on commit cacd868

Please sign in to comment.