first commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
"""Per-user timezone helpers for dates and displayed times."""
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
DEFAULT_TIME_ZONE = 'Europe/Amsterdam'
|
||||
|
||||
|
||||
def timezone_for_name(name):
|
||||
try:
|
||||
return ZoneInfo(name or DEFAULT_TIME_ZONE)
|
||||
except ZoneInfoNotFoundError:
|
||||
return ZoneInfo(DEFAULT_TIME_ZONE)
|
||||
|
||||
|
||||
def timezone_for_user(user):
|
||||
"""Return a user's saved IANA zone without requiring a settings row."""
|
||||
if not getattr(user, 'is_authenticated', False):
|
||||
return timezone_for_name(DEFAULT_TIME_ZONE)
|
||||
settings = getattr(user, 'planner_settings', None)
|
||||
if settings is None:
|
||||
from planner.models import UserSettings
|
||||
settings = UserSettings.objects.filter(user=user).only('time_zone').first()
|
||||
return timezone_for_name(getattr(settings, 'time_zone', DEFAULT_TIME_ZONE))
|
||||
|
||||
|
||||
def localdate_for_user(user, now=None):
|
||||
return timezone.localtime(now or timezone.now(), timezone_for_user(user)).date()
|
||||
Reference in New Issue
Block a user