当前位置:首页>python>Python datetime模块终极指南:告别时间混乱,掌握专业级时间处理!

Python datetime模块终极指南:告别时间混乱,掌握专业级时间处理!

  • 2026-03-26 09:14:22
Python datetime模块终极指南:告别时间混乱,掌握专业级时间处理!
引言:为什么你的时间处理总是出问题?
在日常开发中,你是否遇到过这些令人头疼的场景?
  • 用户在不同时区提交的时间数据,在数据库中存储后竟然对不上?
  • 计算两个日期之间的天数差,结果总是少一天或多一天?
  • 格式化日期字符串时,%Y和%y傻傻分不清,总是报ValueError?
  • 处理夏令时转换时,发现同一时间点在系统中存在两个不同的表示?
  • 选择日期时间库时,在datetime、dateutil、arrow、pandas之间犹豫不决?
如果你正在为这些问题烦恼,那么恭喜你,今天就是你告别时间处理噩梦的转折点!datetime模块作为Python标准库的核心组件,提供了完整、专业、经过千锤百炼的时间处理能力,是每一位Python开发者必须掌握的基本功。
本文将带你从零开始,系统性地掌握datetime模块的每一个核心概念,通过大量实战代码示例和最佳实践,让你彻底理解Python时间处理的精髓,成为时间管理的大师!
一、datetime模块概述
1.1 模块定位与核心价值
datetime模块是Python标准库中专门用于处理日期和时间的核心模块。它提供了一套完整、类型安全、高性能的API,能够满足从简单日期计算到复杂时区处理的各种需求。
核心价值体现在五个方面:
  1. 完整性:涵盖日期(date)、时间(time)、日期时间(datetime)、时间间隔(timedelta)、时区(timezone)等所有时间相关概念
  2. 准确性:支持微秒级精度,正确处理闰年、闰秒、夏令时等复杂时间规则
  3. 类型安全:每个时间概念都有专门的类表示,编译时和运行时都能发现错误
  4. 国际化:支持时区转换、ISO 8601标准格式、本地化显示
  5. 生态兼容:与pandas、numpy、SQLAlchemy等主流库无缝集成
1.2 模块核心组件一览
datetime模块主要包含以下5个核心类和2个关键函数:
类/函数
主要用途
简单示例
date
处理年、月、日
date(2024, 12, 25)
time
处理时、分、秒、微秒
time(14, 30, 45)
datetime
日期时间组合(最常用)
datetime(2024, 12, 25, 14, 30, 45)
timedelta
表示时间间隔
timedelta(days=7, hours=3)
timezone
固定时差时区
timezone(timedelta(hours=8))
strftime()
时间→字符串
dt.strftime("%Y-%m-%d %H:%M:%S")
strptime()
字符串→时间
datetime.strptime("2024-12-25", "%Y-%m-%d")
在Python 3.9+中,还可以使用zoneinfo模块(标准库新增)来处理更复杂的时区需求,我们会在时区处理部分详细介绍。
二、date类:纯日期处理
2.1 功能说明
date类用于处理不带时间信息的日期,即年、月、日的组合。它适用于生日、纪念日、合同生效日、节假日等只需要日期的场景。
2.2 基础使用示例
from datetime import date# 创建日期对象d1 = date(20241225)  # 2024年圣诞节print(f"圣诞节: {d1}")  # 输出: 2024-12-25# 获取当前日期today = date.today()print(f"今天是: {today}")  # 输出: 2026-03-17# 获取日期组件print(f"年: {d1.year}, 月: {d1.month}, 日: {d1.day}")  # 输出: 年: 2024, 月: 12, 日: 25# 获取星期几 (0=周一, 6=周日)print(f"星期几: {d1.weekday()}")  # 输出: 2 (周三)print(f"ISO星期几: {d1.isoweekday()}")  # 输出: 3 (1=周一, 7=周日)
2.3 日期运算与比较
from datetime import date, timedelta# 日期比较d1 = date(20241225)d2 = date(202511)print(f"d1 < d2: {d1 < d2}")  # 输出: Trueprint(f"d1 == d2: {d1 == d2}")  # 输出: False# 日期加减today = date.today()yesterday = today - timedelta(days=1)tomorrow = today + timedelta(days=1)next_week = today + timedelta(weeks=1)print(f"昨天: {yesterday}")print(f"明天: {tomorrow}")print(f"一周后: {next_week}")# 日期差计算christmas = date(20241225)new_year = date(202511)days_between = new_year - christmas  # 返回timedelta对象print(f"圣诞节到元旦还有 {days_between.days} 天")  # 输出: 7天
三、time类:纯时间处理
3.1 功能说明
time类用于处理不带日期信息的时间,即时、分、秒、微秒的组合。它适用于会议时间、营业时间、航班时刻等只需要时间的场景。
3.2 基础使用示例
from datetime import time# 创建时间对象t1 = time(143045)  # 14:30:45t2 = time(143045123456)  # 14:30:45.123456 (微秒级精度)t3 = time(900, tzinfo=None)  # 无时区信息print(f"会议时间: {t1}")  # 输出: 14:30:45print(f"精确时间: {t2}")  # 输出: 14:30:45.123456print(f"带微秒: {t2.microsecond}")  # 输出: 123456# 获取时间组件print(f"时: {t1.hour}, 分: {t1.minute}, 秒: {t1.second}")
3.3 时间格式化
from datetime import time# 创建时间对象meeting_time = time(143045)# 格式化为字符串formatted = meeting_time.strftime("%H:%M:%S")print(f"24小时制: {formatted}")  # 输出: 14:30:45formatted_12h = meeting_time.strftime("%I:%M:%S %p")print(f"12小时制: {formatted_12h}")  # 输出: 02:30:45 PM# ISO格式iso_time = meeting_time.isoformat()print(f"ISO格式: {iso_time}")  # 输出: 14:30:45
四、datetime类:完整日期时间处理
4.1 功能说明
datetime类是datetime模块中最常用、功能最全面的类,它结合了date和time的功能,可以处理带日期和时间信息的完整时间点。几乎所有实际业务场景都应该使用datetime类。
4.2 基础使用示例
from datetime import datetime# 获取当前日期时间now = datetime.now()print(f"当前时间: {now}")  # 输出: 2026-03-17 14:30:45.123456# 创建指定日期时间dt1 = datetime(20241225143045)print(f"圣诞节下午: {dt1}")  # 输出: 2024-12-25 14:30:45# 获取日期和时间组件print(f"年: {dt1.year}, 月: {dt1.month}, 日: {dt1.day}")print(f"时: {dt1.hour}, 分: {dt1.minute}, 秒: {dt1.second}, 微秒: {dt1.microsecond}")# 提取date和time部分print(f"日期部分: {dt1.date()}")  # 输出: 2024-12-25print(f"时间部分: {dt1.time()}")  # 输出: 14:30:45
4.3 时间戳转换
时间戳是计算机领域表示时间的通用方式,指从1970年1月1日00:00:00 UTC开始到当前时间的秒数。
from datetime import datetime# datetime转时间戳dt = datetime(20241225143045)timestamp = dt.timestamp()print(f"时间戳: {timestamp}")  # 输出: 1735137045.0# 时间戳转datetimedt_from_ts = datetime.fromtimestamp(timestamp)print(f"从时间戳恢复: {dt_from_ts}")  # 输出: 2024-12-25 14:30:45# UTC时间戳转datetimedt_utc = datetime.utcfromtimestamp(timestamp)print(f"UTC时间: {dt_utc}")  # 输出: 2024-12-25 06:30:45 (假设本地时区+8)
4.4 日期时间运算
from datetime import datetime, timedelta# 当前时间now = datetime.now()# 未来的时间one_hour_later = now + timedelta(hours=1)three_days_later = now + timedelta(days=3)two_weeks_ago = now - timedelta(weeks=2)print(f"一小时后: {one_hour_later}")print(f"三天后: {three_days_later}")print(f"两周前: {two_weeks_ago}")# 复合时间间隔complex_delta = timedelta(days=5, hours=12, minutes=30, seconds=45)future = now + complex_deltaprint(f"5天12小时30分45秒后: {future}")# 时间差计算start = datetime(202411900)end = datetime(202412103015)time_diff = end - start  # 返回timedelta对象print(f"总秒数: {time_diff.total_seconds()}")  # 输出: 91815.0print(f"天数: {time_diff.days}, 秒数: {time_diff.seconds}")  # 输出: 1天, 18015秒
五、timedelta类:时间间隔处理
5.1 功能说明
timedelta类用于表示两个时间点之间的间隔。它支持天、秒、微秒三个基本单位,所有其他时间单位(小时、分钟、周等)都会转换为这三个单位的组合。
5.2 基础使用示例
from datetime import timedelta# 创建时间间隔td1 = timedelta(days=7)  # 一周td2 = timedelta(hours=12, minutes=30)  # 12小时30分钟td3 = timedelta(weeks=2, days=3, hours=8)  # 2周3天8小时print(f"一周: {td1}")  # 输出: 7 days, 0:00:00print(f"12小时30分: {td2}")  # 输出: 12:30:00print(f"2周3天8小时: {td3}")  # 输出: 17 days, 8:00:00# 获取时间间隔组件print(f"天数: {td3.days}, 秒数: {td3.seconds}")  # 输出: 17天, 28800秒# 总秒数print(f"总秒数: {td3.total_seconds()}")  # 输出: 1504800.0
5.3 timedelta运算
from datetime import timedelta# timedelta加减运算td1 = timedelta(days=5, hours=12)td2 = timedelta(days=3, hours=6)td_sum = td1 + td2td_diff = td1 - td2print(f"td1 + td2 = {td_sum}")  # 输出: 8 days, 18:00:00print(f"td1 - td2 = {td_diff}")  # 输出: 2 days, 6:00:00# timedelta乘除运算td3 = timedelta(days=2, hours=6)td_multiplied = td3 * 3# 乘以整数td_divided = td3 / 2# 除以整数print(f"td3 * 3 = {td_multiplied}")  # 输出: 6 days, 18:00:00print(f"td3 / 2 = {td_divided}")  # 输出: 1 day, 3:00:00# timedelta比较运算print(f"td1 > td2: {td1 > td2}")  # 输出: Trueprint(f"td1 == td2: {td1 == td2}")  # 输出: False
六、时区处理:从naive到aware
6.1 naive与aware对象
这是datetime模块中最关键的概念区分:
  • naive对象:不带时区信息的日期时间对象,无法确定其表示的绝对时间点
  • aware对象:带时区信息的日期时间对象,可以唯一确定一个绝对时间点
