convert seconds to HH:MM:SS and HH:MM:SS to seconds
While making a video stream project, I had to convert seconds to HH:MM:SS and vice versa, so I created this class.
/* USAGE: TimeUtils.encode(90); TimeUtils.decode("00:01:30"); */ package euro.utils { public class TimeUtils { public function TimeUtils():void { } public static function encode(_seconds:int):String { var seg = _seconds %60; var min = _seconds/60 %60; var hor = _seconds/60 /60; return String(normalize(hor,2) + ":" + normalize(min,2) + ":" + normalize(seg,2)); } public static function decode(_string:String):Number { var parts:Array = _string.split(":"); var seconds:Number = parts[2]; var minutes:Number = parts[1]; var hours:Number = parts[0]; return seconds + 60 * minutes + 60 * 60 * hours; } private static function normalize(_int:int, _size:int):String { var temp:String = String(_int) for (var i = 1; i < _size; i++) { if (_int < Math.pow(10,i)) { temp = "0" + temp; } } return temp; } } }


