I'm tellin' ya Imma parse every damn thing you can parse. For anybody that cares, below is my method for parsing milliseconds to hh:mm:ss:ms
It's pretty simple but I'll run through it once anyway for people that don't understand what .shift()/unshift() is doing.
1) we use a do/while loop because one condition of the while is that neither temp or cfg.length = 0. temp starts by equaling zero though. do/while loops work by running do once before checking while. This allows us to give temp a value. On the next iteration of the loop temp will already have a value so it gets converted to the next position of time (temp/conversion)
2) shift() works to remove the first index of an array and return it's value. So after the first iteration cfg = [60,60,24] and the next cfg = [60,24]
3) unshift() works to push something into the first array index. we start with milliseconds but on the next iteration seconds gets pushed into the beginning which pushes milliseconds up one.
4)I convert what I'm unshifting into the array into a string preceded by a 0, this way if the result is < 9 I have a leading zero to display. It's entirely possible that the result of this will be 3 numbers, we fix that in the end.
5) then I say ~ temp = all the time that's left ~ and the process starts over.
6) I loop through every array index and tell it that it equals only the last 2 characters of itself, voila 3 digit possibility crushed
7) finally, I use .join(":") to convert the entire array into a string with each index separated by ":"
time[0] = "02"
time[1] = "52"
time[2] = "26"
time.join(":");
"02:52:26"
There are 2 things that I believe make my method superior
1) by setting the while condition to make sure that both remaining time and our cfg array have length, the time that is returned will never have 00 time. In other words, if there are no hours a position for it will never be created, in contrast if their are days they wont hold the loop in infinity due to the while never reaching 0
2) this IS the algorithm for millisecond conversion. Which means all you ever have to do is add more conversion times to cfg to get more time
ex: cfg = [1000, 60, 60, 24, 31]
That would parse out even the days in the milliseconds leaving months as a hanging remainder. Of course to go this far is tricky cause the final index in the cfg array should be how many days are in the month that the milliseconds represent. You could of course just average it for a close enough result (365.3/12)
.3 should more or less handle leap years. You can have up to 3 leap years in a decade so, this is in the pocket. round(365.3/12) might also be a good idea especially since everything is uint
Code:
[color="MediumTurquoise"]public[/color] [color="RoyalBlue"]function[/color] millisecondConversion(_len:[color="PaleGreen"]uint[/color], cfg:[color="PaleGreen"]Array[/color] = [[color="DarkOrange"]1000[/color], [color="DarkOrange"]60[/color], [color="DarkOrange"]60[/color], [color="DarkOrange"]24[/color]]):[color="PaleGreen"]String[/color] { [color="RoyalBlue"]var[/color] time:[color="PaleGreen"]Array[/color] = [color="RoyalBlue"]new[/color] [color="PaleGreen"]Array[/color](); [color="RoyalBlue"]var[/color] conversion:[color="PaleGreen"]uint[/color], temp:[color="PaleGreen"]uint[/color]; [color="RoyalBlue"]do[/color] { temp = (!temp) ? _len : temp/conversion; conversion = cfg.shift(); time.unshift([color="#CC0000"]"0"[/color]+(temp % conversion)); temp -= [color="PaleGreen"]uint[/color](time[[color="DarkOrange"]0[/color]]); } [color="RoyalBlue"]while[/color] (temp && cfg.length); time.forEach([color="RoyalBlue"]function[/color](str:[color="PaleGreen"]String[/color], i:[color="PaleGreen"]uint[/color], arr:[color="PaleGreen"]Array[/color]) { arr[i] = str.substr(-[color="DarkOrange"]2[/color]): }); [color="RoyalBlue"]return[/color] time.join([color="#CC0000"]":"[/color]); }
1) we use a do/while loop because one condition of the while is that neither temp or cfg.length = 0. temp starts by equaling zero though. do/while loops work by running do once before checking while. This allows us to give temp a value. On the next iteration of the loop temp will already have a value so it gets converted to the next position of time (temp/conversion)
2) shift() works to remove the first index of an array and return it's value. So after the first iteration cfg = [60,60,24] and the next cfg = [60,24]
3) unshift() works to push something into the first array index. we start with milliseconds but on the next iteration seconds gets pushed into the beginning which pushes milliseconds up one.
4)I convert what I'm unshifting into the array into a string preceded by a 0, this way if the result is < 9 I have a leading zero to display. It's entirely possible that the result of this will be 3 numbers, we fix that in the end.
5) then I say ~ temp = all the time that's left ~ and the process starts over.
6) I loop through every array index and tell it that it equals only the last 2 characters of itself, voila 3 digit possibility crushed
7) finally, I use .join(":") to convert the entire array into a string with each index separated by ":"
time[0] = "02"
time[1] = "52"
time[2] = "26"
time.join(":");
"02:52:26"
There are 2 things that I believe make my method superior
1) by setting the while condition to make sure that both remaining time and our cfg array have length, the time that is returned will never have 00 time. In other words, if there are no hours a position for it will never be created, in contrast if their are days they wont hold the loop in infinity due to the while never reaching 0
2) this IS the algorithm for millisecond conversion. Which means all you ever have to do is add more conversion times to cfg to get more time
ex: cfg = [1000, 60, 60, 24, 31]
That would parse out even the days in the milliseconds leaving months as a hanging remainder. Of course to go this far is tricky cause the final index in the cfg array should be how many days are in the month that the milliseconds represent. You could of course just average it for a close enough result (365.3/12)
.3 should more or less handle leap years. You can have up to 3 leap years in a decade so, this is in the pocket. round(365.3/12) might also be a good idea especially since everything is uint
Comment