Question

I have been struggling to understand how to use datetime objects. I want to use datetime.date instances as keys in a dictionary. I then want to be able to return dates within specified ranges using datetime.delta.

My first conundrum is when I create an object to be entered into the dictionary.

class Work_day():
    '''input a workday , date and hours worked'''

    def __init__(self, date, hours, rate):
        self.date = datetime.date() 
        self.hours = hours
        self.rate = rate

I want self.date to be a datetime.date object but datetime.date takes 3 argument (year, month, day) so what is the correct syntax for the def_init_ argument 'date'?

Then I assume when I change how that is written in the Work_day class then I will have to modify my code when I create instances of it in the Timesheet class e.g. in add_work_day() method

class Timesheet():
    '''Represent a collection of workdays'''

    def __init__(self):
        self.timesheet = {}

    def add_work_day(self, date, hours,rate):
        '''adds a record of a work day into the timesheet dictionary'''

        day = Work_day(date, hours, rate)
        if day.date in self.timesheet:
            print("There is already an entry for this day. ")

        else:
            self.timesheet[day.date] = hours, rate

I've been researching the python docs and scouring books but I'm not getting it! Need some help.

I also have a method that prints a range of the workdays in the timesheet. I made it work when I subbed the date key for a simple int. here it is (in ''' ''') with a shonky attempt at a datetime delta underneath

def show_days(self):
    '''shows a user defined range of dates and the total pay for that period'''
    pp = pprint.PrettyPrinter()
    date_from = input("From date: ")
    date_to = input("To date: ")
    t = self.timesheet
    total = 0

    '''for dates in range(date_from, date_to + 1):
        if dates in t:
            total += self.sum_day(dates)
            pp.pprint((dates, t[dates)])
    print("Total £", total)'''

    date = date_start = datetime.date(date_from)
    date_end = datetime.date(date_to)

    while date <= date_end:
        if date in t:
            print(date, t[dates])
        date += datetime.timedelta(days=1)

I hope someone can find the patience to talk me through this. Cheers.

Was it helpful?

Solution

If you assign the date with self.date = datetime.date(*date), then you can create a Work_day by passing a (year,month,day) tuple:

day = Work_day((2013,5,31), 8.0, 8.25)

Alternatively, if you want the input to be a date string, use datetime.strptime, an appropriate formatting string, and the date() method to get a date object:

self.date = datetime.datetime.strptime(date,'%m/%d/%Y').date()
...
date = Work_day('5/31/2013', 8.0, 8.25)

Finally, you could just pass a date object:

day = Work_day(datetime.date(2013,5,31), 8.0, 8.25)
...
self.date = date

The Timesheet class should work after any of these changes. show_days still needs some work, but I'll leave that as an exercise. Hint: Parse the input dates with strptime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top