In this article, we will be discussing Unix time and how to convert Unix time to datetime in Python. Before we review the Python implementations, let’s discuss what the concept of Unix time is.
In the field of computing, Unix time is a digital timestamp. Unix time represents the number of seconds passed since the Unix epoch without including leap seconds.
What is the Unix epoch? This epoch is nothing but a date and time set by Unix engineers to establish the start of Unix time. The date is New Year’s Day, 1970, at exactly 00:00:00 UTC. Therefore, the total number of seconds since January 1st, 1970, 00:00:00 UTC, is the current Unix time.
What is the Unix Time Format?
A single signed number represents Unix time which increases each second.
As of writing this article, the UNIX time is: 1661324766
This means 1,661,324,766 seconds have passed since Unix Epoch.
What is Datetime Module, and What are Datetime Objects?
Python datetime module is a part of the Python standard library under the “datatypes” category. The DateModule provides multiple classes to handle dates and times in different formats.
A Datetime object is an instance created within the Python class. datetime
.datetime
. In the datetime module, there are 2 types of objects
- Naive Objects – Doesn’t contain timezone information
- Aware Objects – A localized datetime object based on a timezone.
How to Convert Unix Time to a Python Datetime Object?
As we’ve discussed, we use the Datetime module to handle dates and times. With the help of the fromtimestamp()
function, we can convert Unix time to a datetime object in Python. Within the function, pass Unix time as an argument.
Let’s look at the following implementation:
import datetime
unixToDatetime = datetime.datetime.fromtimestamp(1661324766) # Unix Time
print(unixToDatetime)
Output
2022-08-24 07:06:06
How to Convert Datetime Object to Unix time in Python
Using the timestamp() function from the Datetime module, we can convert the Datetime object to Unix time. The function will return a float value representing the Unix time.
Let’s use the datetime object (unixToDatetime
) created in the previous demonstration
datetimeToUnix = unixToDatetime.timestamp()
print(datetimeToUnix)
1661324766.0
How To Convert Unix Time To Datetime in Pandas Dataframes
Let’s say a data frame consists of a column containing Unix time values. How do we convert the values of this column into standard date and time?
Using the to_datetime()
function from the Datetime module, we can solve our problem.
Here’s our sample data frame(df):
date price 0 1349720105 2300.23 1 1349806505 8776.11 2 1349892905 9009.33 3 1350065705 2223.22
Let’s see how the function is implemented in this scenario:
import pandas as pd
from datetime import datetime
df
df['date'] = pd.to_datetime(df['date'],unit='s') # Changing the values of the column
Output
date price 0 2012-10-08 18:15:05 2300.23 1 2012-10-09 18:15:05 8776.11 2 2012-10-10 18:15:05 9009.33 3 2012-10-12 18:15:05 2223.22
Observe that all values in the “Date” column are now Datetime objects.
How To Convert a Unix timestamp in Nanoseconds to Date and Time
By converting the Unix time to a datetime object, we can present it in a human-readable format. Let’s look at the following implementation
from datetime import datetime
# flooring the value by 1 billion
readableDt = datetime.fromtimestamp( 1220287113082844499 // 1000000000)
readableDt
Output
datetime.datetime(2008, 9, 1, 16, 38, 33)
Tip: To format the result in YYYY/MM/DD H: M: S, use the .strftime()
method
How To Convert Datetime Object To a Specific Timezone
Time Zone conversion is an essential feature that this module provides. Using the .astimezone
function, we can achieve such requirements.
The following program converts from UTC to America/Los Angeles.
from datetime import datetime, timezone, ZoneInfo
dt_UTC = datetime.now(timezone.utc)
dt_LA = dt_UTC.astimezone(tz=datetime.ZoneInfo("America/Los_Angeles"))
print(dt_LA)
Output
2022-08-25 19:53:40.100123
FAQs on Python Convert UNIX time to datetime
You can display Unix time as normal time by converting it to a Datetime object. Use the fromtimestamp()
function.
Unix time is represented as a single signed value that denotes the number of seconds since the Unix Epoch.
First, convert the Unix timestamp to a Datetime object using fromtimestamp()
. Then, convert the default timezone of the Datetime object to UTC using astimezone()
.
Conclusion
In this article, we have discussed the concept of UNIX time and its origin. Unix time is the number of seconds since the Unix Epoch. The Unix Epoch is a date set by UNIX engineers to represent the start of Unix time. Unix time is represented within a single signed value.
We have discussed techniques to convert Unix times to Datetime objects and vice-versa. We have also discussed techniques to convert Unix time values in Pandas data frames.