0 day streak
python / beginner
Snippet

Handling Timezones Correctly

Django's timezone.now() returns an aware datetime object based on the USE_TZ setting, preventing errors when comparing times across regions.

snippet.py
python
1
2
3
4
5
from django.utils import timezone
def get_current_time():
# Always use Django's timezone utility
return timezone.now()
django
Breakdown
1
from django.utils import timezone
Imports Django's utilities for time-aware calculations.
2
return timezone.now()
Returns the current date and time in a safe, project-aware format.