Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is the difference between naive and aware?

Educative Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The datetime class

The datetime class initiates the datetime objects that contain information about the date, time, and time zone.

class datetime.datetime(year, month, day, hour, minute, 
second, microsecond, tzinfo, fold)

Note: The first three arguments, year, month, and day arguments are mandatory, while all the other arguments are optional.

# import datetime class from datetime module
from datetime import datetime
# Initializing with year, month and date
date_obj = datetime(1998, 12, 27)
print("Date: ", date_obj)
# Initializing with time
datetime_obj = datetime(1998, 12, 27, 13, 22, 2, 398740)
print("Date with Time: ",datetime_obj)
# to get current datetime use now()
current_time = datetime.now()
# Print Current time
print("Current Time: " ,current_time)
Initialization of the "datetime" class

Types of datetime class

The datetime class is classified into two categories:

  1. Naive
  2. Aware

If a datetime object has time zone information, then it will be aware. Otherwise, it will be naive. In the case of a naive datetime object, tzinfo will be None and time will be in UTC(+00:00).

Naive versus aware "datetime" format

Example

# import date time class
from datetime import datetime
# import pytz for timezone
import pytz
# Naive time
N = datetime.now()
print("----Naive----")
print("UTC time:", N)
# time zone of Karachi
tz_Karachi = pytz.timezone('Asia/Karachi')
datetime_Karachi = datetime.now(tz_Karachi)
print("----Aware----")
print("Karachi time:", datetime_Karachi)
# time zone of NewYork
tz_NewYork = pytz.timezone('America/New_York')
datetime_NewYork= datetime.now(tz_NewYork)
print("NewYork time:", datetime_NewYork)
Naive versus aware overview

Explanation

In line 7, datetime.now() does not have any time zone information. It just returns the current system time in UTC (+00:00).

To display time according to the right timezone, the pytz module must be used.

To use the pytz module, “Asia/Karachi” and “America/New_York” in line 12 and 17, respectively, are taken as arguments of the pythz.timezone() function. This function is then called as an argument inside datetime.now().

Hence, lines 13 and 18 return the local time with respect to the local timezone.

RELATED TAGS

python
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring