diff --git a/ox/format.py b/ox/format.py index a06c311..73cc829 100644 --- a/ox/format.py +++ b/ox/format.py @@ -30,6 +30,8 @@ def toAZ(num): az = digits[r] + az return az +encode_base26=toAZ + def fromAZ(num): """ Converts a bijective base 26 string to an integer @@ -71,6 +73,8 @@ def to26(q): converted.insert(0, l) return "".join(converted) or 'A' +decode_base26=toAZ + def from26(q): """ Converts an base 26 string to an integer @@ -402,6 +406,43 @@ def format_duration(ms, verbosity=0, years=True, hours=True, milliseconds=True): duration = ' '.join(durations) return duration +def format_timecode(seconds): + ''' + >>> format_timecode(60 * 60 * 24 * 366) + '1:001:00:00:00.000' + >>> format_timecode(30) + '00:30:00' + ''' + seconds = float(seconds) + d = int(seconds / 86400) + h = int(seconds % 86400 / 3600) + m = int(seconds % 36000 / 60) + s = float(seconds % 60) + duration = "%02d:%02d:%06.3f" % (h, m, s) + if d: + duration = '%d:'%d + duration + while duration.startswith('00:'): + duration = duration[3:] + return duration + +def parse_timecode(string): + ''' + Takes a formatted timecode, returns seconds + + >> parse_timecode('1:02:03:04.05') + 93784.05 + >> parse_timecode('3') + 3.0 + >> parse_timecode('2:') + 120 + >> parse_timecode('1::') + 3600.0 + ''' + timecode = 0 + for i, v in enumerate(list(reversed(string.split(':')))[:4]): + timecode += float(v) * ( 86400 if i == 3 else pow(60, i)) + return timecode + def ms2runtime(ms, shortenLong=False): # deprecated - use format_duration '''