How do you convert utc to time in python?

  1. HowTo
  2. Python How-To's
  3. Convert UTC to CST in Python

Created: October-10, 2021

  1. Use the dateutil.tz Module to Convert the Date and Time From UTC to CST in Python
  2. Use the pytz Module to Convert the Date and Time From UTC to CST in Python
  3. Use the zoneinfo Module to Convert the Date and Time From UTC to CST in Python

Python stores the date and time in a very sophisticated way. It has a data type specifically for storing this type of data. Moreover, Python also provides a datetime module that offers several classes to manipulate date and time.

The conversion into different time zones is an essential part of manipulating date and time. In this article, we will discuss two such time zones, namely UTC and CST, and the different ways available to convert the former to the latter.

The datetime module is a necessity and will need to be imported and used in all the methods mentioned in the article below.

Use the dateutil.tz Module to Convert the Date and Time From UTC to CST in Python

The dateutil module powers up the standard datetime module by providing some significant extensions. The dateutil.tz module supplies time zone implementations that sub-class the abstract datetime.tzinfo type.

Here, we will frequently use the tz.gettz() function, which essentially retrieves an object of the time zone from the given string passed as its argument.

The following code uses the dateutil.tz module to convert the date and time from UTC to CST in Python.

import datetime
from dateutil import tz

from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/Chicago')
json_data = {'time': "2021-10-08T08:17:42Z"} 
utc = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")
utc = utc.replace(tzinfo=from_zone)
cst = utc.astimezone(to_zone)
print(utc)
print(cst)

The above code provides the following output:

2021-10-08 08:17:42+00:00
2021-10-08 03:17:42-05:00

Use the pytz Module to Convert the Date and Time From UTC to CST in Python

The pytz module supports a vast majority of time zones, making it the perfect module to provide date and time conversion functionalities and provide functions that enable simple time zone calculations in the programmer’s python application software.

Moreover, the pytz module allows the creation of smart datetime objects or instances that are aware of their time zone. The pytz module needs to be manually installed and can be done by using the pip command.

The following code uses the pytz module to convert the date and time from UTC to CST in Python

from datetime import datetime, timezone
import pytz

fmt = '%Y-%m-%d %H:%M:%S %Z%z'
e = pytz.timezone('US/Central')

time_from_utc = datetime.fromtimestamp(1607020200, tz=timezone.utc)
time_from = time_from_utc.astimezone(e)
time_from.strftime(fmt)
time_to_utc = datetime.fromtimestamp(1609785000, tz=timezone.utc)
time_to = time_to_utc.astimezone(tz=pytz.timezone('US/Central'))
print(time_to_utc)
print(time_to)

The above code provides the following output:

2021-01-04 18:30:10+00:00
2021-01-04 12:30:10-06:00

Although both the dateutil.tz and the pytz modules are utilized to implement similar things in Python, they have some differences.

  • The pytz module supports almost all time zones, significantly more than what the dateutil.tz module offers.
  • The library of the pytz module is significantly more optimized.
  • The pytz module is historically faster than dateutil.tz, but the gap has been lessening over the last couple of updates.
  • The use of dateutil.tz module is recommended in the earlier versions of Python when the pytz module was not stable enough.

Use the zoneinfo Module to Convert the Date and Time From UTC to CST in Python

With the introduction of Python 3.9 came the zoneinfo module, which eliminated the need to install any other third-party libraries to manage time zones. The zoneinfo module deals with everything itself.

The zoneinfo module is utilized to provide a strong time zone implementation that, as specified in PEP 615, can support the IANA time zone database.

The following code uses the zoneinfo module to convert the date and time from UTC to CST in Python.

from datetime import datetime
from zoneinfo import ZoneInfo   

naive_datetime = datetime(2021, 10, 08, 12, 0, 0)
user_tz_preference = ZoneInfo('US/Central')

user_datetime = naive_datetime.replace(tzinfo=user_tz_preference)
user_datetime = datetime(2011, 10, 26, 12, tzinfo=ZoneInfo('US/Central'))
utc_datetime = user_datetime.astimezone(ZoneInfo('UTC'))
print(user_datetime.isoformat())
print(utc_datetime.isoformat())

The above code provides the following output:

# 2021-10-08T12:00:00-06:00
# 2021-10-08T19:00:00+00:00

Related Article - Python DateTime

  • Convert Pandas Column to Datetime
  • Get the Current Time in Python
  • Get the Day of the Week in Python
  • Convert String to Datetime in Python
  • How do you convert UTC to time?

    Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

    How do I calculate UTC time in Python?

    Get Current UTC Time.
    Use the datetime.now() method to get the current time..
    Use the timezone class with UTC instance with a now() method to to get the current UTC time in Python..

    How do I convert UTC to local time in pandas?

    To convert naive Timestamp to local time zone, use the timestamp..
    import pandas as pd. Creating a naive timestamp..
    timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624') Add the timezone..
    timestamp.tz_localize(tz='Australia/Brisbane') Example. Following is the code..

    How do you convert UTC to CST in Python?

    Convert UTC to CST in Python.
    Use the dateutil.tz Module to Convert the Date and Time From UTC to CST in Python..
    Use the pytz Module to Convert the Date and Time From UTC to CST in Python..
    Use the zoneinfo Module to Convert the Date and Time From UTC to CST in Python..