5 Examples to Understand Python FromTimeStamp Better

Hello Geeks! Welcome back to another article, i.e., Python fromtimestamp(). So, this one comes from the python DateTime module. So today, in this article, first, we will take a quick overview of the python timestamp and then will see the fromtimestamp() method from the Date class. Let’s get started.

Python Timestamp

Now, once we have overviewed about Datetime Module, take a look at Python Timestamp. So, first, understand what timestamp is? So, a timestamp is encoded information for the time of any particular happening. Talking more convincingly, we can say that the timestamp refers to a group of encoded characters representing any particular date-time, including seconds. However, we don’t need to worry about encoding as python handles it. Now, the question is how to get a timestamp. A simple answer for this is that different modules have their methods to create a timestamp. For example:-

ModuleMethods
Time Moduletime.time()
DateTime Moduledatetime_obj.timestamp()
Calendercalender_obj.timegm()

Example

#importing datetime class from datetime module.
from datetime import datetime 

random_date = datetime(2010,11,12,13,14,15)
print(random_date)

ts = random_date.timestamp()     # Converting date into timestamp
print(ts)

Output:

2010-11-12 13:14:15
1289567655.0

Now, once we understand timestamp, let’s see the python fromtimestamp() method.

Also, See | Easily Convert Python Timedelta to Seconds

Python Fromtimestamp()

So, this function is from the DateTime class and Date class of the DateTime module. We can Access it as datetime.datetime.fromtimestamp() or datetime.date.fromtimestamp() respectively. What this function does is return the date corresponding to any particular timestamp. We need to pass the timestamp object as the argument of this function, and in return, it gives us the datetime object corresponding to the given timestamp.

Example 1:- Converting timestamp to Datetime Object

#importing datetime class from datetime module
from datetime import datetime

#Creating a timestamp

datetime_obj = datetime(2010,11,12,13,14,15)
print(datetime_obj)
ts = datetime_obj.timestamp()     # Converting date into timestamp
print(ts)

# Converting timestamp to date

datetime_obj2 = datetime.fromtimestamp(ts)
print("Converted time stamp from datetime class",datetime_obj2)

Output:

2010-11-12 13:14:15
1289567655.0
Converted time stamp from datetime class 2010-11-12 13:14:15

Example 2:- Converting timestamp to Date Object

#importing date class from datetime module
from datetime import date

#Creating a timestamp

datetime_obj = datetime(2010,11,12,13,14,15)
print(datetime_obj)
ts = datetime_obj.timestamp()     # Converting date into timestamp
print(ts)

# Converting timestamp to date
date_obj = date.fromtimestamp(ts)
print("Converted time stamp from date class",date_obj)

Output:

2010-11-12 13:14:15
1289567655.0
Converted time stamp from date class 2010-11-12

Example 3:- Python fromtimestamp for UTC

UTC stands for Universal Time Coordinated. It is the standard used by the international community to keep time. Let’s see how we can create a timestamp for that and then get a datetime object from that timestamp.

from datetime import datetime,timezone

# Getting the current date
# and time
dt = datetime.now(timezone.utc)
print("dt object datatype",type(dt))
print(dt)

# Converting datetime object 
utc_timestamp = dt.timestamp()
print("Datatype of utc_timestamp",type(utc_timestamp))
print(utc_timestamp)
print(type(utc_timestamp))

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

Output:

dt object datatype <class 'datetime.datetime'>
2021-11-27 17:32:34.626788+00:00
Datatype of utc_timestamp <class 'float'>
1638034354.626788
<class 'float'>
Converted timestamp from datetime class 2021-11-27 17:32:34.626788

Example 4:- Python fromtimestamp Set Timezone

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

ACST = pytz.timezone('Australia/Darwin')
datetime_obj = datetime(1901,11,12,13,14,15, tzinfo=ACST)

print(datetime_obj)
ts = datetime_obj.timestamp()     # Converting date into timestamp
print(ts)
print(type(ts))

# Converting timestamp to date

datetime_obj2 = datetime.fromtimestamp(ts,tz=ACST)
print("Converted time stamp from datetime class",datetime_obj2)

Output:

1901-11-12 13:14:15+08:43
-2150220525.0
<class 'float'>
Converted time stamp from datetime class 1901-11-12 13:14:15+08:43

Example 5:- Python datetime fromtimestamp milliseconds

from datetime import datetime

time_in_millis = 1596542285000
datetime_obj = datetime.fromtimestamp(time_in_millis/1000)
print(datetime_obj)

Output:

2020-08-04 11:58:05

Errors while using fromtimestamp()

Error: Python datetime fromtimestamp yielding valueerror year out of range

f = 1437506779950.0
print(datetime.fromtimestamp(float(f)))


Output:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-a7a81cc369c5> in <module>()
      1 f = 1437506779950.0
      2 
----> 3 print(datetime.fromtimestamp(float(f)))

ValueError: year 47522 is out of range

The error is that we need to divide f to 1000 to convert milliseconds to seconds for computation.

f = 1437506779950.0
print(datetime.fromtimestamp(float(f)/1000))

Output:

2015-07-21 19:26:19.950000

Error: Python fromtimestamp() method inconsistent

This error is occurred due to an inconsistent timezone defined while using the function. Here is the complete list of timezones.

Conclusion

So, today in this article, we took the overview of the Python Datetime module. Then we discussed different classes in that module. After that, we have seen what timestamps are and how we can create a timestamp. In the end, we have seen how we can convert a timestamp to a datetime and date object using the fromtimestamp() method from both classes.

I hope this article has helped you. Keep supporting. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments