Handling DateTime Data with Python
One of the most difficult aspects of computer programming is dealing with datetime data sets. Not only must you keep track of the date, but you must also learn how to represent dates and times in each language, create indices from those data points, and ensure that all of your data sets handle daylight saving time consistently. Fortunately, this datetime data primer will get you started in Python.
DateTime Object in Python Defined
Most date and time representations in Python are datetime objects created by the Datetime package. This means that understanding the Datetime package and how to use it is essential!
A datetime object is essentially a variable that contains information about (surprise, surprise) a date and time. It can also include time zone information, and tools for changing time zones are available.
Let’s look at some datetime objects created with the Datetime package. To begin, we can use the package’s command to create a variable that stores the current time, as shown below:
import datetime
import pytz
now = datetime.datetime.now(pytz.timezone('US/Eastern'))
print(now)
print(now.tzinfo)
The first two lines of code import the necessary packages for this task. The first package is Datetime, which allows us to create and manipulate datetime objects. The Pytz package, which provides time zone information, is the second.
The datetime is referred to in the third line.
We use the datetime.now function to generate a datetime object that represents the time we run the code. This line also appends a time zone to the datetime, indicating that it represents a time in the United States Pacific time zone.
The fourth and fifth lines are both printouts used to demonstrate the code’s output.
The following are the results of that code:
2022-07-11 08:56:32.704353-04:00
US/Eastern
The first output now displays the variable’s complete information. The variable was created on July 11, 2022, at 8:56 a.m. and 32.70 seconds. Because I set the time zone to ‘US/Eastern,’ the program adds -4 hours (in comparison to UTC, Coordinate Universal Time) to the variable. The second output confirms the time zone information by printing that the variable is in the Eastern time zone of the United States.
You’ll notice that I set the time zone to the US Eastern time zone in the preceding code by calling pytz.timezone(‘US/Eastern’). You must know the correct code to use a different time zone (though they all follow the same format and reference known time zones, so they’re fairly predictable). If you want to find your time zone, use the following command to print a list of all options.
print(pytz.all_timezones)
To create a datetime at a specific date, we can also use the datetime.datetime function. Take note of the format of the previous example’s datetime object displaying the current time, as the inputs to datetime.datetime are provided in the same order (year, month, day, hour, minute, seconds). In other words, we can use the following code to create a datetime object representing July 11, 2022 at 8:10:03 in the United States Eastern time zone:
specified_datetime = datetime.datetime(2022, 7, 11, 8, 10, 3).astimezone(pytz.timezone('US/Eastern'))
print(specified_datetime)
print(specified_datetime.tzinfo)
Take note of how the datetime.datetime inputs appear above. The.astimezone method is then used to set the time zone to the Eastern time zone of the United States.
The following are the results of that code:
2022-07-11 08:10:03-04:00
US/Eastern
It turns out that the code generated the variable we were looking for. As expected, specified datetime now returns July 11, 2022 at 8:10:03 in the Eastern time zone of the United States.
What if we want to represent the same time in another time zone? We could go through the effort of calculating that new time zone and creating a new datetime object, but that would necessitate knowing the time difference, doing the math, and creating the new object. Another possibility is to convert the time zone to the desired time zone and save the result in a new variable. So, if we want to convert specified datetime to the Pacific time zone in the United States, we can use the code below.
pacific_datetime = specified_datetime.astimezone(pytz.timezone('US/Pacific'))
print(pacific_datetime)
print(pacific_datetime.tzinfo)
This code calls the specified datetime.astimezone() method, which returns a new time zone object representing the Pacific time zone in the United States. The printed outputs are as follows:
2022-07-11 05:10:03-07:00
US/Pacific
Take note of the differences between specified datetime and atlantic datetime. Because the United States Pacific time zone is three hours behind the United States Eastern time zone, the hour has been changed from 8 to 5. The time zone information has been changed from -4 to -7 because US Pacific time is now seven hours different from UTC rather than four hours. Finally, the printed time zone information is now US/Pacific rather than US/Eastern.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Check out our Community Discord and join our Talent Collective.