FromTimeStamp Python Function with Examples

Python is known for its useful functions, and one of them is fromtimestamp(). Now, this tutorial is all about discovering and understanding how the Python fromtimestamp function works with examples.

What is fromtimestamp() in Python?

In Python, the fromtimestamp() function comes from the DateTime module. It is used to return the local date based on the POSIX (Portable Operating System Interface of Unix) timestamp. This timestamp ranges from 1970 to 2038 and ignores leap seconds (if there are any in the present timestamp).

Furthermore, the fromtimestamp() function may throw an OverflowError if the timestamp is beyond the range of values recognized by the platform C localtime() function, or an OSError if localtime() fails.

However, if the timestamp is outside the range of values supported by the localtime() method on platform C, you can raise OverflowError rather than ValueError. But if localtime() fails, throw OSError instead of ValueError.

Also read: Chr Python | What is chr() in Python

Example program to Convert timestamp to Datetime Object

The first task that you’re about to perform is to convert the timestamp to the DateTime object. The example below will guide you on how each line of the program works.

Example Program

from datetime import datetime
 
datetime_obj = datetime(2022,9,8,7,6,5)
print(datetime_obj)
timestamp = datetime_obj.timestamp() 
print(timestamp)
 
datetime_obj2 = datetime.fromtimestamp(timestamp)
print("From TimeStamp to DateTime Object", datetime_obj2)

To invoke the DateTime module, we need to import the DateTime function from the DateTime class.

You may acquire knowledge on impr=orting modules and classes through Python Import Class From Another File with Examples. This is a full tutorial on Python import classes and modules.

Output:

2022-09-08 07:06:05
1662620765.0
From TimeStamp to DateTime Object 2022-09-08 07:06:05

Example program to Convert timestamp to Date Object

This other example shows the same procedure as the above discussion. However, this will convert the timestamp into a data object.

Example Program

from datetime import datetime
 
datetime_obj = datetime(2022,11,12,13,14,15)
print("Objects:", datetime_obj)
timestamp = datetime_obj.timestamp()
print("TimeStamp:", timestamp)
 
date_obj = datetime.fromtimestamp(timestamp)
print("Date and Time:",date_obj)

To invoke the DateTime module, we need to import the DateTime function from the DateTime class.

Output:

Objects: 2022-09-05 04:03:02
TimeStamp: 1662350582.0
Date and Time: 2022-09-05 04:03:02

The line datetime_obj = datetime() creates the timestamp which returns the output 2022-09-05 04:03:02. This output is based on the declared objects at the datetime() function.

To know the timestamp, the program used the timestamp() function and displays the output of 1662350582.0. This timestamp is also based on the given objects.

Finally, the line date_obj = datetime.fromtimestamp(timestamp) enables the program to convert the timestamp() into an actual datetime object.

Python fromtimestamp() for UTC example program

The next example will show you how to use the Python fromtimestamp() function to convert a timestamp into a date object. This example will base the conversion on actual time or UTC (Universal Time Coordinated).

Example Program

from datetime import datetime,timezone
 
# Getting the current date
# and time
datetime = datetime.now(timezone.utc)
print("Present Date and Time:", datetime)
 
# Converting datetime object 
utc_timestamp = datetime.timestamp()
print("Timestamp:", utc_timestamp)

#Converting timestamp to datetime object
datetime_obj2 = datetime.fromtimestamp(utc_timestamp)
print("Converted DateTime:", datetime_obj2)

The line containing from datetime import datetime,timezone enables the program to secure that we get the exact present date and time.

Output:

Present Date and Time: 2022-09-05 08:54:30.670722+00:00
Timestamp: 1662368070.670722
Converted DateTime: 2022-09-05 08:54:30.670722

As seen in the output, the present date and time as of trying this fromtimestamp() function is 2022-09-05 08:54:30.670722+00:00. This date and time are based on UTC.

utc_timestamp = datetime.timestamp() converts the data and time into timestamp using the timestamp() function. It displays the output timestamp 1662368070.670722 based on the given date and time.

Now, to convert the timestamp to the actual date and time, the program includes the Python fromtimestamp() function. This function invokes the program to display the DateTime: 2022-09-05 08:54:30.670722.

fromtimestamp() Set Timezone example program

In the example below, you will know how to set the time zone using Python fromtimestamp.

Example Program

#importing datetime class from datetime module
from datetime import datetime
import pytz
#Creating a timestamp

ACST = pytz.timezone('Asia/Manila')
datetime_obj = datetime(2001,1,2,3,4,5, tzinfo=ACST)
 
print(datetime_obj)
timestamp = datetime_obj.timestamp()     # Converting date into timestamp
print(timestamp)

# Converting timestamp to date
 
datetime_obj2 = datetime.fromtimestamp(timestamp,tz=ACST)
print("Timestamp from DateTime",datetime_obj2)

To initiate the program, the datatime module is called from the datatime class. The pytz module is also imported to enable the next line of code to execute the conversion of datetime into timezone.

 Output:

2001-01-02 03:04:05-15:56
978462005.0
Timestamp from DateTime 2001-01-03 03:00:05+08:00

The line ACST = pytz.timezone('Asia/Manila') creates the datetime object based on Asia/Manila Timezone.

According to the Asia/Manila Timezone, the datetime is 2001-01-02 03:04:05-15:56.

With the same procedure in the above examples, the program converts the timezone to a timestamp through timestamp = datetime_obj.timestamp(). This line displays the converted timestamp 978462005.0.

Python datetime fromtimestamp() milliseconds example program

The next example will show how to convert the given milliseconds into an actual Datetime using the Python fromtimestamp() function.

Example Program


from datetime import datetime
 
milliseconds = 1670241221000
print(datetime.fromtimestamp(milliseconds/1000))

The milliseconds in this example are equal to 1670241221000. Thus, the line datetime.fromtimestamp(milliseconds/1000) enables the program to execute the conversion of milliseconds datetime format.

Output:

2022-12-05 11:53:41

Upon running the line of codes above, the program displays the converted milliseconds to datetime which is 2022-12-05 11:53:41.

fromtimestamp() timezone example program

The example program below can be your guide on how to set timezone as your basis for testing the Python fromtimestamp() function.

You may use other timezones on GitHub because they provide the list of exact timezones available for the programs and applications.

Example Program

from datetime import datetime
import pytz

d = datetime.now()
timezone = pytz.timezone("America/Los_Angeles")

d_aware = timezone.localize(d)
d_aware.tzinfo

# One Liner
timezone.localize(datetime.now())

# Timestamp to Datetime with timezone
datetime.fromtimestamp(3456789, timezone)

The timezone in this example is from pytz-time-zones.py.

How does datetime work in Python?

The Datetime module in Python provides classes for date and time manipulation. These classes provide several methods for working with dates, times, and time intervals. In Python, date and datetime are objects; thus, when you manipulate them, you are altering objects rather than strings or timestamps.

Remember: Date and time are not distinct data types, but a datetime module can be imported to operate with both date and time. Moreover, the Python Datetime module is pre-installed, so no additional installation is required.

Summary

As a recap, this tutorial discusses every detail of how to use the Python fromtimestamp() function and explains how this works in different scenarios.

The examples above were all available for beginners and programmers to use. Either apply the concepts of the functions provided or alter them with your own.

Nevertheless, the Python fromtimestamp() function is understood to be under the datetime module. Additionally, this function is useful for converting any time format into an actual DateTime.

Leave a Comment