473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to convert a timedelta object to a string?

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
cv*********@mvista.com
Build and Release
MontaVista Software

Sep 14 '06 #1
6 30573
Carl J. Van Arsdall wrote:
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.

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '06 #2
tobiah wrote:
Carl J. Van Arsdall wrote:
>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.

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
cv*********@mvista.com
Build and Release
MontaVista Software

Sep 14 '06 #3
"Carl J. Van Arsdall" <cv*********@mvista.comwrote in message
news:ma**************************************@pyth on.org...
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:
>>startTime = datetime.timedelta(seconds=45,minutes=22,hours=10)
stopTime = datetime.timedelta(seconds=25,minutes=2,hours=4)
delta = startTime-stopTime
time.strftime("%H:%M:%S",time.gmtime(delta.secon ds))
'06:20:20'

-- Paul
Sep 14 '06 #4
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

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '06 #5
>>startTime = datetime.timedelta(seconds=45,minutes=22,hours=10)
stopTime = datetime.timedelta(seconds=25,minutes=2,hours=4)
delta = startTime-stopTime
time.strftime("%H:%M:%S",time.gmtime(delta.secon ds))
'06:20:20'

Which, alas, will lose any subsecond resolution:
>>str(datetime.timedelta(seconds=0.1))
'0:00:00.100000'

Skip
Sep 14 '06 #6
Paul McGuire wrote:
"Carl J. Van Arsdall" wrote:
>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.
[...]
From the Python console:
>>>startTime = datetime.timedelta(seconds=45,minutes=22,hours=10)
stopTime = datetime.timedelta(seconds=25,minutes=2,hours=4)
delta = startTime-stopTime
time.strftime("%H:%M:%S",time.gmtime(delta.seco nds))
'06:20:20'
On the other hand:
>>startTime = datetime.timedelta(hours=28)
stopTime = datetime.timedelta(hours=4)
delta = startTime-stopTime
time.strftime("%H:%M:%S",time.gmtime(delta.secon ds))
'00:00:00'
--
--Bryan
Sep 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
3
by: klynn | last post by:
I defined a session variable as an array using Session = new string; Later, in my code, I need to set it and sort it. I tried Session = "some string"; My error is "Cannot apply indexing with to...
2
by: UJ | last post by:
I have need to convert a text string (with formatting) to an image. This is so that I can resize the image to different sizes and the formatting stays exactly the same regardless of the size. I...
2
by: james | last post by:
I wrote values to the registry in a "MultiString" registry type. I now want to retrieve these into a string array. I use this: String LoadedObject = SUBKEY.GetValue(KeyNames.ToString()) but...
5
by: Lad | last post by:
Hello, what is the best /easest way how to get number of hours and minutes from a timedelta object? Let's say we have aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)...
15
by: Buddy Home | last post by:
Hello, I'm trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us...
3
by: silverburgh.meryl | last post by:
Hi, If I have a std string object , how to convert a STD string object to an integer? Thank you.
4
by: Lloyd Dupont | last post by:
to store user preference in our application we have an 'hand written' XML file. (as opposed to XmlSerializer written one, which crash sometimes, on some user's computer, for some unknown reason.....
2
by: manjitsarma | last post by:
In the following code ,I am getting the errors public abstract class Theme { private string sFrmBackGroundImage; private Color sFrmTransperncyColor; public object...
3
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.