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
1
2
3
4
5
from django.utils import timezonedef get_current_time():# Always use Django's timezone utilityreturn 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.