# Django DateTimeUTC
[![CI and unit tests](https://github.com/pixeldomain/django-datetime-utc/actions/workflows/ci.yml/badge.svg)](https://github.com/pixeldomain/django-datetime-utc/actions/workflows/ci.yml)
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:
```bash
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:
```python
TIME_ZONE = 'Europe/London'
USE_TZ = True
INSTALLED_APPS = (
#...
'datetimeutc',
)
```
## Usage
In `models.py`:
```python
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:
```python
import datetime
from zoneinfo import ZoneInfo
departure_time = datetime.datetime.now(datetime.UTC)
# or user defined (naive) datetime objects
departure_time = user_datetime.replace(tzinfo=ZoneInfo(settings.TIME_ZONE))
Journey.objects.create(name='Flight to LA', departure_time=departure_time)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/pixeldomain/django-datetime-utc",
"name": "django-datetime-utc",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "django, timestamp without time zone, utc, datetime",
"author": "Darren O'Neill",
"author_email": "darren@pixeldomain.co.uk",
"download_url": "https://files.pythonhosted.org/packages/73/9d/b19612366e7cc72139651194ae3532e0cbae2d89475bf7d38906076717e1/django_datetime_utc-1.0.5.tar.gz",
"platform": null,
"description": "# Django DateTimeUTC\n\n[![CI and unit tests](https://github.com/pixeldomain/django-datetime-utc/actions/workflows/ci.yml/badge.svg)](https://github.com/pixeldomain/django-datetime-utc/actions/workflows/ci.yml)\n\ndjango-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.\n\n\n## Why use this?\n\nThe 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.\n\n## Features\n\n- Supports the default `DateTimeField` options such as `auto_now_add` and `auto_now`\n- Saves all timestamps in UTC\n- Values automatically converted to local time (specified by `TIME_ZONE` in your project `settings.py`) in forms and templates\n\n## Install\n\nFrom PyPi:\n\n```bash\npip install django-datetime-utc\n```\n\nIn `settings.py` add `datetimeutc` to the list of installed apps, set your local time zone and ensure time zone support is enabled:\n\n```python\nTIME_ZONE = 'Europe/London'\nUSE_TZ = True\nINSTALLED_APPS = (\n #...\n 'datetimeutc',\n)\n```\n\n## Usage\n\nIn `models.py`:\n\n```python\nfrom django.db import models\nfrom datetimeutc.fields import DateTimeUTCField\n\nclass Journey(models.Model):\n name = models.CharField(max_length=100)\n departure_time = DateTimeUTCField(null=True)\n arrival_time = DateTimeUTCField(null=True)\n record_created = DateTimeUTCField(auto_now_add=True)\n```\n\n## Notes\n\nIf 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.\n\nUsing the `Journey` model above as an example, to set `departure_time` correctly you would:\n\n```python\nimport datetime\nfrom zoneinfo import ZoneInfo\n\ndeparture_time = datetime.datetime.now(datetime.UTC)\n\n# or user defined (naive) datetime objects\ndeparture_time = user_datetime.replace(tzinfo=ZoneInfo(settings.TIME_ZONE))\n\nJourney.objects.create(name='Flight to LA', departure_time=departure_time)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django UTC datetime field - timestamp without time zone",
"version": "1.0.5",
"project_urls": {
"Homepage": "https://github.com/pixeldomain/django-datetime-utc",
"Repository": "https://github.com/pixeldomain/django-datetime-utc"
},
"split_keywords": [
"django",
" timestamp without time zone",
" utc",
" datetime"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a154e25d7c6f037dfcccd820b10b19412aee8f784bcdb9dbed5759621cd9a8e6",
"md5": "3a0096ee7785d71ea8c9e5684b6ae60a",
"sha256": "1d3a5628291e1471251a7983230b2260af530f6f2967a5591c1a25c8a4c0ae7f"
},
"downloads": -1,
"filename": "django_datetime_utc-1.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3a0096ee7785d71ea8c9e5684b6ae60a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 3734,
"upload_time": "2025-01-30T04:01:57",
"upload_time_iso_8601": "2025-01-30T04:01:57.881838Z",
"url": "https://files.pythonhosted.org/packages/a1/54/e25d7c6f037dfcccd820b10b19412aee8f784bcdb9dbed5759621cd9a8e6/django_datetime_utc-1.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "739db19612366e7cc72139651194ae3532e0cbae2d89475bf7d38906076717e1",
"md5": "8b49f36cd41f14f09d017b29e7b90866",
"sha256": "82cf4d8eaf576850b46111dec19ee10a545f903321946129b504c789872031a3"
},
"downloads": -1,
"filename": "django_datetime_utc-1.0.5.tar.gz",
"has_sig": false,
"md5_digest": "8b49f36cd41f14f09d017b29e7b90866",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 3256,
"upload_time": "2025-01-30T04:01:59",
"upload_time_iso_8601": "2025-01-30T04:01:59.880277Z",
"url": "https://files.pythonhosted.org/packages/73/9d/b19612366e7cc72139651194ae3532e0cbae2d89475bf7d38906076717e1/django_datetime_utc-1.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-30 04:01:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pixeldomain",
"github_project": "django-datetime-utc",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-datetime-utc"
}