How to convert a timedelta object to a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl J. Van Arsdall

    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
    cvanarsdall@mvi sta.com
    Build and Release
    MontaVista Software

  • tobiah

    #2
    Re: How to convert a timedelta object to a string?

    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

    Comment

    • Carl J. Van Arsdall

      #3
      Re: How to convert a timedelta object to a string?

      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
      cvanarsdall@mvi sta.com
      Build and Release
      MontaVista Software

      Comment

      • Paul McGuire

        #4
        Re: How to convert a timedelta object to a string?

        "Carl J. Van Arsdall" <cvanarsdall@mv ista.comwrote in message
        news:mailman.10 4.1158267229.10 491.python-list@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


        Comment

        • tobiah

          #5
          Re: How to convert a timedelta object to a string?

          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

          Comment

          • skip@pobox.com

            #6
            Re: How to convert a timedelta object to a string?

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

            Comment

            • Bryan Olson

              #7
              Re: How to convert a timedelta object to a string?

              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

              Comment

              Working...