Last active
December 24, 2020 15:29
-
-
Save bigntallmike/b02b15584135bc8d62291d202a9a187b to your computer and use it in GitHub Desktop.
Convert HH:MM:SS timestamps in a list to seconds only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 -ttu | |
# | |
# For a list in format hh:mm:ss - hh:mm:ss, where hh: and mm: are optional, return seconds instead | |
# | |
# Free to use or critique, its not complex enough for licensing | |
import re | |
import sys | |
# Re-formatted for better visual parsing | |
timestampre = re.compile( | |
r"(" | |
r"((?P<shr>[0-9]+):)?" | |
r"((?P<smin>[0-9]+):)" | |
r")?" | |
r"(?P<ssec>[0-9]+)" | |
r"\s*-\s*" | |
r"(" | |
r"((?P<ehr>[0-9]+):)?" | |
r"((?P<emin>[0-9]+):)" | |
r")?" | |
r"(?P<esec>[0-9]+)" | |
) | |
def zeroifnone(val): | |
return 0 if val is None else int(val) | |
def secfromlist(h,m,s): | |
return 3600 * zeroifnone(h) + 60 * zeroifnone(m) + zeroifnone(s) | |
def timetosec(sourcefile): | |
with open(sourcefile, "r") as source: | |
for line in source: | |
data = timestampre.match(line).groupdict() | |
stime = secfromlist(data['shr'], data['smin'], data['ssec']) | |
etime = secfromlist(data['ehr'], data['emin'], data['esec']) | |
yield (stime, etime) | |
if __name__ == "__main__": | |
for (start,end) in timetosec(sys.argv[1]): | |
print("%d - %d" % (start,end)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment