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 trialAndrei Oprescu
9,547 PointsI am very confused at this task
Hi!
I have stumbled across a challenge that asked me this:
Create a function named timestamp_oldest that takes any number of POSIX timestamp arguments. Return the oldest one as a datetime object.
Remember, POSIX timestamps are floats and lists have a .sort() method.
I don't remember a time where I was thought what a 'POSIX timestamp' is and I don't know how they are written.
can someone tell me what a 'POSIX timestamp' is?
Thanks!
Andrei
# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in.
3 Answers
John Goodwin
17,921 PointsThis is pretty close! It will actually be easier to use the min() method instead of sort() because of the data. Also, we are missing a classmethod from inside datetime called fromtimestamp. That classmethod will handle the error about integer expected, but got float. The min() method is similar to sorting the data and then taking index 0 (there is also a max() method to get the maximum value).
Code that passes the challenge looks like this:
import datetime
# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in
def timestamp_oldest(*args):
all_timestamps = []
for value in args:
all_timestamps.append(value)
return datetime.datetime.fromtimestamp(min(all_timestamps))
Signed integer is less than minimum is an overflow error. Because the args come in to the function together, you don't actually need to build a list for it. This works as well:
import datetime
def timestamp_oldest(*args):
return datetime.datetime.fromtimestamp(min(args))
John Goodwin
17,921 PointsHi Andrei,
I don't think you need to understand POSIX timestamps to complete the challenge. (Although, POSIX timestamps are basically the time counted in seconds since January 1, 1970, but what's important is they are very large numbers and an earlier timestamp will be a smaller number than a later timestamp.)
For this challenge, this line is important:
Remember, POSIX timestamps are floats and lists have a .sort() method.
All of the timestamps will be in the same format and they will all be floats. If you can get them into a list together and then .sort() the list, you can return the value that is the lowest number.
Andrei Oprescu
9,547 PointsHi John!
I have tried to complete the code challenge with this code:
import datetime
# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in
def timestamp_oldest(*args):
all_timestamps = []
for value in args:
all_timestamps.append(value)
all_timestamps.sort()
return datetime.datetime.date(all_timestamps[-1])
And in return, it gives me this:
descriptor 'date' requires a 'datetime.datetime' object but received a 'float'
Can you tell me what I did wrong in my code and how I can fix it?
Thanks!
John Goodwin
17,921 PointsHi Andrei,
I don't think you need datetime.datetime.date in that line. Also, the sort() put the lowest number first and the highest number last, so you may want to check the index you are referencing in the [].
Andrei Oprescu
9,547 PointsHi John!
I have done what you said in my code but in the end it said this:
Didn't get back a datetime
I did what it asked me and I came up with this code:
import datetime
# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in
def timestamp_oldest(*args):
all_timestamps = []
for value in args:
all_timestamps.append(value)
all_timestamps.sort()
result = int(all_timestamps[0])
return datetime.datetime(year=0, month=0, day=0, hour=0, second=result, microsecond=0)
It then gave me an error saying this:
signed integer is less than minimum
What should I do in this scenario?
Thanks!
Andrei
Andrei Oprescu
9,547 PointsOh thanks! I forgot the datetime.fromtimestamp() there. one last thing; is the datetime.fromtimestamp() used to convert a timestamp into a datetime? That's what I understood from that code.
Andrei
Ross Coe
5,061 PointsRoss Coe
5,061 Pointsnice and neat