Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Các module Python thao tác date/time

Nhiều module trong standard library và PyPI cho date manipulation. Phổ biến nhất:

  • datetime (stdlib): Class manipulate date/time đơn giản và phức tạp. Focus: efficient attribute extraction, formatting.
  • time (stdlib): Time-related functions. Gọi platform C library. Không phải platform nào cũng có hết function.
  • dateutil: Mở rộng mạnh cho datetime (Python 2.3+). Parser linh hoạt, relative delta, recurrence rule.
  • mxDateTime: Extension package cung cấp DateTime, DateTimeDelta, RelativeDateTime - tự nhiên hơn ticks (seconds since epoch).
  • pytz: Olson tz database cho Python. Timezone calculation chính xác, cross-platform (Python 2.4+). Xử lý ambiguous time ở DST transition.

Khuyến nghị

  • Cơ bản: datetime + zoneinfo (Python 3.9+)
  • Timezone legacy: pytz
  • Parse tự do: dateutil.parser
  • Business logic phức tạp: dateutil.relativedelta