Files
MediaCrawler/tools/time_util.py
ztcools 04631ba8e4 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 指向同一时刻。两条测试在修复前均失败。
2026-07-30 15:07:07 +08:00

133 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (c) 2025 relakkes@gmail.com
#
# This file is part of MediaCrawler project.
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/tools/time_util.py
# GitHub: https://github.com/NanmiCoder
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
#
# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则:
# 1. 不得用于任何商业用途。
# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。
# 3. 不得进行大规模爬取或对平台造成运营干扰。
# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。
# 5. 不得用于任何非法或不当的用途。
#
# 详细许可条款请参阅项目根目录下的LICENSE文件。
# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。
# -*- coding: utf-8 -*-
# @Author : relakkes@gmail.com
# @Time : 2023/12/2 12:52
# @Desc : Time utility functions
import time
from datetime import datetime, timedelta, timezone
def get_current_timestamp() -> int:
"""
Get current timestamp (13 digits): 1701493264496
:return:
"""
return int(time.time() * 1000)
def get_current_time() -> str:
"""
Get current time: '2023-12-02 13:01:23'
:return:
"""
return time.strftime('%Y-%m-%d %X', time.localtime())
def get_current_time_hour() -> str:
"""
Get current time with hour: '2023-12-02-13'
:return:
"""
return time.strftime('%Y-%m-%d-%H', time.localtime())
def get_current_date() -> str:
"""
Get current date: '2023-12-02'
:return:
"""
return time.strftime('%Y-%m-%d', time.localtime())
def get_time_str_from_unix_time(unixtime):
"""
Unix integer timestamp ==> datetime string
:param unixtime:
:return:
"""
if int(unixtime) > 1000000000000:
unixtime = int(unixtime) / 1000
return time.strftime('%Y-%m-%d %X', time.localtime(unixtime))
def get_date_str_from_unix_time(unixtime):
"""
Unix integer timestamp ==> date string
:param unixtime:
:return:
"""
if int(unixtime) > 1000000000000:
unixtime = int(unixtime) / 1000
return time.strftime('%Y-%m-%d', time.localtime(unixtime))
def get_unix_time_from_time_str(time_str):
"""
Time string ==> Unix integer timestamp, precise to seconds
:param time_str:
:return:
"""
try:
format_str = "%Y-%m-%d %H:%M:%S"
tm_object = time.strptime(str(time_str), format_str)
return int(time.mktime(tm_object))
except Exception as e:
return 0
pass
def get_unix_timestamp():
return int(time.time())
def rfc2822_to_china_datetime(rfc2822_time):
# Define RFC 2822 format
rfc2822_format = "%a %b %d %H:%M:%S %z %Y"
# Convert RFC 2822 time string to datetime object
dt_object = datetime.strptime(rfc2822_time, rfc2822_format)
# Convert datetime object timezone to China timezone
dt_object_china = dt_object.astimezone(timezone(timedelta(hours=8)))
return dt_object_china
def rfc2822_to_timestamp(rfc2822_time):
# Define RFC 2822 format
rfc2822_format = "%a %b %d %H:%M:%S %z %Y"
# Convert RFC 2822 time string to datetime object
dt_object = datetime.strptime(rfc2822_time, rfc2822_format)
# Convert datetime object to UTC time
dt_utc = dt_object.astimezone(timezone.utc)
# Calculate Unix timestamp from UTC time
timestamp = int(dt_utc.timestamp())
return timestamp
if __name__ == '__main__':
# Example usage
_rfc2822_time = "Sat Dec 23 17:12:54 +0800 2023"
print(rfc2822_to_china_datetime(_rfc2822_time))