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
Neil Andruski
21,509 PointsNeed help with timestamp_oldest problem
I am having trouble trying to understand what i need to do with the fromtimestamp method within this problem. Its not working and I am getting a missing Year(pos 1) error. This is what I have so far but I don't know if I am on the right track
import datetime
# If you need help, look up datetime.datetime.fromtimestamp()
def timestamp_oldest(args):
#set a starting reference
result = datetime.datetime()
diff = datetime.dateim.now() - datetime.datetime.fromtimestamp(args[0])
#loop through arguments to find the oldest
for arg in args:
#if the oldest update references
if(datetime.datetime.now() - datetime.datetime.fromtimestamp(arg) > diff):
diff = datetime.datetime.now() - datetime.datetime.fromtimestamp(arg)
result = datetime.datetime.fromtimestamp(arg)
return result
2 Answers
Kenneth Love
Treehouse Guest TeacherYou have a typo on line 7 (the first with diff =).
As for how to use datetime.datetime.fromtimestamp(), you just pass it a POSIX timestamp, which is what *args is full of.
You're doing a lot of unnecessary extra work. Remember way back in Python Basics and Python Collections when we talked about how list has a .sort() method that sorts a list? Well, timestamps are just floats so they sort oldest to newest just fine. Convert your *args tuple into a sortable format, though.
Adam Tatusko
16,589 PointsHere's my solution based on Kenneth's help:
import datetime
def timestamp_oldest(*my_tuple):
my_list = list(my_tuple)
my_list.sort()
return datetime.datetime.fromtimestamp(my_list[0])
Kenneth Love
Treehouse Guest TeacherThe only faster way I know of (but haven't taught yet), would be to use sorted().
import datetime
def timestamp_oldest(*args):
return datetime.datetime.fromtimestamp(sorted(args)[0])
Adam Tatusko
16,589 PointsThat's some fine Python code there, Kenneth. You can't get much more concise than that! :-)