Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Let's Build a Timed Quiz App Timestamp Ordering

Jacob Hill
Jacob Hill
2,805 Points

How does datetime.fromtimestamp actually work?

import datetime 
def timestamp_oldest(*arg): 
    holder = list(arg) 
    holder.sort()
    return datetime.datetime.fromtimestamp(holder[0])

So the above code works just fine. I understand everything there from changing the incoming tuples into a list, sorting them, holder[0] has the oldest value since it's the lowest value.

However the documentation for fromtimestamp only says,

"classmethod datetime.fromtimestamp(timestamp, tz=None)

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive."

What exactly is getting returned? What does it mean by local date and time? When the docs call the argument, 'timestamp' does it mean a POSIX timestmap? Is a POSIX timestamp just a floating point number formatted in a certain manner?

Thanks in advance.

1 Answer

Hello

please read here to know more about POSIX timestamp

https://en.wikipedia.org/wiki/Unix_time

so a POSIX timestamp or sometime called EPOCH time is something like this (example from url)

1483139842 (ISO 8601: 2016-12-30T23:17:22Z)

1483139842 is number of seconds since 1/1/1970

so now back to your question

localrime is the time as set on your PC .... if you look at the lower right side of your window OS, there is a clock and if you are like me, you have it display the local time

I made minor changes to your code to illustract

import datetime

import pytz

def timestamp_oldest(*arg):

holder = list(arg)

holder.sort()

print(datetime.datetime.fromtimestamp(1226527167.595983))

print(datetime.datetime.fromtimestamp(1226527167.595983, tz=pytz.timezone('America/Montreal')))

print(datetime.datetime.fromtimestamp(1226527167.595983, tz=pytz.timezone('US/Pacific')))

print(datetime.datetime.fromtimestamp(1226527167.595983, tz=pytz.timezone('US/Central')))

print(datetime.datetime.fromtimestamp(1226527167.595983, tz=pytz.timezone('US/Eastern')))

timestamp_oldest()

this is return the date id date time from (e.g.; returning 2008-11-12 16:59:27.595983) which is a a reformatting of the EPOCH time 1226527167.595983 (representing number of seconds since 1/1/1970

take a look at the other print outs and see what you get.

now take this number

1226527167.595983/3600(number of seconds in one hour)/24 (number of hours per day)/365 number of days per year)

you should get ~39 yrs ...which is approx number of years between 2008 an 1970 ... this is an approx since I am assuming all years are 365 days.

Hope this help. If this answers your question, please mark the question as answered.