fix(merchant): 操作人统计时间筛选兼容 USE_TZ=False
MySQL 在关闭 USE_TZ 时不接受 timezone-aware datetime,解析为 naive 再查询。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -15,22 +15,35 @@ from products.models import ShangpinLeixing
|
||||
|
||||
|
||||
def _parse_dt(value, end_of_day=False):
|
||||
"""解析时间;USE_TZ=False 时必须返回 naive datetime(MySQL)。"""
|
||||
if value is None or value == '':
|
||||
return None
|
||||
dt = None
|
||||
if isinstance(value, datetime):
|
||||
return value
|
||||
text = str(value).strip().replace('/', '-')
|
||||
for fmt in ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d'):
|
||||
try:
|
||||
dt = datetime.strptime(text, fmt)
|
||||
if fmt == '%Y-%m-%d' and end_of_day:
|
||||
dt = dt.replace(hour=23, minute=59, second=59)
|
||||
if timezone.is_naive(dt):
|
||||
dt = timezone.make_aware(dt, timezone.get_current_timezone())
|
||||
return dt
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
dt = value
|
||||
else:
|
||||
text = str(value).strip().replace('/', '-')
|
||||
for fmt in ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d'):
|
||||
try:
|
||||
dt = datetime.strptime(text, fmt)
|
||||
if fmt == '%Y-%m-%d' and end_of_day:
|
||||
dt = dt.replace(hour=23, minute=59, second=59)
|
||||
break
|
||||
except ValueError:
|
||||
continue
|
||||
if dt is None:
|
||||
return None
|
||||
|
||||
from django.conf import settings
|
||||
use_tz = bool(getattr(settings, 'USE_TZ', False))
|
||||
|
||||
if use_tz:
|
||||
if timezone.is_naive(dt):
|
||||
dt = timezone.make_aware(dt, timezone.get_current_timezone())
|
||||
else:
|
||||
if timezone.is_aware(dt):
|
||||
dt = timezone.make_naive(dt, timezone.get_current_timezone())
|
||||
return dt
|
||||
|
||||
|
||||
def _money(val):
|
||||
|
||||
Reference in New Issue
Block a user