By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

How to parse this timestamp?

P: n/a
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)

Mar 12 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 <mi*********@gmail.com>
http://pythonwise.blogspot.com
Mar 12 '08 #2

P: n/a
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
Mar 12 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.