How to parse this timestamp?

Discussion in 'Python' started by gnu.gcc.help, Mar 12, 2008.

  1. gnu.gcc.help

    gnu.gcc.help Guest

    I've got timestamps in a file that look like:

    [19-Aug-2007 07:38:43+216ms NZST]

    How can I parse them? I don't see any way to build a strftime()
    format string that can handle the +216ms part. The best I can see is
    tearing it all apart with a regex, but I'm trying to avoid that pain
    if I can.

    (PS: I have no clue why google groups thinks it should put
    "gnu.gcc.help" on the from line)
     
    gnu.gcc.help, Mar 12, 2008
    #1
    1. Advertising

  2. gnu.gcc.help

    Miki Guest

    Hello,

    > [19-Aug-2007 07:38:43+216ms NZST]
    >
    > How can I parse them?  I don't see any way to build a strftime()
    > format string that can handle the +216ms part. The best I can see is
    > tearing it all apart with a regex, but I'm trying to avoid that pain
    > if I can.
    >
    > (PS: I have no clue why google groups thinks it should put
    > "gnu.gcc.help" on the from line)

    Just zap the end and use time.strptime:
    >>> s = '19-Aug-2007 07:38:43+216ms NZST'
    >>> strptime(re.sub("\+\d{3}ms [A-Z]{4}", "", s), "%d-%b-%Y %H:%M:%S")

    (2007, 8, 19, 7, 38, 43, 6, 231, -1)
    >>>


    HTH,
    --
    Miki <>
    http://pythonwise.blogspot.com
     
    Miki, Mar 12, 2008
    #2
    1. Advertising

  3. gnu.gcc.help schrieb:
    > I've got timestamps in a file that look like:
    >
    > [19-Aug-2007 07:38:43+216ms NZST]
    >
    > How can I parse them? I don't see any way to build a strftime()
    > format string that can handle the +216ms part. The best I can see is
    > tearing it all apart with a regex, but I'm trying to avoid that pain
    > if I can.
    >
    > (PS: I have no clue why google groups thinks it should put
    > "gnu.gcc.help" on the from line)
    >

    Then don't use the regexes. Use string.split to separate the string on
    the +, then parse the left part with strptime, and usp pytz and
    datetime.timedelta to do the rest.

    Diez
     
    Diez B. Roggisch, Mar 12, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manzoorul Hassan

    Convert to NTP Timestamp

    Manzoorul Hassan, Mar 11, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,723
    Jim Gibson
    Mar 11, 2005
  2. Shawn

    displaying sybase timestamp

    Shawn, Jan 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,155
    Shawn
    Jan 27, 2004
  3. Reiner Merz

    Parse timestamp string

    Reiner Merz, Sep 9, 2003, in forum: C Programming
    Replies:
    11
    Views:
    1,009
    Kevin Easton
    Sep 24, 2003
  4. Replies:
    19
    Views:
    1,143
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    192
    7stud --
    Feb 20, 2008
Loading...

Share This Page