Hey what's up guys.
I'm Jacob blitzed with Def slips dot com in this lesson.
We're going to get our duration timer working and formatted.
So that's going to be really cool.
Last time we got our distance finally working and we have it all formatted for meters to Miles.
And obviously if you want kilometers you can just change that extension that we made and you have your
kilometers.
So in this lesson we need to start a duration timer right.
And so that'll be in our Start Run we'll probably have like us start timer function.
So inside of our Start Run why don't we just call start timer cool and underneath and run let's go ahead
and make a function func start timer and opening and closing brackets and all right.
So let's also we need a variable for our timer right.
So underneath run distance Let's go ahead and just do var counter or you call it timer or whatever you
want and set it to zero.
So this is just going to be an integer and it's just going to keep track of seconds for us and then
we are going to put an extension on type integer and do our whole formatting thing that we did before
with meters to Miles.
But we're going to format the timer from seconds to our minutes and seconds format.
And so it'll be really cool just like our label if you remember I don't let's say let's just pull up
our docs real quick.
On run here we go.
So basically we'll have you know are our hours minutes and seconds and that is we're going to format
seconds to display it.
All three of those.
All right so we have our counter which is just an integer which is great.
And then we need to create a our start timer function.
So the first thing would be we need to format or we would need to start a timer right.
So to create a timer we're going to need a another variable and we'll put it underneath a last location
so var timer and timer is going to be equal to a instance of timer just like that.
So we have our timer here and now in start timer we just have to set it up so to set up our timer.
Let's go ahead and start timer function.
Let's go ahead and do timer equals timer capital for timer scheduled timer.
And we want it with interval.
We want it not we don't want the repeating one we want to have the selector.
Here we go this is what we want.
OK so our interval is going to be one we want to count one value every time it gets called The Target
self and our selector is going to be a function that we have to make right after this.
But we'll call our function like Update counter.
OK.
And then the user info we don't need nyl and we want this to repeat Trews.
So this is constantly going to be running when we're in this Visi and it's yelling at us because it
doesn't know where.
Update timer so underneath start timer.
Let's make another function and call this update counter.
Our cool and inside update counter are variable we created at the top.
Instead it's a zero let's just say counter plus equals 1.
So we're just going to add 1 to it every time this is called.
Which is it makes sense right.
And then we also want to take our duration label dot text.
Whoops I can't get the period text and we want that to equal our counter.
Right.
All right cool.
And we should probably have our start timer.
Also set our duration level as well.
So duration dot text equals counter that way.
Start timers only called once but we want our label to be updated to zero right away right.
Without using the default storyboard label.
So with this being done we're going to now display segments on our Visi what doesn't.
Oh I have to just
you go.
At once a string because it's a label.
So we'll just print this for now.
But the problem is it's just going to show one integer it's going to be seconds.
Right.
And that's not how we want to display but we'll make sure our timer works.
So go ahead and press start.
Run.
And there we go our timer is counting up our distances move in.
And if your distance isn't moving de-bug location city run.
So our timer works but now we just need to format it.
So it looks nice because if it gets over 60 seconds it's going to be pretty unreadable at a glance right.
So let's go ahead and create our extension off of integer or we should extend and.
So let's open our extensions.
Class or file.
And we want to now extent an extension off of int it opening and closing brackets and we will call this
function format time duration
2 string.
So we're we're actually going to change our integer directly into a string before we return it.
So we don't have to cast it in the label later it'll just make our lives a little easier.
So let's go ahead and call this format time duration to string and we're returning a string.
So this is also cool because you extending and right and you're not returning and you're actually returning
a string so that's cool.
And let's see we're passing.
And so we're going to be formatting and integer and we need duration for hours minutes and seconds.
So let's set up a variable for lat duration of hours equal self and we need to divide the integer to
figure out hours by 3600.
And that's because there are thirty six hundred seconds in an hour.
And now we need to also create a variable for duration minutes and that is going to be equal to.
We need a opening in parentheses.
So we need self.
And then we're going to use a modulus 3600.
And what that does it's going to return how many times we can divide thirty six hundred into it.
And then once we know that we can then divide by 60 which there are 60 Minutes in an hour and that will
give us how many minutes are in the integer of seconds we just passed in and then we all are going to
do the same thing for seconds so that duration seconds equal
self modulus 3600 and then we also want to have this one return.
How many times
seconds can be divisible into the remaining value so we're going to do another modulus 60.
And that is going to give us how many seconds are left in this integer we passed in and then we can
do some cool things like if duration.
Second chance is less than zero.
That means it would actually return zero.
Right well you still want it to return a value that we want to see so we can just return the string
of 0 0 0 0 0 0.
So that's what we want it to look like.
If there is no value and then else we can also do like if duration hours isn't equal to zero or is equal
to zero I'm sorry.
Then we will return a string.
Whoops.
I'm having struggles here.
A string of type format and the way you format a string now is you create a string and you do a percent
sign.
And then we want two places.
So we always want if only like a single integers return like one or two.
We want to make sure the second below far left field still has a value in it.
And so we are going to put a 0 0 here and that means it'll always be filled with a 2.
Or I mean I'm sorry.
We always want two spots in this field right.
And if there is only one it will put a zero there.
That's what that means.
And then we'll put our colon.
And we want the same thing as 0 2.
And we also need Deese there because they are type double.
And then we just end our quotation and then arguments we pass in here since duration ours is zero.
We just pass and duration minutes and duration seconds.
And so this will get passed here and then duration seconds will get passed here and then so if duration
hours isn't equal to zero.
We want to return all three locations with the values so format opening and closing parentheses percent.
0 2 d Colan percent 0 2 D.
Colon and percent zero D.
And then our arguments would be duration hours duration minutes and duration seconds.
And that will return our formatted label for us.
So now let's go ahead and go back to our current current run Visi and where the counter is let's just
delete our our string where we can add the variable in and just call counter and we can say Dot.
And we want to format time duration to string and then same in our update counter counter data format
time duration 2 string.
And let's go ahead and run this.
And we should now see our cool label being updated and formatted properly.
All right so now if we just press Start Run There we go with that.
Our seconds are there and minutes will fill in there and our hours are missing until the run would get
that long of an hours would appear.
So that's really cool.
So we are getting there guys.
I'm going to call this video done before we are there let's go ahead and pull up our terminal and make
sure we commit this so get ad period get commit Desch am and we got our duration timer functioning.
All right sweet guys in the next run or in the next lesson we are going to get our average pace working.
So if you guys want to stretch your abilities you should try to get that working before starting the
next lesson.
And then if you get stuck.
Watch the next lesson and I will help you figure it out.
All right.
So I will see you in the next lesson.