Add hms_letters functions to make 00h00m00s outputs.
This commit is contained in:
parent
9381c2c9c7
commit
597705fc57
1 changed files with 28 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
import math
|
import math
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from voussoirkit import pipeable
|
from voussoirkit import pipeable
|
||||||
|
@ -21,6 +22,18 @@ def hms_to_seconds(hms) -> float:
|
||||||
seconds += float(parts[0])
|
seconds += float(parts[0])
|
||||||
return seconds
|
return seconds
|
||||||
|
|
||||||
|
def hms_letters_to_seconds(hms) -> float:
|
||||||
|
match = re.match(r'(?:(\d+)h)?(?:(\d+)m)?(\d+)s?', hms.strip())
|
||||||
|
if not match:
|
||||||
|
raise ValueError(f'{hms} does not match 00h00m00s pattern')
|
||||||
|
(hours, minutes, seconds) = match.groups()
|
||||||
|
seconds = int(seconds)
|
||||||
|
if hours:
|
||||||
|
seconds += int(hours) * 3600
|
||||||
|
if minutes:
|
||||||
|
seconds += int(minutes) * 60
|
||||||
|
return seconds
|
||||||
|
|
||||||
def seconds_to_hms(seconds, force_minutes=False, force_hours=False) -> str:
|
def seconds_to_hms(seconds, force_minutes=False, force_hours=False) -> str:
|
||||||
'''
|
'''
|
||||||
Convert integer number of seconds to an hh:mm:ss string.
|
Convert integer number of seconds to an hh:mm:ss string.
|
||||||
|
@ -38,11 +51,26 @@ def seconds_to_hms(seconds, force_minutes=False, force_hours=False) -> str:
|
||||||
hms = ':'.join(f'{part:02d}' for part in parts)
|
hms = ':'.join(f'{part:02d}' for part in parts)
|
||||||
return hms
|
return hms
|
||||||
|
|
||||||
|
def seconds_to_hms_letters(seconds, force_minutes=False, force_hours=False) -> str:
|
||||||
|
seconds = math.ceil(seconds)
|
||||||
|
(minutes, seconds) = divmod(seconds, 60)
|
||||||
|
(hours, minutes) = divmod(minutes, 60)
|
||||||
|
parts = []
|
||||||
|
if hours or force_hours:
|
||||||
|
parts.append(f'{hours:02d}h')
|
||||||
|
if hours or minutes or force_hours or force_minutes:
|
||||||
|
parts.append(f'{minutes:02d}m')
|
||||||
|
parts.append(f'{seconds:02d}s')
|
||||||
|
hms = ''.join(parts)
|
||||||
|
return hms
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
lines = pipeable.input_many(args, strip=True, skip_blank=True)
|
lines = pipeable.input_many(args, strip=True, skip_blank=True)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if ':' in line:
|
if ':' in line:
|
||||||
line = hms_to_seconds(line)
|
line = hms_to_seconds(line)
|
||||||
|
if 's' in line:
|
||||||
|
line = hms_letters_to_seconds(line)
|
||||||
else:
|
else:
|
||||||
line = float(line)
|
line = float(line)
|
||||||
if line > 60:
|
if line > 60:
|
||||||
|
|
Loading…
Reference in a new issue