1 00:00:00,112 --> 00:00:04,400 Okay, so your task was to take this time in seconds, and convert that to 2 00:00:04,400 --> 00:00:09,268 a formatted string that showed those seconds in hours, minutes, and seconds. 3 00:00:09,268 --> 00:00:12,712 Now this is a bit tricky, but it all involves operators and 4 00:00:12,712 --> 00:00:14,268 the correct precedents. 5 00:00:14,268 --> 00:00:17,710 So first, we need to get the hours value out of this time component. 6 00:00:17,710 --> 00:00:22,153 And the way we do that is by dividing the total time by the number 7 00:00:22,153 --> 00:00:24,983 of seconds in an hour, so 3,600. 8 00:00:24,983 --> 00:00:29,495 Because this is an integer value when we do this division, even though we get 9 00:00:29,495 --> 00:00:34,218 a decimal value because it doesn't evenly divide here, at least in this case, 10 00:00:34,218 --> 00:00:38,960 the compiler, Swift, just gets rid of all the values after the decimal point. 11 00:00:38,960 --> 00:00:42,897 So we know that we have 664 hours with some amount of 12 00:00:42,897 --> 00:00:47,014 time remaining when we divide by the number of seconds. 13 00:00:47,014 --> 00:00:50,354 So that's how we got the hours component out. 14 00:00:50,354 --> 00:00:53,012 For the minutes, this is the trickiest part. 15 00:00:53,012 --> 00:00:58,284 For the minutes, we first want to see, once we figure out how many seconds there 16 00:00:58,284 --> 00:01:03,337 are or how many hours there are in this total value, what time is remaining? 17 00:01:03,337 --> 00:01:06,538 So essentially over here, when the decimal values got cut off, 18 00:01:06,538 --> 00:01:09,870 what were those decimal values, and how many seconds were those? 19 00:01:09,870 --> 00:01:13,100 And we can do that easily by using the modular operator. 20 00:01:13,100 --> 00:01:17,963 So when we do time % 3600, that operation defines how many 21 00:01:17,963 --> 00:01:23,587 3600s are there In the total time, and then gives us the remainder. 22 00:01:23,587 --> 00:01:29,745 So that remainder is the amount of seconds left after we deduct the hours component. 23 00:01:29,745 --> 00:01:32,072 Once we do that, we want it in minutes, so 24 00:01:32,072 --> 00:01:35,334 we simply divide it by 60 to get it in minutes which is 5. 25 00:01:35,334 --> 00:01:38,715 And then those are even number of minutes, because again, 26 00:01:38,715 --> 00:01:43,260 here it's going to discard the remainder because this is an integer division. 27 00:01:43,260 --> 00:01:47,952 So finally, to get any remaining seconds value, we do time. 28 00:01:47,952 --> 00:01:52,143 And then using the percent sign, we get the remainder of finding out how many 29 00:01:52,143 --> 00:01:55,043 seconds are there in this complete time component. 30 00:01:55,043 --> 00:01:56,585 And when we do that we get 24. 31 00:01:56,585 --> 00:02:01,144 So if I were to bring a calculator, or let me just write it out here, so 32 00:02:01,144 --> 00:02:04,840 if I were to do, you'll see that in our printed string, 33 00:02:04,840 --> 00:02:08,720 it says this is 664 hours, 5 minutes, 24 seconds. 34 00:02:08,720 --> 00:02:12,410 And we did that final part pretty easily using string interpolation. 35 00:02:12,410 --> 00:02:16,178 But if I were to do, for example, 664, 36 00:02:16,178 --> 00:02:20,910 that's hours, so in minutes, and then in seconds. 37 00:02:20,910 --> 00:02:26,156 To that we'll add 5 minutes in seconds and then 24 seconds. 38 00:02:26,156 --> 00:02:30,823 And you'll see that the final value is exactly 39 00:02:30,823 --> 00:02:35,021 the same as the value we started out with. 40 00:02:35,021 --> 00:02:38,617 So keep this formula in mind because this is something you'll be doing fairly 41 00:02:38,617 --> 00:02:39,348 often, okay? 42 00:02:39,348 --> 00:02:40,820 On to the next exercise.