Time Zone Utilities for Django Models
Project description
django-timezone-utils adds automatic time zone conversions and support utilities to Django.
Suggestions, constructive criticism, and feedback are certainly welcomed and appreciated.
Documentation
Documentation for django-timezone-utils is available at Read the Docs.
Inspiration
On multiple occasions, I have had the need to store time zone information to the one model, then base another model’s datetime on that time zone. If you have ever had to deal with this, you will know how complicated this can be.
I created these fields to ease the process of manipulating times based on another field’s or models timezone choice. Instead of having to remember to use Model.clean, we can now create the models with the validation built into the model field.
Quick Example
from datetime import datetime
from timezone_utils.fields import LinkedTZDateTimeField, TimeZoneField
from timezone_utils.choices import PRETTY_ALL_TIMEZONES_CHOICES
class Location(models.Model):
# ...
timezone = TimeZoneField(choices=PRETTY_ALL_TIMEZONES_CHOICES)
def get_location_timezone(obj):
"""Returns the Location.timezone field from above"""
return obj.location.timezone
class LocationReport(models.Model):
# ...
location = models.ForeignKey('app_label.Location', related_name='reports')
# Populates from the Location.timezone
timestamp = LinkedTZDateTimeField(populate_from=get_location_timezone)
class LocationPeriod(models.Model):
# ...
location = models.ForeignKey('app_label.Location', related_name='periods')
# Sets the time to 12:00am in the location.timezone
start = LinkedTZDateTimeField(
populate_from=get_location_timezone,
time_override=datetime.min.time()
)
# Sets the time to 11:59:59.99999pm in the location.timezone
end = LinkedTZDateTimeField(
populate_from=get_location_timezone,
time_override=datetime.max.time()
)
Contributors
Changelog
0.13 Fixed error for Python 3 on PyPi.
0.12 Add support for Django 2.1. Support Python 3.7. Drop support for Django 1.8.
0.11 Removed reference to django.db.models.fields.subclassing.SubfieldBase, which means that only Django 1.8+ is now supported. Removed support for Python versions < 2.6. The Django 1.6 series was the last to support Python 2.6. Added testing support for Django 1.10. Changed development status from Beta to Production/Stable.
0.10 Added testing support for Python 3.5 and Django 1.9.
0.9 Corrected a bug to where time_override caused invalid date due to not converting to the correct timezone first. Refactored conversion code. Added testing support for Django 1.8. Removed Django from setup requirements - the onus of having a supported version of Django is on the developer.
0.8 Corrected a bug to where time_override caused invalid date due to not converting to the correct timezone first. Added choices GROUPED_ALL_TIMEZONES_CHOICES and GROUPED_COMMON_TIMEZONES_CHOICES to the documentation.
0.7 Corrected a bug where datetime.max.time() resulted in incorrect date/time. Changed tests to compare time_override models via string to prevent future regressions. Added choices GROUPED_ALL_TIMEZONES_CHOICES and GROUPED_COMMON_TIMEZONES_CHOICES.
0.6 Added RTD documentation. LinkedTZDateTimeField now returns the datetime object in the overidden timezone and time.
0.5 Bug fix: time override on datetime.min.time() failed to set time properly
0.4 Removed support for Python 2.5
0.3 Code cleanup.
0.2 Multiple bug fixes based on testing.
0.1 Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file django-timezone-utils-0.13.tar.gz
.
File metadata
- Download URL: django-timezone-utils-0.13.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be06d41e0d12f6279f5a1e10ab01c41723ed495754bf12f6ff735baa8aeee863 |
|
MD5 | fe3c6d77caa02c67213d8749c6d6863d |
|
BLAKE2b-256 | e398bde04ad755628d2e5d175344db42d849349a3e516e6cd3f9a706d0cc71de |
File details
Details for the file django_timezone_utils-0.13-py3-none-any.whl
.
File metadata
- Download URL: django_timezone_utils-0.13-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23cb2fe38c8d813f6e8f033f2f5cec87fa7ae63e9737a6d8d032f997254d4044 |
|
MD5 | 67ec4c497452893337f6ddbbfab78636 |
|
BLAKE2b-256 | 353f863f20de67971c26de9e96cb8271b85a9ad303b20ccd350f8c5bcd9ef49c |
File details
Details for the file django_timezone_utils-0.13-py2.py3-none-any.whl
.
File metadata
- Download URL: django_timezone_utils-0.13-py2.py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c35529e7f1d32249add1c0286ba9afffe00cbe22e2d59a0e9dc413bd2aedfb9e |
|
MD5 | 5a05e803dd1c89eea3c88a55879fc4c8 |
|
BLAKE2b-256 | bd82517436e3826d4ba8981e6e7c0a29961cb2e5e801852bf4abd3ffd16ecc0a |
File details
Details for the file django_timezone_utils-0.13-py2-none-any.whl
.
File metadata
- Download URL: django_timezone_utils-0.13-py2-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 543a29fbef1ed253a262e1078bd404010240c16716243864e65082bf2b7162e5 |
|
MD5 | 0c1d420e474d503cba4be6253cfc7487 |
|
BLAKE2b-256 | 5e442b9e1aaa577924263d6d9220251f82a612cc6f57a2d2b4fe316bf24faae5 |