Datetime Module In Python

Overview:

datetime module in Python provides classes and constants pertaining to date, time, timezone and time differences.

 

The classes in datetime module represent two types of objects. They are

  1. Naive objects
  2. Aware  objects

   

1. Naive objects:

  • Naive time objects contain a time quantity but do not tell which timezone the time quantity belongs to.
  • Naive objects for example can contain 10 AM, but in which time zone is not mentioned.
  • Naive objects do not even assume whether the time is UTC or not.
  • Nor they convey what is the timezone offset from UTC for the time mentioned.
  • It is up to the program that processes the native time objects to interpret a meaning for the time associated with Naive Objects.
  • One Program can process a naive object as CST whereas another program can process a naive object with the same time quantity as EST.
  • The date and the timedelta classes provide the naive objects in Python.

 

2. Aware Objects

  • The aware objects provided by the datetime module classes contain time quantity as well as the time zone information. 
  • The time zone information tells what is the time difference between UTC and the time represented.
  • The time zone information also contains the name of the time zone.
  • The classes datetime and time provide time zone aware objects in Python.

 

List of classes in datetime module:

Class Name

Description

Is Time Zone Aware?

date

  • Represents a time quantity without timezone, DST settings

Not time zone Aware. Produce naive objects.

datetime

  • Represent time with attributes for date and time along with time zone info.

Time zone aware.

time

  • It represents just time. No date.
  • Possess time attributes that represent hour, minute, second, microsecond and timezone info.
  • It just assumes there are exactly 86400 seconds in a day.
  • Does not understand the concept of leap seconds.

Time zone aware.

timedelta

Objects of this class are used to represent time difference between two time quantities.

Not time zone Aware. Produce naive objects.

tzinfo

Abstract base class for objects that represent a time zone.

Concrete classes of tzinfo provide time zone information.

 

timezone

  • A concrete class that provides an implementation of the abstract base class tzinfo.

 

  • timezone class possess attributes for name of the timezone and the UTC offset.
  • Represents a time zone.
  • Its typically associated with a time quantity like time and datetime.

 


Copyright 2023 © pythontic.com