Exercise #1

Write a function named delorean that takes an integer. Return a datetime that is that many hours ahead from starter.

import datetime


starter = datetime.datetime(2015, 10, 21, 16, 29)

def delorean(int1):
    sec = int1 * 3600
    ahead_date = starter + datetime.timedelta(seconds=sec)
    return ahead_date

 


Exercise #2

Write a function named time_machine that takes an integer and a string of “minutes”, “hours”, “days”, or “years”. This describes a timedelta. Return a datetime that is the timedelta’s duration from the starter datetime.

 

# Remember, you can't set "years" on a timedelta!
# Consider a year to be 365 days.
## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)

import datetime


starter = datetime.datetime(2015, 10, 21, 16, 29)

def time_machine(duration, timestr):
    timestr = timestr.lower()
    if timestr == 'years':
        duration = duration * 365
        td = datetime.timedelta(days = duration)
    elif timestr in ['days', 'hours', 'minutes']:
        td = datetime.timedelta(**{timestr:duration})
    return starter + td

 


Exercise #3

Create a function named timestamp_oldest that takes any number of POSIX timestamp arguments. Return the oldest one as a datetime object.

 

# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in.

import datetime


def timestamp_oldest(*timestamps):
    time_list = list(timestamps)
    time_list.sort()
    return datetime.datetime.fromtimestamp(time_list[0])