Skip to main content

Time Zone Utilities for Django Models

Project description

Latest Version Test Status Coverage Status Code Health Supported Python versions License Development Status

django-timezone-utils adds automatic time zone conversions and support utilities to Django.

Please note that this project is currently marked as a development status of Beta. 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_fields, 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.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


Download files

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

Source Distribution

django-timezone-utils-0.6.tar.gz (6.9 kB view details)

Uploaded Source

File details

Details for the file django-timezone-utils-0.6.tar.gz.

File metadata

File hashes

Hashes for django-timezone-utils-0.6.tar.gz
Algorithm Hash digest
SHA256 99c5aadfa37b1278ec1bdad6d4733bc125bf4cad79300c00109e4b2fa11a894d
MD5 9458e51dae9f28dfd6afc64830123420
BLAKE2b-256 57e5118d349d7ca8ab3426ede500c7c5919f8e5c2e0c7373fb1ddd789e8e3a99

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