G
google0
I know this is a trivial function, and I've now spent more time
searching for a surely-already-reinvented wheel than it would take to
reinvent it again, but just in case... is there a published,
open-source, function out there that takes a string in the form of
"hh:mm:ss" (where hh is 00-23, mm is 00-59, and ss is 00-59) and
converts it to an integer (ss + 60 * (mm + 60 * hh))? I'd like
something that throws an exception if hh, mm, or ss is out of range, or
perhaps does something "reasonable" (like convert "01:99" to 159).
Thanks,
--dang
p.s.
In case this looks like I'm asking for a homework exercise, here's what
I'm using now. It returns False or raises a ValueError exception for
invalid inputs. I'm just wondering if there's an already-published
version.
def dms2int(dms):
"""Accepts an 8-character string of three two-digit numbers,
separated by exactly one non-numeric character, and converts it
to an integer, representing the number of seconds. Think of
degree, minute, second notation, or time marked in hours,
minutes, and seconds (HH:MM:SS)."""
return (
len(dms) == 8
and 00 <= int(dms[0:2]) < 24
and dms[2] not in '0123456789'
and 00 <= int(dms[3:5]) < 60
and dms[5] not in '0123456789'
and 00 <= int(dms[6:8]) < 60
and int(dms[6:8]) + 60 * (int(dms[3:5]) + 60 * int(dms[0:2]))
)
searching for a surely-already-reinvented wheel than it would take to
reinvent it again, but just in case... is there a published,
open-source, function out there that takes a string in the form of
"hh:mm:ss" (where hh is 00-23, mm is 00-59, and ss is 00-59) and
converts it to an integer (ss + 60 * (mm + 60 * hh))? I'd like
something that throws an exception if hh, mm, or ss is out of range, or
perhaps does something "reasonable" (like convert "01:99" to 159).
Thanks,
--dang
p.s.
In case this looks like I'm asking for a homework exercise, here's what
I'm using now. It returns False or raises a ValueError exception for
invalid inputs. I'm just wondering if there's an already-published
version.
def dms2int(dms):
"""Accepts an 8-character string of three two-digit numbers,
separated by exactly one non-numeric character, and converts it
to an integer, representing the number of seconds. Think of
degree, minute, second notation, or time marked in hours,
minutes, and seconds (HH:MM:SS)."""
return (
len(dms) == 8
and 00 <= int(dms[0:2]) < 24
and dms[2] not in '0123456789'
and 00 <= int(dms[3:5]) < 60
and dms[5] not in '0123456789'
and 00 <= int(dms[6:8]) < 60
and int(dms[6:8]) + 60 * (int(dms[3:5]) + 60 * int(dms[0:2]))
)