datetime.time() class - How to pass it a time string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Dailey

    datetime.time() class - How to pass it a time string?

    Hi,

    I have a string in the following format:

    "00:00:25.88641 1"

    I would like to pass this string into the datetime.time() class and
    have it parse the string and use the values. However, the __init__()
    method only takes integers (which means I'd be forced to parse the
    string myself). Does anyone know of a way I can make it use the
    string? Thanks.

  • Miles

    #2
    Re: datetime.time() class - How to pass it a time string?

    On 7/24/07, Robert Dailey wrote:
    Hi,
    >
    I have a string in the following format:
    >
    "00:00:25.88641 1"
    >
    I would like to pass this string into the datetime.time() class and
    have it parse the string and use the values. However, the __init__()
    method only takes integers (which means I'd be forced to parse the
    string myself). Does anyone know of a way I can make it use the
    string? Thanks.
    timestr = "00:00:25.88641 1"
    timesep = re.compile('[:.]')
    datetime.time(*[int(i) for i in timesep.split(t imestr)])

    Comment

    • Petr Jakes

      #3
      Re: datetime.time() class - How to pass it a time string?

      On 24 ec, 19:11, Robert Dailey <rcdai...@gmail .comwrote:
      Hi,
      >
      I have a string in the following format:
      >
      "00:00:25.88641 1"
      >
      I would like to pass this string into the datetime.time() class and
      have it parse the string and use the values. However, the __init__()
      method only takes integers (which means I'd be forced to parse the
      string myself). Does anyone know of a way I can make it use the
      string? Thanks.

      the part "http://pleac.sourcefor ge.net/pleac_python/
      datesandtimes.h tml"
      HTH
      Petr Jakes

      Comment

      • Gabriel Genellina

        #4
        Re: datetime.time() class - How to pass it a time string?

        En Tue, 24 Jul 2007 14:37:20 -0300, Miles <semanticist@gm ail.comescribió :
        On 7/24/07, Robert Dailey wrote:
        >I have a string in the following format:
        >>
        >"00:00:25.8864 11"
        >>
        >I would like to pass this string into the datetime.time() class and
        >have it parse the string and use the values. However, the __init__()
        >method only takes integers (which means I'd be forced to parse the
        >string myself). Does anyone know of a way I can make it use the
        >string? Thanks.
        >
        timestr = "00:00:25.88641 1"
        timesep = re.compile('[:.]')
        datetime.time(*[int(i) for i in timesep.split(t imestr)])
        That's OK if one can always guarantee the 6 digits after the decimal
        point; else a bit more work is required:

        pytimestr = "00:00:25.8 86"
        pyh, m, s = timestr.split(" :")
        pyif '.' in s:
        .... s, us = s.split('.')
        .... us = us[:6].ljust(6, '0')
        .... else:
        .... us = 0
        ....
        pydatetime.time (int(h), int(m), int(s), int(us))
        datetime.time(0 , 0, 25, 886000)

        --
        Gabriel Genellina

        Comment

        Working...