471,312 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

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

Hi,

I have a string in the following format:

"00:00:25.886411"

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.

Jul 24 '07 #1
3 2536
On 7/24/07, Robert Dailey wrote:
Hi,

I have a string in the following format:

"00:00:25.886411"

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.886411"
timesep = re.compile('[:.]')
datetime.time(*[int(i) for i in timesep.split(timestr)])
Jul 24 '07 #2
On 24 ec, 19:11, Robert Dailey <rcdai...@gmail.comwrote:
Hi,

I have a string in the following format:

"00:00:25.886411"

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.
http://pleac.sourceforge.net/pleac_p...sandtimes.html
the part "http://pleac.sourceforge.net/pleac_python/
datesandtimes.html"
HTH
Petr Jakes

Jul 24 '07 #3
En Tue, 24 Jul 2007 14:37:20 -0300, Miles <se*********@gmail.comescribió:
On 7/24/07, Robert Dailey wrote:
>I have a string in the following format:

"00:00:25.886411"

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.886411"
timesep = re.compile('[:.]')
datetime.time(*[int(i) for i in timesep.split(timestr)])
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.886"
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

Jul 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by andrew lowe | last post: by
26 posts views Thread by Reny J Joseph Thuthikattu | last post: by
11 posts views Thread by Cor Ligthert | last post: by
9 posts views Thread by Phil B | last post: by
9 posts views Thread by piersh | last post: by
reply views Thread by rosydwin | last post: by

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.