8000 JSONField: Better handling of non-UTC dates · Issue #95 · django-extensions/django-extensions · GitHub
[go: up one dir, main page]

Skip to content

JSONField: Better handling of non-UTC dates #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
acdha opened this issue Apr 7, 2011 · 6 comments
Closed

JSONField: Better handling of non-UTC dates #95

acdha opened this issue Apr 7, 2011 · 6 comments

Comments

@acdha
Copy link
Contributor
acdha commented Apr 7, 2011

https://github.com/django-extensions/django-extensions/blob/master/django_extensions/db/fields/json.py#L26 causes assertion failures if the system timezone isn't UTC. It would be preferable to support datetime's native tz handling if e.g. pytz is installed rather than simply crashing.

@akaihola
Copy link
Contributor

In one use case I'd be storing datetime.datetime.utcnow() values in a JSONField and later comparing those against one another. I don't think that settings.TIME_ZONE would even have any effect in this case.

Maybe a warning about timezone issues in the documentation would suffice?

@joestump
Copy link

It's also somewhat annoying that this breaks when you set settings.TIME_ZONE to None and the machine's timezone is set to UTC.

@acdha
Copy link
Contributor Author
acdha commented Aug 1, 2012

I've proposed the following solution within our group - how does this sound? I thought about using pytz but I think this simple approach of requiring that the user either provide non-naive datetime instances or that the system time zone be UTC is sufficient:

# See http://docs.python.org/library/datetime.html
from datetime import tzinfo, timedelta, datetime

ZERO = timedelta(0)
HOUR = timedelta(hours=1)

class UTC(tzinfo):
    """UTC"""

    def utcoffset(self, dt):
        return ZERO

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return ZERO

utc = UTC()

class JSONEncoder(simplejson.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return str(obj)
        elif isinstance(obj, datetime.datetime):
            if obj.tzinfo is None:
                assert settings.TIME_ZONE == 'UTC'
            else:
                obj = obj.astimezone(utc)
            return obj.strftime('%Y-%m-%dT%H:%M:%SZ')
        return simplejson.JSONEncoder.default(self, obj)

acdha added a commit to acdha/django-extensions that referenced this issue Aug 2, 2012
Extends the JSONField implementation to allow arbitrary timezones
as long as the user passes in non-naive datetime instances. Naive
values are still handled as in the past, requiring
settings.TIME_ZONE to be UTC.

Minor PEP8 cleanup
@camilonova
Copy link
Member

@acdha django already handles this, recently it was integrated into django-extensions, so it now uses it by default.

https://github.com/camilonova/django-extensions/blob/master/django_extensions/db/fields/json.py#L17

Seems you can close this issue.

@acdha
Copy link
Contributor Author
acdha commented Oct 5, 2013

@camilonova Was that added in Django 1.5 or 1.6? It does sound like we could simply close this either way since the release schedule for 1.6 and 1.7 is pretty aggressive and there's less reason to say django-extensions needs to care about anything which is more than one or two major releases back.

@camilonova
Copy link
Member

@acdha thats right, it was added in Django 1.5 so seems reasonable to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0