Some useful datetime functions for Django
Project description
Introduction
These utility functions are helpful in handling dates, datetimes, and timezones in Django.
Examples
Let's say you have the following in your Django settings.py
TIME_ZONE = 'EST'
USE_TZ = True
Now let's attempt to write our own tomorrow() function.
from datetime import timedelta
from django.utils.timezone import now
def tomorrow():
# Subtle bug! Django's now() function returns a timezone enabled datetime
# in UTC, regardless of the TIME_ZONE setting. If it's 11 Wednesday
# MORNING in New York, this function will work. But if it's 11 Wednesday
# NIGHT in New York, the UTC timezone is already in Thursday, so in that
# case this code will return Friday.
return now().date() + timedelta(days=1)
Here's one way to fix the problem.
from datetime import timedelta
from djavedt import now
def tomorrow():
# djavedt's now() function returns a timezone enabled datetime in the
# timezone of the TIME_ZONE setting, which is EST in our example. So this
# function will work as expected at 11PM in New York.
return now().date() + timedelta(days=1)
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
djavedt-1.0.0.tar.gz
(4.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djavedt-1.0.0.tar.gz.
File metadata
- Download URL: djavedt-1.0.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18b49e43a9fa8e5af2a40620b51e8748ac6032860b43253a3d4601cbdf56957c
|
|
| MD5 |
9bc5d81750f6e49b7901c8045868dc10
|
|
| BLAKE2b-256 |
e8078d03eba62db1f14c70945948d19b17ade8d9c623862070c3731d8b6ad50d
|
File details
Details for the file djavedt-1.0.0-py3-none-any.whl.
File metadata
- Download URL: djavedt-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09d5e14ffae28112b98f6721ac5c5e77c5e5bb9d9ef74f952711b0f1434958a0
|
|
| MD5 |
664cfedbcd5e9921786b577040d545ea
|
|
| BLAKE2b-256 |
a83d2e21a44d57087daf7418b99b1c5402dc0053ee43f2c3def5bc76942784ca
|