Hi,
Recently I started again on the 100 days of swiftUI. Never finished it before but want to try it again (hopefully I will make it to day 100 this time ;)).
I'm currently finished (I think) with day 19 but do wonder if things can be done better. I searched of course online and on this forum but "time conversion" give a lot of results and it doesn't look like what I think I need.
Last time I did day 19 I also choose for the seconds/minutes/hours/days converter. Mainly because I understand the math behind that so it is way easier to see if my code is doing what is should be doing. Last time, if I for example entered 22 hours and wanted to convert that to days, it would say 0 days. So it only worked with whole numbers. (I don't know why I kept it with that, maybe because my brain thougt that Paul set to not make it to complicated, so I just continuted). This time I want to do better. So if a user for example enters 1234567 seconds and want to know how many days that is, it will say 14 days, 6 hours, 56 minutes and 7 seconds.
I have that working but do wonder if it can be better. A small example;
If the user selected they want to convert their given input to days, It will do this:
else if timeOutSelected == "days" {
timeCalc = timeIn / 60 / 60 / 24
hoursReturn = HourCalc
minutReturn = minutCalc
secondsReturn = secondsCalc
returnString = "\(timeCalc) days \(hoursReturn)\(minutReturn)\(secondsReturn) "
}
In that code runs for example hourCalc. That looks like this:
var hourCalc: String {
var hour = 0
var hourReturn = ""
let timeIn = inputToSeconds
hour = (timeIn % 86400) / 3600
if hour == 0 {
hourReturn = "0 hours "
} else {
hourReturn = "\(hour) hours "
}
return hourReturn
}
minutReturn and secondsReturn look similar but with ofcourse different math. I'm doing the math in multiple locations and that makes me wonder if I'm doing the right thing. And even more, is their a better way in doing this?