mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-09-15 14:17:54 +08:00
fix(weibo): 修复 RFC2822 时间转换丢弃时区偏移导致时间戳晚 8 小时
rfc2822_to_timestamp 先用 %z 解析出带偏移的 datetime,再用 replace(tzinfo=timezone.utc) 处理。replace 只是覆盖 tzinfo 而不做换算, +0800 的时间被当成 UTC,算出的时间戳比真实时间晚 8 小时。 改用 astimezone(timezone.utc) 做真正的时区换算,与紧邻上方的 rfc2822_to_china_datetime 保持一致。 影响 store/weibo 落库的 create_time 字段(帖子与评论各一处)。同一条记录里 create_time 与 create_date_time 会相差 8 小时,跨日内容的日期还会整体偏移 一天。 补两条离线单测:一条断言 +0800 输入对应的时间戳,一条断言 create_time 与 create_date_time 指向同一时刻。两条测试在修复前均失败。
This commit is contained in:
@@ -47,3 +47,16 @@ async def test_convert_browser_context_cookies_uses_url_filter():
|
|||||||
browser_context.cookies.assert_awaited_once_with(urls=["https://www.douyin.com"])
|
browser_context.cookies.assert_awaited_once_with(urls=["https://www.douyin.com"])
|
||||||
assert cookie_str == "sessionid=abc"
|
assert cookie_str == "sessionid=abc"
|
||||||
assert cookie_dict == {"sessionid": "abc"}
|
assert cookie_dict == {"sessionid": "abc"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_rfc2822_to_timestamp_converts_utc_offset():
|
||||||
|
# 2023-12-23 17:12:54 +0800 == 2023-12-23 09:12:54 UTC == 1703322774
|
||||||
|
assert utils.rfc2822_to_timestamp("Sat Dec 23 17:12:54 +0800 2023") == 1703322774
|
||||||
|
|
||||||
|
|
||||||
|
def test_rfc2822_to_timestamp_agrees_with_china_datetime():
|
||||||
|
rfc2822_time = "Sat Dec 23 17:12:54 +0800 2023"
|
||||||
|
|
||||||
|
assert utils.rfc2822_to_timestamp(rfc2822_time) == int(
|
||||||
|
utils.rfc2822_to_china_datetime(rfc2822_time).timestamp()
|
||||||
|
)
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ def rfc2822_to_timestamp(rfc2822_time):
|
|||||||
dt_object = datetime.strptime(rfc2822_time, rfc2822_format)
|
dt_object = datetime.strptime(rfc2822_time, rfc2822_format)
|
||||||
|
|
||||||
# Convert datetime object to UTC time
|
# Convert datetime object to UTC time
|
||||||
dt_utc = dt_object.replace(tzinfo=timezone.utc)
|
dt_utc = dt_object.astimezone(timezone.utc)
|
||||||
|
|
||||||
# Calculate Unix timestamp from UTC time
|
# Calculate Unix timestamp from UTC time
|
||||||
timestamp = int(dt_utc.timestamp())
|
timestamp = int(dt_utc.timestamp())
|
||||||
|
|||||||
Reference in New Issue
Block a user