@@ -410,8 +410,22 @@ async def test_query_daily_days_boundary_exact(logger):
410410@pytest .mark .asyncio
411411async def test_query_daily_groups_by_local_date (logger ):
412412 """UTC 时间 16:30 (= UTC+8 次日 00:30) 应归入本地次日."""
413- # 模拟:北京时间 2026-04-04 00:30 → UTC 2026-04-03 16:30
414- utc_ts = "2026-04-03T16:30:00.000Z"
413+ # 动态计算:取「今天本地日期」的前一天作为基准,
414+ # 构造该日本地 00:30(= UTC 前日 16:30)的时间戳,
415+ # 确保 days=7 窗口必然包含此数据。
416+ now_local = datetime .now (_SHANGHAI )
417+ target_local_date = now_local .date () - timedelta (days = 1 ) # 昨天
418+ target_local_dt = datetime (
419+ target_local_date .year ,
420+ target_local_date .month ,
421+ target_local_date .day ,
422+ 0 ,
423+ 30 ,
424+ 0 ,
425+ tzinfo = _SHANGHAI ,
426+ )
427+ utc_ts = target_local_dt .astimezone (UTC ).strftime ("%Y-%m-%dT%H:%M:%S.%fZ" )
428+
415429 await logger ._db .execute (
416430 """INSERT INTO usage_log (ts, vendor, model_requested, model_served,
417431 input_tokens, output_tokens)
@@ -424,8 +438,8 @@ async def test_query_daily_groups_by_local_date(logger):
424438 rows = await logger .query_daily (days = 7 )
425439
426440 assert len (rows ) == 1
427- # 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为 2026-04-04
428- assert rows [0 ]["date" ] == "2026-04-04"
441+ # 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为 target_local_date
442+ assert rows [0 ]["date" ] == target_local_date . strftime ( "%Y-%m-%d" )
429443
430444
431445@pytest .mark .asyncio
0 commit comments