Zero data with django 1.4 USE_TZ=True

Issue #6 new
Atomic Creative
created an issue

When using USE_TZ=True on django1.4 with sqlite (for sure) and postgre sql I have no data in time_series list. Please fix asap.

Comments (4)

  1. Mikhail Korobov repo owner

    Hi there,

    Can you please provide an example? I can't reproduce it: time_series tests work with django 1.4 TZ enabled for both postgres and sqlite (these configurations are in tox.ini).

    It may be the case the result is not what is expected because django returns date aggregations in UTC ignoring time zones (and currently django-qsstats-magic follows this), see

    [copy-paste from github]

  2. Mikhail Porokhovnichenko

    Hello, Mike,

    I've got this problem too. I'm working with Django==1.4 USE_TZ=True settings and using PostgreSQL 9.x.

    Here is simple workaround only for "_fast_time_series" method:

    --- qsstats/
    +++ qsstats/
    @@ -122,6 +122,12 @@ class QuerySetStats(object):
             stat_list = []
             dt = start
    +        try:
    +            from django.utils.timezones import utc
    +            dt = dt.replace(tzinfo=utc)
    +            end = end.replace(tzinfo=utc)
    +        except ImportError:
    +            pass
             while dt < end:
                 idx = 0
                 value = 0

    Sorry for just diff snippet. Unfortunely I haven't I enough time to make the full patch and cover code with test cases. I hope, that helps you to close this issue :-)

  3. Anonymous

    I can confirm this is happening on Django 1.5 with USE_TZ = True and sqlite, it looks like the ticket linked above has been resolved in the last 3 weeks, It didn't make the 1.5 cut, probably be 1.6 now before this is resolved.

    I've had a look over the tests vs. what code I use at the moment and I suspected it was my use of utcnow() instead of now() like the tests. I've had a play around but can't seem to get it in a working state.

    Thankfully my app doesn't require USE_TZ at the moment, so I can avoid this.

    Here is a snippet of when i'm constructing the QSS, the model is a simple key/datetime/value model which is prefiltered by key.

    qs = qsstats.QuerySetStats(q, 'added', module.get_aggregate_function() or Avg('value'))
    week = qs.time_series(datetime.utcnow() - timedelta(weeks=1), datetime.utcnow())
    month = qs.time_series(datetime.utcnow() - timedelta(weeks=4), datetime.utcnow())
    year = qs.time_series(datetime.utcnow() - timedelta(weeks=52), datetime.utcnow(), interval='weeks')
  4. Log in to comment