Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Dates and Times!
You have completed Python Dates and Times!
Preview
In this video, we dive into the final function of the Time Tracker App, a powerful tool for calculating the total time spent on a client in the last X number of days.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, we've come to our final function for
this project, you are almost there.
0:00
We are here at
the display_x_days_totals function.
0:05
Now for this one, the user is entering
a day in which they want to go back in
0:09
time to start grabbing the data from.
0:14
So this is very,
very similar to our date range function.
0:17
In fact,
we can probably reuse a lot of that code.
0:20
But what we can't reuse is the range, and
0:23
that's what we're trying
to figure out here.
0:26
So as it says in this TODO,
it says, determine the start and
0:29
end datetimes for this range.
0:33
Now, the end date time should
be pretty straightforward,
0:35
because it's now, it's right now.
0:39
So let's pop that right in.
0:42
So datetime.datetime.now.
0:44
And that's half the battle done.
0:47
Now let's have a look at this one,
this start datetime.
0:50
We are getting passed in a number of days,
this is just an integer.
0:53
It could be five, it could be ten, and
0:56
that's how many days we have to go back
in time to find our starting datetime.
0:59
So, this calls for relative delta,
and we want to set
1:05
a relative delta that goes
back a certain number of days.
1:10
So let's call it go_back.
1:15
And we'll set this to
a relativedelta.relativedelta(days=days).
1:17
Okay, so let's say days comes in as five,
1:24
this will be relativedelta(days=5).
1:28
Now all we have to do here is take today's
date and then minus that many days.
1:31
So we'll do
datetime.datetime.now()- go_back.
1:38
So that should give us the range,
the start date,
1:42
and the end date of our range.
1:47
Let's just give it a quick go,
range_start_dt, range_end_dt.
1:50
And let's just see what that looks like,
python3, whoops, my apologies.
1:57
Okay, start calculating the totals,
2:04
let's do ACME because we know we've done
a couple of things with ACME recently.
2:07
Check the last x days, so
let's just do the last three days.
2:13
Okay, so it looks like this was
three days ago on the 9th, and
2:16
this is today, that's the 12th,
so that is the last three days.
2:21
Okay, so we can comment that out.
2:27
Now we've got the start and
end of the range in datetimes,
2:28
which means this next TODO
is actually pretty easy.
2:33
Filter and display client_jobs to only
include those within the start and
2:37
end datetimes.
2:41
That sounds a lot like what
we did in the last function.
2:42
So, I'm gonna challenge you
to pause this video and
2:46
try to write it out by yourself without
scrolling up and copying everything.
2:50
Give it a go, I'll be right here.
2:55
Okay, I hope you didn't scroll up, I hope
you gave it a really good go yourself.
2:58
Now, I will very quickly type
up the rest of the code and
3:03
you can have a look to see
if you did a similar thing.
3:07
So first we'll have
a total=relativedelta.relativedelta,
3:10
this will just be an empty total for
us to be adding to.
3:15
And then we'll start our loop for
job in client_jobs.
3:18
Now, our job_end datetime is
3:24
datetime.datetime.strptime.
3:29
And our date string is job end_time.
3:34
And our format is,
we can scroll up for this one.
3:39
[LAUGH] Is this, okay?
3:44
Now if the range_end_dt is
3:50
greater than our job_end_dt,
3:55
and also greater than our range_start_dt,
4:00
we can create our job_start_dt,
4:07
which I will just grab this.
4:12
Okay, now I've got the end and the start.
4:19
Let's calculate the difference.
4:23
Time_spent relativedelta.relativedelta,
4:25
job_end, and job_start.
4:31
And then finally,
we will print the job description.
4:35
And then the time_spent.hours, hours,
4:44
and time_spent.minutes, minutes.
4:49
Okay, so that's our print statement and
4:54
then we'll add everything
to total time_spent.
4:58
And then we'll exit our if block,
and it will exit our for loop, and
5:03
then print our finals.
5:07
So, total for client, and then print.
5:09
We will do total_hours,
5:18
hour, and total.minutes, minutes.
5:23
Okay, so
you can either have typed all that out or
5:32
perhaps you can grab it from the last bit.
5:35
However, I think we are possibly done.
5:38
So, let's come over here to our terminal,
we'll exit it, make it nice and big.
5:43
And then we will run the app,
calculate our totals.
5:49
Let's do ACME.
5:54
We'll check the last x days,
we'll check the last three days.
5:56
And it looks like we've got some more
React, some cleaning of the roof,
6:00
and some mow the lawn,
which we did earlier in this course.
6:06
Now you've got a complete app,
so we can start tracking.
6:10
We can do ACME, [LAUGH] and
let's cook dinner.
6:15
And then we can wait for a little bit.
6:20
And now that the clock has ticked over,
we can stop our tracking.
6:26
And then we can calculate totals,
so let's see Emmerton first.
6:30
We use Emmerton to retrieve all data.
6:35
And we can see all the times
that we've spent on Emmerton.
6:39
And let's calculate totals again.
6:43
We'll check out ACME, and
then we'll enter a date range.
6:45
Let's do the beginning of the month,
6:51
09-01 to 09 let's 05.
6:55
Okay, looks like we've grabbed all
of those between that date range.
6:59
And it looks like we've got seven hours.
7:03
And finally, we can calculate totals.
7:05
We'll do ACME again.
7:08
We'll check the last x days.
7:10
And we should see that cooked
dinner that we just put in.
7:11
Retrieved data, let's do three days.
7:15
So we've got React for an hour,
clean the roof for zero minutes,
7:18
mow the lawn a minute, and cook dinner for
one minute, and that is our total there.
7:22
And there you have it.
7:27
You have completed this time tracking
app as the developer who works on
7:28
the date time functions.
7:33
Now that you understand what
date time can do for you.
7:35
I can't wait to see what
apps you make with it.
7:38
Congratulations, you've completed
the Python dates and times course.
7:43
You learned about date,
time, date time, time delta,
7:49
and even the powerful relative
delta from the dateutil module.
7:53
Plus, you've completed two practical
apps for your team, a birthday app,
7:59
and a time tracker app.
8:04
So keep coding, keep creating.
8:07
And remember,
your Python journey is just beginning.
8:09
There are no limits to
what you can achieve.
8:13
Happy coding, and I'll see you next time.
8:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up