from datetime import datetime# naive对象(不推荐在实际业务中使用)naive_dt = datetime(20241225143045)print(f"naive对象: {naive_dt}, tzinfo: {naive_dt.tzinfo}")  # tzinfo=None# aware对象(推荐)from datetime import timezone, timedelta# 使用timezone类(固定时差)utc_dt = datetime(20241225143045, tzinfo=timezone.utc)beijing_dt = datetime(20241225143045                      tzinfo=timezone(timedelta(hours=8)))print(f"UTC时间: {utc_dt}")  # 输出: 2024-12-25 14:30:45+00:00print(f"北京时间: {beijing_dt}")  # 输出: 2024-12-25 14:30:45+08:00
6.2 zoneinfo模块(Python 3.9+)
Python 3.9引入了zoneinfo模块,提供了对IANA时区数据库的完整支持,是处理时区的首选方案。
from datetime import datetimefrom zoneinfo import ZoneInfo# 创建带时区的当前时间beijing_now = datetime.now(ZoneInfo("Asia/Shanghai"))newyork_now = datetime.now(ZoneInfo("America/New_York"))print(f"北京时间: {beijing_now}")  # 例如: 2026-03-17 14:30:45.123456+08:00print(f"纽约时间: {newyork_now}")  # 例如: 2026-03-17 02:30:45.123456-04:00# 时区转换utc_time = beijing_now.astimezone(ZoneInfo("UTC"))print(f"对应的UTC时间: {utc_time}")  # 例如: 2026-03-17 06:30:45.123456+00:00# 处理夏令时paris_time = datetime(20243312300, tzinfo=ZoneInfo("Europe/Paris"))print(f"巴黎时间 (夏令时切换): {paris_time}")  # 自动处理夏令时
6.3 时区处理最佳实践
from datetime import datetimefrom zoneinfo import ZoneInfodefcreate_aware_datetime(year, month, day, hour=0, minute=0, second=0, tz_name="UTC"):"""创建带时区的日期时间对象"""return datetime(year, month, day, hour, minute, second, tzinfo=ZoneInfo(tz_name))defconvert_timezone(dt, target_tz_name):"""安全转换时区"""if dt.tzinfo isNone:raise ValueError("必须使用时区感知对象")return dt.astimezone(ZoneInfo(target_tz_name))defstore_in_utc(dt):"""将时间转换为UTC存储(最佳实践)"""if dt.tzinfo isNone:raise ValueError("必须使用时区感知对象")return dt.astimezone(ZoneInfo("UTC"))defdisplay_local_time(dt, user_tz_name):"""根据用户时区显示时间"""    utc_time = dt.astimezone(ZoneInfo("UTC"))return utc_time.astimezone(ZoneInfo(user_tz_name))# 使用示例meeting_time = create_aware_datetime(202412251430, tz_name="Asia/Shanghai")utc_storage = store_in_utc(meeting_time)display_time = display_local_time(utc_storage, "America/New_York")print(f"原始会议时间: {meeting_time}")print(f"UTC存储时间: {utc_storage}")print(f"纽约显示时间: {display_time}")
七、格式化与解析:strftime与strptime
7.1 strftime:日期时间转字符串
strftime()方法将日期时间对象格式化为指定格式的字符串。
from datetime import datetimenow = datetime.now()# 常用格式formats = {"标准格式": now.strftime("%Y-%m-%d %H:%M:%S"),  # 2026-03-17 14:30:45"中文格式": now.strftime("%Y年%m月%d日 %H时%M分%S秒"),  # 2026年03月17日 14时30分45秒"美国格式": now.strftime("%m/%d/%Y %I:%M %p"),  # 03/17/2026 02:30 PM"ISO格式": now.strftime("%Y-%m-%dT%H:%M:%S"),  # 2026-03-17T14:30:45"周信息": now.strftime("%A, %B %d, %Y"),  # Tuesday, March 17, 2026"文件名安全": now.strftime("%Y%m%d_%H%M%S"),  # 20260317_143045}for name, fmt in formats.items():    print(f"{name}{fmt}")
7.2 strptime:字符串转日期时间
strptime()方法将字符串解析为日期时间对象,格式必须严格匹配。
from datetime import datetime# 解析不同格式的字符串date_strings = [    ("2024-12-25 14:30:45""%Y-%m-%d %H:%M:%S"),    ("25/12/2024 14:30""%d/%m/%Y %H:%M"),    ("2024年12月25日""%Y年%m月%d日"),    ("Dec 25, 2024 02:30 PM""%b %d, %Y %I:%M %p"),    ("2024-12-25T14:30:45+08:00""%Y-%m-%dT%H:%M:%S%z"),  # 带时区]for date_str, format_str in date_strings:    dt = datetime.strptime(date_str, format_str)    print(f"字符串: {date_str} -> 解析结果: {dt}")
7.3 常用格式化代码速查表
代码
含义
示例
%Y
四位数年份
2024
%y
两位数年份
24
%m
两位数月份
01-12
%B
月份全名
January
%b
月份缩写
Jan
%d
两位数日期
01-31
%A
星期全名
Monday
%a
星期缩写
Mon
%H
24小时制小时
00-23
%I
12小时制小时
01-12
%p
AM/PM
AM
%M
分钟
00-59
%S
00-59
%f
微秒
000000-999999
%z
时区偏移
+0800
%Z
时区名称
CST
八、实战应用场景
8.1 场景一:跨时区会议安排系统
from datetime import datetime, timedeltafrom zoneinfo import ZoneInfodefschedule_meeting(organizer_tz, participants, duration_hours=1):"""安排跨时区会议"""# 获取组织者当前时间    organizer_now = datetime.now(ZoneInfo(organizer_tz))# 找到最近的整点时间if organizer_now.minute > 0or organizer_now.second > 0or organizer_now.microsecond > 0:        start_time = organizer_now.replace(            minute=0, second=0, microsecond=0        ) + timedelta(hours=1)else:        start_time = organizer_now# 计算结束时间    end_time = start_time + timedelta(hours=duration_hours)# 为每个参与者转换时间    meeting_schedule = {"organizer": {"timezone": organizer_tz,"start": start_time,"end": end_time        },"participants": {}    }for participant, tz in participants.items():        participant_start = start_time.astimezone(ZoneInfo(tz))        participant_end = end_time.astimezone(ZoneInfo(tz))        meeting_schedule["participants"][participant] = {"timezone": tz,"start": participant_start,"end": participant_end        }return meeting_schedule# 使用示例participants = {"张三""Asia/Shanghai","李四""America/New_York","王五""Europe/London","赵六""Australia/Sydney"}schedule = schedule_meeting("Asia/Shanghai", participants, duration_hours=1.5)print("会议安排:")print(f"组织者 ({schedule['organizer']['timezone']}):")print(f"  开始: {schedule['organizer']['start'].strftime('%Y-%m-%d %H:%M')}")print(f"  结束: {schedule['organizer']['end'].strftime('%Y-%m-%d %H:%M')}")print("\n参与者:")for name, info in schedule["participants"].items():    print(f"{name} ({info['timezone']}):")    print(f"  开始: {info['start'].strftime('%Y-%m-%d %H:%M')}")    print(f"  结束: {info['end'].strftime('%Y-%m-%d %H:%M')}")
8.2 场景二:工作日计算与排期
from datetime import datetime, date, timedeltafrom zoneinfo import ZoneInfodefcalculate_workdays(start_date, num_days, holidays=None, timezone="Asia/Shanghai"):"""计算工作日,排除周末和节假日"""if holidays isNone:        holidays = set()  # 可以预先加载法定节假日    current_date = start_date    workdays_count = 0    workdays_list = []while workdays_count < num_days:# 检查是否是周末 (周一=0, 周日=6)if current_date.weekday() >= 5:  # 5=周六, 6=周日            current_date += timedelta(days=1)continue# 检查是否是节假日if current_date in holidays:            current_date += timedelta(days=1)continue        workdays_list.append(current_date)        workdays_count += 1        current_date += timedelta(days=1)return workdays_listdefcreate_project_timeline(start_date, tasks, holidays=None):"""创建项目时间线"""    timeline = []    current_date = start_datefor task in tasks:        name = task["name"]        workdays = task["workdays"]# 计算结束日期        workdays_list = calculate_workdays(            current_date, workdays, holidays        )        end_date = workdays_list[-1if workdays_list else current_date        timeline.append({"task": name,"start_date": current_date,"end_date": end_date,"workdays": workdays,"actual_days": (end_date - current_date).days + 1        })        current_date = end_date + timedelta(days=1)return timeline# 使用示例tasks = [    {"name""需求分析""workdays"5},    {"name""系统设计""workdays"10},    {"name""开发实现""workdays"20},    {"name""测试验收""workdays"10}]# 假设今天是项目开始日project_start = date.today()timeline = create_project_timeline(project_start, tasks)print("项目时间线:")for item in timeline:    print(f"{item['task']}:")    print(f"  开始: {item['start_date'].strftime('%Y-%m-%d')}")    print(f"  结束: {item['end_date'].strftime('%Y-%m-%d')}")    print(f"  工作日: {item['workdays']}天")    print(f"  实际跨度: {item['actual_days']}天")    print()
8.3 场景三:日志时间戳统一处理
from datetime import datetimefrom zoneinfo import ZoneInfoimport jsonclassLogTimestampProcessor:"""日志时间戳处理器"""def__init__(self, default_timezone="UTC"):        self.default_timezone = default_timezonedefparse_log_timestamp(self, timestamp_str, source_timezone=None):"""解析日志时间戳"""# 尝试常见格式        formats = [            ("%Y-%m-%dT%H:%M:%S.%f%z"True),  # ISO带时区            ("%Y-%m-%dT%H:%M:%S%z"True),     # ISO带时区(无微秒)            ("%Y-%m-%d %H:%M:%S.%f"False),   # 带微秒无时区            ("%Y-%m-%d %H:%M:%S"False),      # 标准格式无时区            ("%d/%b/%Y:%H:%M:%S %z"True),    # Nginx格式            ("%a %b %d %H:%M:%S %Y"False),   # C标准格式        ]for fmt, has_tz in formats:try:                dt = datetime.strptime(timestamp_str, fmt)# 如果没有时区信息,根据参数添加ifnot has_tz and source_timezone:                    dt = dt.replace(tzinfo=ZoneInfo(source_timezone))elifnot has_tz:                    dt = dt.replace(tzinfo=ZoneInfo(self.default_timezone))return dtexcept ValueError:continueraise ValueError(f"无法解析的时间戳格式: {timestamp_str}")defnormalize_to_utc(self, dt):"""统一转换为UTC时间"""if dt.tzinfo isNone:            dt = dt.replace(tzinfo=ZoneInfo(self.default_timezone))return dt.astimezone(ZoneInfo("UTC"))defformat_for_storage(self, dt):"""格式化为存储格式"""        utc_dt = self.normalize_to_utc(dt)return {"iso_utc": utc_dt.isoformat(),"timestamp": utc_dt.timestamp(),"local_display": dt.strftime("%Y-%m-%d %H:%M:%S %Z")        }# 使用示例processor = LogTimestampProcessor(default_timezone="Asia/Shanghai")# 模拟不同来源的日志logs = ["2024-12-25T14:30:45.123456+08:00",  # ISO带时区"2024-12-25 14:30:45",               # 无时区"25/Dec/2024:14:30:45 +0800",        # Nginx格式]print("日志时间戳处理结果:")for log in logs:try:        parsed = processor.parse_log_timestamp(log)        normalized = processor.normalize_to_utc(parsed)        formatted = processor.format_for_storage(parsed)        print(f"\n原始: {log}")        print(f"解析: {parsed}")        print(f"UTC: {normalized}")        print(f"存储格式: {json.dumps(formatted, indent=2, ensure_ascii=False)}")except ValueError as e:        print(f"\n错误: {e}")
九、常见问题与解决方案
9.1 问题一:naive与aware对象混合运算
错误现象
from datetime import datetime, timezonenaive_dt = datetime(20241225143045)aware_dt = datetime(20241225143045, tzinfo=timezone.utc)# 会引发TypeErrorresult = aware_dt - naive_dt
解决方案
# 方案1:统一转换为aware对象from datetime import datetime, timezonenaive_dt = datetime(20241225143045)aware_dt = datetime(20241225143045, tzinfo=timezone.utc)# 将naive对象转换为aware对象(假设为UTC)naive_as_aware = naive_dt.replace(tzinfo=timezone.utc)result = aware_dt - naive_as_aware# 方案2:统一转换为naive对象(不推荐,会丢失时区信息)aware_as_naive = aware_dt.replace(tzinfo=None)result = aware_as_naive - naive_dt
9.2 问题二:时区转换中的夏令时陷阱
错误现象
from datetime import datetimeimport pytz  # 旧版方法,容易出错# 错误:直接使用replace添加时区dt = datetime(20243312300)paris_time = dt.replace(tzinfo=pytz.timezone("Europe/Paris"))# 结果可能不正确
正确做法
from datetime import datetimefrom zoneinfo import ZoneInfo# 方法1:创建时直接指定时区dt = datetime(20243312300, tzinfo=ZoneInfo("Europe/Paris"))# 方法2:使用zoneinfo(Python 3.9+推荐)dt = datetime(20243312300)paris_time = dt.replace(tzinfo=ZoneInfo("Europe/Paris"))
9.3 问题三:日期格式化字符串混淆
常见混淆
  • %Y vs %y:四位数年份 vs 两位数年份
  • %m vs %M:月份 vs 分钟
  • %H vs %I:24小时制 vs 12小时制
记忆技巧
# Y大(大写)表示年份完整,y小(小写)表示年份简写"%Y-%m-%d"# 2024-12-25 (正确)"%y-%m-%d"# 24-12-25 (容易混淆)# m是month(月份),M是Minute(分钟)"%Y-%m-%d %H:%M:%S"# 2024-12-25 14:30:45# H是Hour(24小时制),I是I(看起来像1,12小时制需要搭配%p)"%H:%M:%S"# 14:30:45"%I:%M:%S %p"# 02:30:45 PM
9.4 问题四:时间戳单位混淆
错误现象
import timefrom datetime import datetime# 混淆秒和毫秒timestamp_ms = 1735137045123# 毫秒时间戳dt_wrong = datetime.fromtimestamp(timestamp_ms)  # 错误!会解析为秒# 正确做法dt_correct = datetime.fromtimestamp(timestamp_ms / 1000.0)
最佳实践
defsafe_timestamp_conversion(timestamp, unit='seconds'):"""安全时间戳转换"""if unit == 'milliseconds':        timestamp = timestamp / 1000.0elif unit == 'microseconds':        timestamp = timestamp / 1000000.0return datetime.fromtimestamp(timestamp)# 使用时明确指定单位dt = safe_timestamp_conversion(1735137045123, unit='milliseconds')
十、总结与扩展学习
10.1 核心要点回顾
通过今天的深度解析,我们系统性地掌握了datetime模块的完整知识体系:
  1. 基础类:date、time、datetime、timedelta的核心用法
  2. 时区处理:naive与aware对象的区别,zoneinfo模块的正确使用
  3. 格式化解析:strftime与strptime的灵活运用
  4. 实战场景:跨时区会议、工作日计算、日志处理等真实业务需求
  5. 避坑指南:常见问题的识别与解决方案
10.2 进阶学习路径
想要在时间处理领域达到专家水平?建议继续深入学习:
  1. 第三方库对比
    • dateutil:功能更丰富的日期时间处理库
    • arrow:更人性化的API设计
    • pandas.Timestamp:数据分析场景的最佳选择
  2. 数据库时间处理
    • PostgreSQL的timestamptz类型
    • MySQL的DATETIMETIMESTAMP区别
    • MongoDB的ISODate格式
  3. 分布式系统时间
    • NTP时间同步协议
    • 逻辑时钟与向量时钟
    • 分布式事务中的时间戳排序
  4. 性能优化
    • 避免频繁的时区转换
    • 使用时间戳进行批量计算
    • 时间数据的缓存策略
10.3 实战项目建议
真正掌握datetime模块需要实践,建议完成以下项目:
  1. 个人时间管理工具
    • 实现番茄工作法计时器
    • 统计每日/每周工作时间分布
    • 生成时间使用分析报告
  2. 跨时区协作平台
    • 自动计算团队成员的工作时间重叠
    • 智能推荐会议时间
    • 实时显示不同时区的当前时间
  3. 历史数据分析系统
    • 处理不同时间格式的历史数据
    • 按时间维度进行数据聚合
    • 时间序列预测模型
10.4 最后的话
时间处理是软件开发中的基础能力,但往往也是最容易被忽视的部分。一个健壮的系统,必然有严谨的时间处理逻辑。
记住datetime模块的三大黄金法则:
  1. 始终使用时区感知对象:避免naive对象在实际业务中使用
  2. 统一存储为UTC时间:在存储和传输层使用UTC,仅在展示层转换时区
  3. 明确时间单位:在使用时间戳时,始终明确是秒、毫秒还是微秒
