473,609 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.key s():
if resultsDict[key].stop != None:
parsedTime = timeRegex.match (resultsDict[key].start)
startDelta = datetime.timede lta(seconds=int (parsedTime.gro up(3)),
minutes=int(par sedTime.group(2 )), hours=int(parse dTime.group(1)) )
parsedTime = timeRegex.match (resultsDict[key].stop)
stopDelta = datetime.timede lta(seconds=int (parsedTime.gro up(3)),
minutes=int(par sedTime.group(2 )), hours=int(parse dTime.group(1)) )
duration = stopDelta - startDelta


Thanks,

-carl
--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Sep 14 '06 #1
6 30595
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*********@mvi sta.com
Build and Release
MontaVista Software

Sep 14 '06 #3
"Carl J. Van Arsdall" <cv*********@mv ista.comwrote in message
news:ma******** *************** *************** @python.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.timede lta(seconds=45, minutes=22,hour s=10)
stopTime = datetime.timede lta(seconds=25, minutes=2,hours =4)
delta = startTime-stopTime
time.strftime ("%H:%M:%S",tim e.gmtime(delta. seconds))
'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.timede lta(seconds=45, minutes=22,hour s=10)
stopTime = datetime.timede lta(seconds=25, minutes=2,hours =4)
delta = startTime-stopTime
time.strftime ("%H:%M:%S",tim e.gmtime(delta. seconds))
'06:20:20'

Which, alas, will lose any subsecond resolution:
>>str(datetime. timedelta(secon ds=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.timede lta(seconds=45, minutes=22,hour s=10)
stopTime = datetime.timede lta(seconds=25, minutes=2,hours =4)
delta = startTime-stopTime
time.strftim e("%H:%M:%S",ti me.gmtime(delta .seconds))
'06:20:20'
On the other hand:
>>startTime = datetime.timede lta(hours=28)
stopTime = datetime.timede lta(hours=4)
delta = startTime-stopTime
time.strftime ("%H:%M:%S",tim e.gmtime(delta. seconds))
'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
10516
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: objMailMessage.To = dtrRecipient; I'm not sure why I am getting this error. Below is the complete code I am working with.
3
4169
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 an expression of type 'object'. Then, later, I try to use it, and sort on it an get 'cannot convert from 'object' to System.array. what am I doing wrong here?
2
2623
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 actually want the font size to increase/decrease as the image grows/shrinks much the way text does in a flash object. Is there an easy way to do this programmatically? It needs to be done behind the scenes on my web site - user enters some text - I...
2
22209
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 this gives an error "Cannot implicitly convert type 'object' to 'string'. Which is fine, but what do I do? Googling, I saw this:
5
28435
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) bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000) so c=bb-aa will be datetime.timedelta(5, 6339, 437000)
15
12296
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 performance degrade. Does anyone know any better way to archive this which just not degrade performance. The code is written in C#. Thanks
3
2441
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
3312
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.. but I'm digressing) In this XML file I store object value, which I convert to string (when writting) and from string (when reading) with a couple of simple function which (should) do a reversible conversion (using TypeConverter) (functions...
2
3108
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 BackGroundImage
3
19846
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 to string object of type str, then pass to function(). please help me how to convert this
0
8083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
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, 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...
0
8547
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8224
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2535
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
1
1676
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1393
bsmnconsultancy
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...

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.