function to convert degree (hour), minute, seconds string to integer

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]))
)
 
J

John Machin

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]))
)

Have you considered time.strptime()?

BTW, your function, given "00:00:00" will return 0 -- you may well have
trouble distinguishing that from False (note that False == 0), without
resorting to ugliness like:

if result is False ...

Instead of returning False for some errors and letting int() raise an
exception for others, I would suggest raising ValueError yourself for
*all* invalid input.

You may wish to put more restrictions on the separators ... I would be
suspicious of cases where dms[2] != dms[5]. What plausible separators
are there besides ":"? Why allow alphabetics? If there's a use case for
"23h59m59s", that would have to be handled separately. Note that
"06-12-31" could be a date, "12,34,56" could be CSV data.

Cheers,
John
 
J

John McMonagle

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]))
)

Have you considered time.strptime()?

BTW, your function, given "00:00:00" will return 0 -- you may well have
trouble distinguishing that from False (note that False == 0), without
resorting to ugliness like:

if result is False ...

Instead of returning False for some errors and letting int() raise an
exception for others, I would suggest raising ValueError yourself for
*all* invalid input.

You may wish to put more restrictions on the separators ... I would be
suspicious of cases where dms[2] != dms[5]. What plausible separators
are there besides ":"? Why allow alphabetics? If there's a use case for
"23h59m59s", that would have to be handled separately. Note that
"06-12-31" could be a date, "12,34,56" could be CSV data.

Cheers,
John


You may also want to look at the dateutil module (especially dateutil.parse).
 
M

Marc 'BlackJack' Rintsch

John Machin said:
You may wish to put more restrictions on the separators ... I would be
suspicious of cases where dms[2] != dms[5]. What plausible separators
are there besides ":"? Why allow alphabetics? If there's a use case for
"23h59m59s", that would have to be handled separately.

Looking at the subject I would expect to be able to give 76°04'54" as
argument. Hm, but degrees don't map directly to hours!?

Ciao,
Marc 'BlackJack' Rintsch
 
G

google0

John said:
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

Have you considered time.strptime()?

BTW, your function, given "00:00:00" will return 0 -- you may well have
trouble distinguishing that from False (note that False == 0), without
resorting to ugliness like:

if result is False ...

Instead of returning False for some errors and letting int() raise an
exception for others, I would suggest raising ValueError yourself for
*all* invalid input.

You may wish to put more restrictions on the separators ... I would be
suspicious of cases where dms[2] != dms[5]. What plausible separators
are there besides ":"? Why allow alphabetics? If there's a use case for
"23h59m59s", that would have to be handled separately. Note that
"06-12-31" could be a date, "12,34,56" could be CSV data.

Cheers,
John

Good point about 0/False. I don't think it would have bitten me in my
current program, given my expected (and filtered) inputs, but I might
have reused it in the future, and been bitten later.

I had looked at the time module, but apparently not long enough.
This does the trick:

def dms2int(dms):
int(time.mktime(time.strptime("2000-01-01 %s" % dms, "%Y-%m-%d
%H:%M:%S")))

I only need the minutes, but can work with seconds. The only downside
is that I'm hardcoding an arbitrary date, but I can deal with that.

Thanks for your help, John!
--dang
 
J

John Machin

John said:
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

Have you considered time.strptime()?

BTW, your function, given "00:00:00" will return 0 -- you may well have
trouble distinguishing that from False (note that False == 0), without
resorting to ugliness like:

if result is False ...

Instead of returning False for some errors and letting int() raise an
exception for others, I would suggest raising ValueError yourself for
*all* invalid input.

You may wish to put more restrictions on the separators ... I would be
suspicious of cases where dms[2] != dms[5]. What plausible separators
are there besides ":"? Why allow alphabetics? If there's a use case for
"23h59m59s", that would have to be handled separately. Note that
"06-12-31" could be a date, "12,34,56" could be CSV data.

Cheers,
John

Good point about 0/False. I don't think it would have bitten me in my
current program, given my expected (and filtered) inputs, but I might
have reused it in the future, and been bitten later.

The bigger pain would have been two types of error handling
(try/except) *AND* if result is False
I had looked at the time module, but apparently not long enough.
This does the trick:

def dms2int(dms):
int(time.mktime(time.strptime("2000-01-01 %s" % dms, "%Y-%m-%d
%H:%M:%S")))

I only need the minutes, but can work with seconds. The only downside
is that I'm hardcoding an arbitrary date, but I can deal with that.

That's a bit too elaborate. Python gives you the hard-coded date for
free -- then (you ingrate!) you ignore it, like this:

|>> import time
|>> dms = "23:48:59"
|>> t = time.strptime(dms, "%H:%M:%S")
|>> t
(1900, 1, 1, 23, 48, 59, 0, 1, -1)
|>> seconds = (t[3] * 60 + t[4]) * 60.0 + t[5]
|>> seconds
85739.0 # assuming you do want it as a float, not an int

Cheers,
John
 
P

Paul McGuire

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

In a froth of functionalism, here is my submission.

-- Paul


tests = """\
00:00:00
01:01:01
23:59:59
24:00:00
H1:00:00
12:34:56.789""".split("\n")

def time2secs(t,decimal=False):
if decimal:
tflds = map(float,t.split(":"))
else:
tflds = map(int,t.split(".")[0].split(":"))
nomorethan = lambda (a,maxa) : 0 <= a < maxa
if sum(map(nomorethan, zip(tflds,(24,60,60)))) == len(tflds):
return reduce(lambda a,b: a*60+b, tflds)
else:
raise ValueError("invalid time field value in '%s'" % str(t))

for tt in tests:
try:
print time2secs(tt)
print time2secs(tt,True)
except Exception,e:
print "%s: %s" % (e.__class__.__name__, e)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top