掌握这些原则,你就能写出更加健壮、可靠、可维护的代码。时间处理不再是你的痛点,而是你代码质量的亮点!

下一篇预告:明天我们将深入探索json模块,学习如何高效、安全地处理JSON数据,掌握序列化复杂对象、性能优化和安全防护的完整技能体系。敬请期待!

本文为"Python与AI智能研习社"公众号原创文章,转载请注明出处。关注公众号,获取更多Python技术干货!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-03-27 11:41:40 HTTP/2.0 GET : https://f.mffb.com.cn/a/482690.html
  2. 运行时间 : 0.084443s [ 吞吐率:11.84req/s ] 内存消耗:4,855.32kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=0842c3fa6ed044602ed7098821bedfac
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000439s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000545s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000283s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.001365s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000519s ]
  6. SELECT * FROM `set` [ RunTime:0.000202s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000579s ]
  8. SELECT * FROM `article` WHERE `id` = 482690 LIMIT 1 [ RunTime:0.004024s ]
  9. UPDATE `article` SET `lasttime` = 1774582900 WHERE `id` = 482690 [ RunTime:0.004122s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000271s ]
  11. SELECT * FROM `article` WHERE `id` < 482690 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000526s ]
  12. SELECT * FROM `article` WHERE `id` > 482690 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000428s ]
  13. SELECT * FROM `article` WHERE `id` < 482690 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.003114s ]
  14. SELECT * FROM `article` WHERE `id` < 482690 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.000824s ]
  15. SELECT * FROM `article` WHERE `id` < 482690 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.001487s ]
0.085975s