Skip to main content

Django UTC datetime field - timestamp without time zone

Project description

django-datetime-utc provides DateTimeUTCField, a naive datetime model field. In PostgreSQL this translates to the field timestamp without time zone. All timestamps are saved in UTC.

Why use this?

The problem with using Django’s DateTimeField is unless the default timezone on your (PostgreSQL) database server is set to UTC it doesn’t matter which TZ settings you select in Django, PostgreSQL will save all timestamps in local time with an offset.

Features

  • Supports the default DateTimeField options such as auto_now_add and auto_now

  • Saves all timestamps in UTC

  • Values automatically converted to local time (specified by TIME_ZONE in your project settings.py) in forms and templates

Install

From PyPi:

$ pip install django-datetime-utc

In settings.py add datetimeutc to the list of installed apps, set your local time zone and ensure time zone support is enabled:

TIME_ZONE = 'Europe/London'
USE_TZ = True
INSTALLED_APPS = (
    #...
    'datetimeutc',
)

Usage

In models.py:

from django.db import models
from datetimeutc.fields import DateTimeUTCField

class Journey(models.Model):
    name = models.CharField(max_length=100)
    departure_time = DateTimeUTCField(null=True)
    arrival_time = DateTimeUTCField(null=True)
    record_created = DateTimeUTCField(auto_now_add=True)

Notes

If your code creates datetime objects, they should always be TZ aware so they are automatically converted correctly to UTC (if necessary) before being saved to the database.

Using the Journey model above as an example, to set departure_time correctly you would:

import datetime
from dateutil import tz

departure_time = datetime.datetime.utcnow().replace(tzinfo=tz.gettz('UTC'))

# or user defined (naive) datetime objects
departure_time = user_datetime.replace(tz.gettz(settings.TIME_ZONE))

Journey.objects.create(name='Flight to LA', departure_time=departure_time)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-datetime-utc-1.0.4.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

django_datetime_utc-1.0.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file django-datetime-utc-1.0.4.tar.gz.

File metadata

  • Download URL: django-datetime-utc-1.0.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for django-datetime-utc-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d0b394df93ca252fda9bf2b177fc31b5f2a4d3df993716917a1ccf6cd9588d8d
MD5 90a2d77002cf5b7ffd57d51cd3f37d73
BLAKE2b-256 4a84cfe6c29002046e2754bc6d420656ba63487c326d55b324e28cfe4f30848b

See more details on using hashes here.

File details

Details for the file django_datetime_utc-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_datetime_utc-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6e729f36ddbb33840bf45cb5cf6798274ff3e2a80e658b1a541adf92f3222c5f
MD5 30f1d4a2521ea8fb8c86da7d626d6b62
BLAKE2b-256 6927c4f81e3e5648a9edec62247c03c6c4fe6e7d23319128af85c8e1faf0b452

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page