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

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

timestamp.py challenge : Didn't get back a datetime

I was trying to solve this in several ways but i don't figure out what's the problem :/

sorry for the comments :P

import datetime

def timestamp_oldest(*args):

    # Creating a simple list to sort
    # list1 = ['physics', 'chemistry', 'test1', 'test2', 'test3'];
    time_stamps = []
    format_time = []
    dtf = []  # a date time format list 

    # Sorting the list
    # list1.sort()
    time_stamps.append(args)


    # ts = time.time()
    for arg in args:
        actual_time = datetime.datetime.fromtimestamp(arg)
        # print(actual_time)
        format_time.append(actual_time)


    # Printing the list of format_time
    count = len(format_time)
    for index in range(0,count):
        result = datetime.datetime.strftime(format_time[index], "%Y-%m-%d %H:%M:%S:%f")
        dtf.append(result)

    return dtf
    # for count in len(actual_time):
    #    result = datetime.datetime.strftime(format_time[count], "%Y-%m-%d %H:%M:%S:%f")
    # print(result)

    # actual_time = datetime.datetime.fromtimestamp(ts)
    # print(actual_time)

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Aldo,

Don't apologise for the comments, documenting your code is always helpful. I would suggest that in future you follow the guidance in the Markdown Cheatsheet and specify the language in your code snippet, as follows:

import datetime


def timestamp_oldest(*args):

    # Creating a simple list to sort
    # list1 = ['physics', 'chemistry', 'test1', 'test2', 'test3'];
    time_stamps = []
    format_time = []
    dtf = []  # a date time format list 

    # Sorting the list
    # list1.sort()
    time_stamps.append(args)


    # ts = time.time()
    for arg in args:
        actual_time = datetime.datetime.fromtimestamp(arg)
        # print(actual_time)
        format_time.append(actual_time)


    # Printing the list of format_time
    count = len(format_time)
    for index in range(0,count):
        result = datetime.datetime.strftime(format_time[index], "%Y-%m-%d %H:%M:%S:%f")
        dtf.append(result)

    return dtf
    # for count in len(actual_time):
    #    result = datetime.datetime.strftime(format_time[count], "%Y-%m-%d %H:%M:%S:%f")
    # print(result)

    # actual_time = datetime.datetime.fromtimestamp(ts)
    # print(actual_time)

As you can see, it makes the code MUCH more readable, and so much easier for people to help you.

As far as your code is concerned, you are making it harder than it needs to be. You don't need to put anything in a list and you mustn't return a list, you just return a single datetime.

Note that to solve this, all you need to do is:

  1. get the smallest number in args. Since POSIX timestamps are simply numbers, you can use the min() function to get the smallest number (i.e., oldest timestamp) from args.
  2. Now you are only working with one number, the POSIX timestamp representing the oldest point in time. You know how to create a datetime object from a timestamp (you do that when you assign a value to actual_time).

  3. Once you've done that, all you need to do is return it.

Hope that's clear

Alex

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

Alex Koumparos first at all, I want to apologize for that, this is my first question, I think I was made the md format, anyway. Your explanation steps are very neat!! I don't know if I don't understand the problem to solve, maybe I need to improve my English.

Thank you so much!

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

HI Aldo,

No apology needed. Glad that the explanation steps were helpful.

As for understanding the problem to solve, you obviously understood the critical component, which was the application of the datetime.fromtimestamp() method. It looks like you just got lost inside your own head for the implementation by thinking the problem was bigger than it actually was, which happens to us all from time to time!

Cheers

Alex