周志洋

个人站

持续学习 才能不被淘汰


Python实用技巧-日期与时间处理详解

日期时间处理的重要性

日期和时间是编程中经常需要处理的数据类型。Python的datetime模块提供了强大的日期时间处理功能,掌握这些技能对于开发实际应用非常重要。

datetime模块基础

1. 基本日期时间对象

from datetime import datetime, date, time, timedelta
import time as time_module

def datetime_basics():
    """datetime基础操作"""
    
    # 获取当前时间
    now = datetime.now()
    print(f"当前时间: {now}")
    print(f"当前日期: {now.date()}")
    print(f"当前时间: {now.time()}")
    
    # 创建特定日期时间
    specific_date = datetime(2018, 10, 2, 14, 30, 45)
    print(f"特定时间: {specific_date}")
    
    # 从时间戳创建
    timestamp = time_module.time()
    from_timestamp = datetime.fromtimestamp(timestamp)
    print(f"从时间戳创建: {from_timestamp}")
    
    # 获取时间戳
    timestamp_value = now.timestamp()
    print(f"时间戳: {timestamp_value}")
    
    # 访问各个组件
    print(f"年: {now.year}")
    print(f"月: {now.month}")
    print(f"日: {now.day}")
    print(f"时: {now.hour}")
    print(f"分: {now.minute}")
    print(f"秒: {now.second}")
    print(f"微秒: {now.microsecond}")
    print(f"星期几: {now.weekday()}")  # 0=周一, 6=周日
    print(f"星期几: {now.isoweekday()}")  # 1=周一, 7=周日

datetime_basics()

2. 日期时间格式化

def datetime_formatting():
    """日期时间格式化"""
    
    now = datetime.now()
    
    # 常用格式化字符串
    formats = {
        "ISO格式": "%Y-%m-%d %H:%M:%S",
        "中文格式": "%Y年%m月%d日 %H时%M分%S秒",
        "美式格式": "%m/%d/%Y %I:%M:%S %p",
        "欧式格式": "%d/%m/%Y %H:%M:%S",
        "文件名格式": "%Y%m%d_%H%M%S",
        "日期格式": "%Y-%m-%d",
        "时间格式": "%H:%M:%S"
    }
    
    print("各种格式化示例:")
    for name, format_str in formats.items():
        formatted = now.strftime(format_str)
        print(f"{name}: {formatted}")
    
    # 解析字符串为日期时间
    date_strings = [
        "2018-10-02 14:30:45",
        "02/10/2018 2:30:45 PM",
        "2018年10月02日 14时30分45秒"
    ]
    
    print("\n解析字符串示例:")
    for date_str in date_strings:
        try:
            if "年" in date_str:
                # 中文格式需要特殊处理
                dt = datetime.strptime(date_str, "%Y年%m月%d日 %H时%M分%S秒")
            elif "/" in date_str and "PM" in date_str:
                dt = datetime.strptime(date_str, "%d/%m/%Y %I:%M:%S %p")
            else:
                dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
            print(f"'{date_str}' -> {dt}")
        except ValueError as e:
            print(f"解析失败: {date_str} - {e}")

datetime_formatting()

时间计算和操作

1. 时间差计算

def time_calculations():
    """时间计算示例"""
    
    # 创建时间差对象
    delta1 = timedelta(days=7, hours=3, minutes=30, seconds=45)
    delta2 = timedelta(weeks=2, days=1)
    
    print(f"时间差1: {delta1}")
    print(f"时间差2: {delta2}")
    print(f"总天数: {delta1.days}")
    print(f"总秒数: {delta1.total_seconds()}")
    
    # 时间加减
    now = datetime.now()
    future = now + timedelta(days=30)
    past = now - timedelta(hours=24)
    
    print(f"现在: {now}")
    print(f"30天后: {future}")
    print(f"24小时前: {past}")
    
    # 计算两个日期之间的差
    date1 = datetime(2018, 10, 1)
    date2 = datetime(2018, 10, 15)
    difference = date2 - date1
    print(f"日期差: {difference.days} 天")
    
    # 工作日计算(简单版本)
    def count_weekdays(start_date, end_date):
        """计算工作日数量"""
        current = start_date
        weekdays = 0
        while current <= end_date:
            if current.weekday() < 5:  # 周一到周五
                weekdays += 1
            current += timedelta(days=1)
        return weekdays
    
    start = datetime(2018, 10, 1)
    end = datetime(2018, 10, 15)
    weekdays = count_weekdays(start, end)
    print(f"工作日数量: {weekdays}")

time_calculations()

2. 时间比较和排序

def time_comparison():
    """时间比较示例"""
    
    # 创建多个时间点
    times = [
        datetime(2018, 10, 2, 9, 0, 0),
        datetime(2018, 10, 2, 14, 30, 0),
        datetime(2018, 10, 2, 8, 45, 0),
        datetime(2018, 10, 2, 16, 15, 0)
    ]
    
    print("原始时间列表:")
    for i, t in enumerate(times):
        print(f"{i+1}. {t}")
    
    # 排序
    sorted_times = sorted(times)
    print("\n排序后的时间:")
    for i, t in enumerate(sorted_times):
        print(f"{i+1}. {t}")
    
    # 找到最早和最晚时间
    earliest = min(times)
    latest = max(times)
    print(f"\n最早时间: {earliest}")
    print(f"最晚时间: {latest}")
    
    # 时间比较
    time1 = datetime(2018, 10, 2, 9, 0, 0)
    time2 = datetime(2018, 10, 2, 14, 30, 0)
    
    print(f"\n时间比较:")
    print(f"{time1} < {time2}: {time1 < time2}")
    print(f"{time1} == {time2}: {time1 == time2}")
    print(f"{time1} > {time2}: {time1 > time2}")

time_comparison()

时区处理

1. 时区基础

from datetime import timezone, timedelta

def timezone_basics():
    """时区处理基础"""
    
    # UTC时间
    utc_now = datetime.now(timezone.utc)
    print(f"UTC时间: {utc_now}")
    print(f"UTC时间戳: {utc_now.timestamp()}")
    
    # 创建特定时区
    beijing_tz = timezone(timedelta(hours=8))
    tokyo_tz = timezone(timedelta(hours=9))
    new_york_tz = timezone(timedelta(hours=-5))
    
    # 转换到不同时区
    beijing_time = utc_now.astimezone(beijing_tz)
    tokyo_time = utc_now.astimezone(tokyo_tz)
    new_york_time = utc_now.astimezone(new_york_tz)
    
    print(f"北京时间: {beijing_time}")
    print(f"东京时间: {tokyo_time}")
    print(f"纽约时间: {new_york_time}")
    
    # 时区信息
    print(f"北京时区偏移: {beijing_tz.utcoffset(utc_now)}")
    print(f"东京时区偏移: {tokyo_tz.utcoffset(utc_now)}")
    print(f"纽约时区偏移: {new_york_tz.utcoffset(utc_now)}")

timezone_basics()

2. 时区转换工具

def timezone_conversion_tool():
    """时区转换工具"""
    
    class TimezoneConverter:
        """时区转换器"""
        
        def __init__(self):
            self.timezones = {
                'UTC': timezone.utc,
                'Beijing': timezone(timedelta(hours=8)),
                'Tokyo': timezone(timedelta(hours=9)),
                'New_York': timezone(timedelta(hours=-5)),
                'London': timezone(timedelta(hours=0)),
                'Sydney': timezone(timedelta(hours=10))
            }
        
        def convert_time(self, dt, from_tz, to_tz):
            """转换时间到不同时区"""
            if from_tz in self.timezones and to_tz in self.timezones:
                # 设置源时区
                source_dt = dt.replace(tzinfo=self.timezones[from_tz])
                # 转换到目标时区
                target_dt = source_dt.astimezone(self.timezones[to_tz])
                return target_dt
            else:
                raise ValueError(f"不支持的时区: {from_tz}{to_tz}")
        
        def get_world_time(self, dt):
            """获取世界时间"""
            world_times = {}
            for name, tz in self.timezones.items():
                world_times[name] = dt.astimezone(tz)
            return world_times
    
    # 使用时区转换器
    converter = TimezoneConverter()
    base_time = datetime(2018, 10, 2, 12, 0, 0)
    
    print("时区转换示例:")
    try:
        # 从北京时间转换到纽约时间
        ny_time = converter.convert_time(base_time, 'Beijing', 'New_York')
        print(f"北京时间 {base_time} -> 纽约时间 {ny_time}")
        
        # 获取世界时间
        world_times = converter.get_world_time(base_time)
        print("\n世界时间:")
        for city, time in world_times.items():
            print(f"{city}: {time}")
            
    except ValueError as e:
        print(f"错误: {e}")

timezone_conversion_tool()

实际应用案例

1. 日志时间戳处理

def log_timestamp_processing():
    """日志时间戳处理示例"""
    
    class LogProcessor:
        """日志处理器"""
        
        def __init__(self):
            self.logs = []
        
        def add_log(self, message, level="INFO"):
            """添加日志"""
            timestamp = datetime.now()
            log_entry = {
                'timestamp': timestamp,
                'level': level,
                'message': message
            }
            self.logs.append(log_entry)
        
        def get_logs_by_time_range(self, start_time, end_time):
            """获取时间范围内的日志"""
            return [log for log in self.logs 
                   if start_time <= log['timestamp'] <= end_time]
        
        def get_logs_by_level(self, level):
            """获取特定级别的日志"""
            return [log for log in self.logs if log['level'] == level]
        
        def format_logs(self, format_str="%Y-%m-%d %H:%M:%S"):
            """格式化日志输出"""
            formatted_logs = []
            for log in self.logs:
                formatted_time = log['timestamp'].strftime(format_str)
                formatted_logs.append(f"[{formatted_time}] {log['level']}: {log['message']}")
            return formatted_logs
    
    # 使用日志处理器
    processor = LogProcessor()
    
    # 添加一些日志
    processor.add_log("系统启动", "INFO")
    processor.add_log("用户登录", "INFO")
    processor.add_log("数据库连接失败", "ERROR")
    processor.add_log("用户退出", "INFO")
    
    # 获取所有日志
    all_logs = processor.format_logs()
    print("所有日志:")
    for log in all_logs:
        print(log)
    
    # 获取错误日志
    error_logs = processor.get_logs_by_level("ERROR")
    print(f"\n错误日志数量: {len(error_logs)}")
    
    # 获取特定时间范围的日志
    start_time = datetime.now() - timedelta(minutes=1)
    end_time = datetime.now()
    recent_logs = processor.get_logs_by_time_range(start_time, end_time)
    print(f"最近1分钟的日志数量: {len(recent_logs)}")

log_timestamp_processing()

2. 定时任务调度器

def task_scheduler():
    """定时任务调度器示例"""
    
    class TaskScheduler:
        """任务调度器"""
        
        def __init__(self):
            self.tasks = []
        
        def add_task(self, name, func, run_time):
            """添加任务"""
            task = {
                'name': name,
                'func': func,
                'run_time': run_time,
                'last_run': None,
                'next_run': run_time
            }
            self.tasks.append(task)
        
        def get_due_tasks(self, current_time):
            """获取到期任务"""
            due_tasks = []
            for task in self.tasks:
                if task['next_run'] <= current_time:
                    due_tasks.append(task)
            return due_tasks
        
        def run_task(self, task):
            """运行任务"""
            print(f"运行任务: {task['name']}")
            try:
                task['func']()
                task['last_run'] = datetime.now()
                # 计算下次运行时间(示例:每天运行)
                task['next_run'] = task['last_run'] + timedelta(days=1)
                print(f"任务 {task['name']} 执行成功")
            except Exception as e:
                print(f"任务 {task['name']} 执行失败: {e}")
        
        def schedule_tasks(self, current_time):
            """调度任务"""
            due_tasks = self.get_due_tasks(current_time)
            for task in due_tasks:
                self.run_task(task)
    
    # 使用任务调度器
    scheduler = TaskScheduler()
    
    # 定义一些任务
    def backup_database():
        print("执行数据库备份...")
    
    def send_email_report():
        print("发送邮件报告...")
    
    def cleanup_temp_files():
        print("清理临时文件...")
    
    # 添加任务
    now = datetime.now()
    scheduler.add_task("数据库备份", backup_database, now + timedelta(seconds=5))
    scheduler.add_task("邮件报告", send_email_report, now + timedelta(seconds=10))
    scheduler.add_task("清理文件", cleanup_temp_files, now + timedelta(seconds=15))
    
    print("任务调度器示例:")
    print("注意:这是一个演示,实际应用中需要循环检查")
    
    # 模拟调度
    for i in range(3):
        current_time = now + timedelta(seconds=i*10)
        print(f"\n检查时间: {current_time}")
        scheduler.schedule_tasks(current_time)

task_scheduler()

总结

掌握Python日期时间处理是开发实际应用的关键:

  1. 基础操作:理解datetime、date、time对象的基本用法
  2. 格式化:掌握strftime和strptime的使用
  3. 时间计算:使用timedelta进行时间加减运算
  4. 时区处理:理解时区概念和转换方法
  5. 实际应用:在日志处理、任务调度等场景中应用
  6. 最佳实践:遵循日期时间处理的最佳实践

通过系统学习这些概念,你将能够处理各种日期时间相关的编程任务,提高代码的实用性和可靠性。


转载请注明:周志洋的博客 » Python实用技巧-日期与时间基础

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