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]))
) 6 7008 go*****@lazytwi nacres.net wrote:
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
On Wed, 2006-07-26 at 20:18 -0700, John Machin wrote:
go*****@lazytwi nacres.net wrote:
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) .
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
In <11************ **********@b28g 2000cwb.googleg roups.com>, John Machin
wrote:
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
John Machin wrote:
go*****@lazytwi nacres.net wrote:
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 go*****@lazytwi nacres.net wrote:
John Machin wrote:
go*****@lazytwi nacres.net wrote:
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(d ms, "%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
<go*****@lazytw inacres.netwrot e in message
news:11******** ************@h4 8g2000cwc.googl egroups.com...
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,dec imal=False):
if decimal:
tflds = map(float,t.spl it(":"))
else:
tflds = map(int,t.split (".")[0].split(":"))
nomorethan = lambda (a,maxa) : 0 <= a < maxa
if sum(map(nomoret han, zip(tflds,(24,6 0,60)))) == len(tflds):
return reduce(lambda a,b: a*60+b, tflds)
else:
raise ValueError("inv alid time field value in '%s'" % str(t))
for tt in tests:
try:
print time2secs(tt)
print time2secs(tt,Tr ue)
except Exception,e:
print "%s: %s" % (e.__class__.__ name__, e) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ken |
last post by:
hello,
I would to know if it is possible to call an object in a function within a
class.
Meaning , In a class, A function X calling onto a function Y, and function
Y we want one of the two calculation ( eg seconds , out of seconds and
minutes)
thanks ,
Ken
|
by: fred14214 |
last post by:
I need to be able to enter an elapsed time into a text
field. This time then needs to be converted into seconds
and stored in a field in some table.
The user will enter a time (format is h:minutes:seconds)
as such:
0:15:34 (0 hours, 15 minutes, 34 seconds -> 934 seconds total)
or as another example:
|
by: James P. |
last post by:
Hello,
In my Access report, I have a minutes field in the detail line. How
do I convert that minute to hour and minute. The problem for me now
is if I take the minutes, say 75 divide by 60, it gives me 1.25; or
90/60 = 1.50. These are not what the user wants.
They want to have hour and minutes, say 75 minutes, needs to convert
to 1.15 (1 hour 15 minutes); or 90 minutes to 1.30, and so on.
|
by: Penguin |
last post by:
At some long ago time Steve Jorgensen answered thus:
Subject: Re: How can I round a time?
Newsgroups: comp.databases.ms-access
Date: 1998/12/11
Access represents a date internally as a double and will convert
between
date/time and double automatically. The double value Access (or VB)
creates is based on 1 day = 1.0 and the fractional part represents a
|
by: Bore Biko |
last post by:
Dear,
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
Here s code compiled with gcc
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...
| |
by: DaveF |
last post by:
I need to do some calculations with lat long. Does anyone have an example
how to work with lat long so I don't loose values
--
David
|
by: simon |
last post by:
I have the name of a day like for example 'Mon'
Is there some function which returns the number of a weekDay if you have the
day name or I should write the select case statements?
If the day is 'Mon' then result should be 2.
Thank you,
Simon
|
by: Laguna |
last post by:
Hi,
I have an XML file in a single long string. How can I convert it into
the nicely indented format as displayed by Internet Explorer using
Python?
Thanks,
Laguna
Input XML file (one continuous string without newline):
|
by: dwasbig9 |
last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access
VBA. Using Access 2003).
I need to sum time, I've found through the groups archive an sql
extract that led me to this
SELECT Format(Sum(Table2.time),"Short Time") AS SumOftime
FROM Table2;
Which works fine but the article also said I would need a Function to
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |