Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We tackle the functions responsible for starting and stopping time tracking. We'll harness the power of the now function and explore how to format time with `strftime`.
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
We'll start off this project by
completing the functions to start and
0:00
stop tracking time, this start_tracking
function, and this stop_tracking function.
0:04
So we'll start with this one,
the start tracking function.
0:11
The ability to append all of our data to
a CSV has already been written down here.
0:15
So we'll just have to focus on
the to do that was left for us.
0:21
So it says grab the current time and
store it as a string in start_time
0:25
in the format this, so and
it gives us an example as well.
0:31
So it looks like we're going to need to
have our format code reference up again.
0:35
So make sure you have that ready to go.
0:41
Now, since we're trying
to grab the current time,
0:44
it would make sense to use datetime.now.
0:47
So let's start a variable now =
datetime.datetime and use the now method.
0:49
Now, we're getting an error,
because I have not imported data on yet.
0:57
So let's make sure we come up and
do that import datetime, and
1:00
why not preemptively import
relative delta as well.
1:04
So from dateutil import relativedelta,
okay,
1:08
and we'll add that space there, all right.
1:14
Now since we're turning a datetime
object into a string, we're going to
1:20
need to use string format time,
which means we'll need a string_format.
1:25
So I'm just going to
do that right here and
1:30
the format is hour,
which is capital I, and
1:35
then minute, which is capital M.
1:39
And then we've got am or pm, which is
lowercase p, and we've got a space.
1:43
And then we have our capital Y,
lowercase m and lowercase d,
1:49
which we're very familiar with
now from the last project.
1:54
Okay, we've got our string_format or
actually, let's call it format_string.
2:00
And now, we want to turn our start_time
string to a start_time datetime.
2:10
So what we'll do here is we'll
use string_format time, so
2:16
datetime.datetime.strftime and
the first will be the date itself.
2:21
Now again, if you're using PyCharm,
you'll get this little handy hint.
2:27
So the first one is the date which is now,
and the next is the format_string,
2:30
which is very handy format_string.
2:35
Okay, that seemed pretty easy, didn't it?
2:40
[LAUGH] Let's come down here to
stop_tracking and have a look.
2:43
It says, grab the current time and
store it as a string in end_time, and
2:48
then we also have some code to
append the data to our CSV.
2:53
So it looks like it's
almost exactly the same,
2:56
just with end_time instead of start_time.
2:58
So let's come up here,
grab the code that we've already written,
3:01
we will replace it, and
then change this to end_time.
3:07
Okay, let's see if this works, so
3:12
we make sure everything
is saved with python3
3:17
main.pi and we can start tracking.
3:22
Let's say that we will do ACME,
3:26
because it's a shortest short description,
I know, moves along [LAUGH].
3:29
Okay, it says start tracking
Mow the lawn for ACME and
3:35
it looks like it's tracking now.
3:39
So now, if we quit the app and
we come over here to our data.csv,
3:42
we can see that we've
got ACME Mow the lawn,
3:48
and then start time and
date, and then no end time.
3:51
So that's pretty cool,
we're now gonna sit around for
3:56
a couple minutes and wait for
the clock to tick over, and
4:00
then we're going to go and stop tracking.
4:05
Okay, the clock has ticked
over to the next minute.
4:09
So let's run the app again, and
then now we can press 3 to stop tracking,
4:12
and it says stopping tracking.
4:17
And we don't even have to quit the app,
this time we can see that the end time,
4:21
which is a minute after we
started has been appended, and
4:26
we have a complete line for
ACME Mow the lawn.
4:30
So we've completed our first two
functions to deal with starting and
4:34
stopping our tracking, so well done.
4:39
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