Naive versus Aware Datetime Objects in Python
The Difference between Naive versus Aware Datetime Objects is on time zone information: naive datetime objects does not have information on the time zone, whereas timezone-Aware datetime objects associate with timezone information
Example of Native Datetime Objects
By default, datetime.now()
does not return information about the time zones. The following shows the examples of native datetime.
# the default output, which tz=none
date_time_default = datetime.datetime.now()
print("date_time_default :",date_time_default)
# specify tz=none
date_time_TZ_None = datetime.datetime.now(tz=None)
print("date_time_TZ_None :",date_time_TZ_None)
The following is the output, which has two, same native datetime.
date_time_default : 2022-04-16 12:12:25.205689 date_time_TZ_None : 2022-04-16 12:12:25.205689
Example of Aware Datetime Objects
You can add tz=datetime.timezone.utc to print out time zone information. Thus, it is an aware datetime object.
# specify tz=datetime.timezone.utc
date_time_TZ_UTC = datetime.datetime.now(tz=datetime.timezone.utc)
print("date_time_TZ_UTC :",date_time_TZ_UTC)
The following is the ouput.
date_time_TZ_UTC : 2022-04-16 16:12:25.205689+00:00