Converting UTC to EST in Python

Convert UTC To EST

In this article, let us try to understand how to convert UTC – Coordinated Universal Time to EST – Eastern Standard Time using different Python programming language modules. We will also find a solution on how to take care of c. Daylight Saving Time is commonly known as ‘Summer Time’, this system consistently advances clocks to prolong daylight hours during regular waking hours during the summer.

The clocks are generally moved forward one hour in late March or early April and backward one hour in late September or early October in nations in the Northern Hemisphere.

Converting UTC to EST in Python can be done using the datetime and pytz modules. Import both modules, create a function that takes a datetime object as input, and use the astimezone() method to convert it. The pytz module accounts for daylight saving time, making the conversion accurate.

Python Modules for UTC to EST Conversion

1. Datetime module: Python includes a DateTime module that allows you to interact with dates and timings. Instead of being a basic data type in Python, DateTime is an integrated module. To interact with dates as date objects, we simply import the module specified above. This class’s attributes are comparable to those of the date and distinct classes. Day, month, year, minute, second, microsecond, hour, and tzinfo are some of these attributes. To learn more about the datetime module, click here.

2. Pytz module: Using Python 2.4 or above, this module enables precise and cross-platform timezone conversions. It also eliminates the confusion caused by daylight saving time. The Olson timezone database is brought into Python with the help of the pytz python module.

Implementation of UTC to EST Conversion in Python

Before we begin with creating the solution function make sure to install and import both the above-mentioned python modules in your current working python IDE. To do so run the following lines of command

#installing pytz module
pip install pytz

#importing both the modules
import datetime
import pytz

Next, we create a function that takes a datetime object as the input parameter. To mention the necessary information like the timezone of the datetime object, we add tzinfo=pytz.utc to the datetime object. If the input parameter is obtained using the datetime.datetime.now(), then we pass tz=pytz.utc along with it.

To retrieve the timezone details for the specified timezone code, we utilize the pytz.timezome() method of the pytz module in the function.

  • Based on the season, this approach also automatically adjusts for daylight saving time.
  • As Python supports Daylight Saving Time (DST) better than EST, it is typically advised to use US/Eastern instead of EST as the timezone identifier.
  • Last but not least, a timezone-aware datetime object can be transformed from one timezone to another using the astimezone() method. We convert the object from UTC to EST before returning the output of the function.

Below is the code of the above-described function

import datetime
import pytz

time = datetime.datetime(2020, 10, 8, 7, 6, 0, tzinfo=pytz.utc)

def convert_utc_to_est(utc_time):
  #fetch the timezone information
  est = pytz.timezone('US/Eastern')

  #convert utc to est
  est_time = utc_time.astimezone(est)

  #return the converted value
  return est_time


est_time = convert_utc_to_est(time)

#print in year-month-day-hour-minutes-secods format
print('UTC time: ', time.strftime('%Y-%m-%d %H:%M:%S'))
print('EST time: ', est_time.strftime('%Y-%m-%d %H:%M:%S'))

OUTPUT

UTC To EST 1

In the above code, a specified datetime object is passed as a parameter to the function. Let us also try to pass the datetime object using the datetime.datetime.now() method. The function and entire procedure remain the same. The only difference is that we add additional tz=pytz.utc instead of tzinfo=pytz.utc. Below is the output of the same

UTC To EST 2

Conclusion

Using Python’s datetime and pytz modules, we can easily convert UTC time to EST time while also accounting for daylight saving time. Python’s versatility and built-in modules make working with dates and times efficient and more streamlined.

To read more such detailed and easy-to-understand articles on various topics related to the python programming language, please click here!

References

Official Document of pytz module