Loading...
Loading...
Every pattern you'd Google - datetime, formatting, math, and more
Python's datetime module contains a small set of classes organized in a clear inheritance tree. The key insight is that datetime is a subclass of date, so every datetime object is also a date. The abstract tzinfo base class provides the timezone interface, and its concrete subclass timezone handles fixed UTC offsets:
object
├── date
│ │ .year .month .day
│ │ .today() .fromtimestamp() .fromisoformat()
│ │ .weekday() .isoformat() .strftime()
│ │
│ └── datetime (inherits from date)
│ │ .hour .minute .second .microsecond .tzinfo
│ │ .now() .utcnow() .combine() .fromisoformat()
│ │ .timestamp() .astimezone() .replace()
│ │
│ │ ┌──────────────────────────────────┐
│ │ │ Naive: dt.tzinfo is None │
│ │ │ Aware: dt.tzinfo is not None │
│ │ │ dt.utcoffset() != None │
│ │ └──────────────────────────────────┘
│ │
│ └── (no further subclasses in stdlib)
│
├── time
│ .hour .minute .second .microsecond .tzinfo
│ .fromisoformat() .isoformat() .strftime()
│ (represents a time-of-day, independent of any date)
│
├── timedelta
│ .days .seconds .microseconds
│ .total_seconds()
│ (represents a duration, the result of date/datetime subtraction)
│ Max: 999999999 days, 23:59:59.999999
│ Min: -999999999 days
│
└── tzinfo (abstract base class)
│ .utcoffset(dt) .tzname(dt) .dst(dt)
│
└── timezone (concrete, fixed-offset only)
timezone.utc = timezone(timedelta(0))
timezone(timedelta(hours=-5)) = ESTdatetime inherits from date, you can pass a datetime wherever a date is expected. However, time and timedelta are independent classes. They do not share an inheritance relationship with date or datetime.