def localized_date(date=None, timezone=None):
"""
Returns the given date, localized to the default timezone.
If no date is given, return the localized current date.
timezone should be a valid timezone object obtained via pytz.
"""
if not timezone:
timezone = TZ
if not date:
date = pytz.utc.localize(datetime.utcnow())
if not date.tzinfo:
# Attempt to guezz current server timezone
# Should be ok for any date coming fom a naive call to datetime.now()
date = date.replace(tzinfo=tzlocal())
return date.astimezone(timezone)
评论列表
文章目录