How to convert a timedelta object to a string?

  • Thread starter Carl J. Van Arsdall
  • Start date
C

Carl J. Van Arsdall

Basically I used the datetime module and timedelta objects to calculate
a difference between two times. Now I'm trying to figure out how I make
that time delta a string HH:MM:SS to show elapsed time. I've spent tons
of time looking at the module's documentation but I'm not seeing how
those methods will help me. Can anyone help me with this?

Here's what I'm trying to do, assum that resultsDict is a dictionary of
objects that have various pieces of information including a start time
and a stop time that are strings which I extracted from a file in an
earlier part of the program. I use regEx to split up the hours,
minutes, and seconds and create timedelta objects, which I then subtract
to get the different. What I need is to get the value in duration as
HH:MM:SS as a string:


#There's lots of other code above this, but I just need help with this part
timeRegex = re.compile(r'(\d\d):(\d\d):(\d\d)')
for key in resultsDict.keys():
if resultsDict[key].stop != None:
parsedTime = timeRegex.match(resultsDict[key].start)
startDelta = datetime.timedelta(seconds=int(parsedTime.group(3)),
minutes=int(parsedTime.group(2)), hours=int(parsedTime.group(1)))
parsedTime = timeRegex.match(resultsDict[key].stop)
stopDelta = datetime.timedelta(seconds=int(parsedTime.group(3)),
minutes=int(parsedTime.group(2)), hours=int(parsedTime.group(1)))
duration = stopDelta - startDelta






Thanks,

-carl


--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
T

tobiah

Carl said:
Basically I used the datetime module and timedelta objects to calculate
a difference between two times. Now I'm trying to figure out how I make
that time delta a string HH:MM:SS

Oddly enough, str(tdobject) does what you want.
 
C

Carl J. Van Arsdall

tobiah said:
Oddly enough, str(tdobject) does what you want.
Well, kinda, although I can work with this, str(tdobject) returns a
string that looks like:

-1 day, 7:34:32



Granted, I can parse that, I was looking for a way to just get the
actual duration. But for now I'll just parse the string, thanks.

-c



--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
P

Paul McGuire

Carl J. Van Arsdall said:
Basically I used the datetime module and timedelta objects to calculate a
difference between two times. Now I'm trying to figure out how I make
that time delta a string HH:MM:SS to show elapsed time. I've spent tons
of time looking at the module's documentation but I'm not seeing how those
methods will help me. Can anyone help me with this?
Here's what I'm trying to do, assum that resultsDict is a dictionary of
objects that have various pieces of information including a start time and
a stop time that are strings which I extracted from a file in an earlier
part of the program. I use regEx to split up the hours, minutes, and
seconds and create timedelta objects, which I then subtract to get the
different. What I need is to get the value in duration as HH:MM:SS as a
string:

From the Python console:
'06:20:20'

-- Paul
 
T

tobiah

Well, kinda, although I can work with this, str(tdobject) returns a
string that looks like:

-1 day, 7:34:32

Oh yeah. I had tried it on a positive timedelta, which does give HH:MM:SS
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top