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

R

Robert Dailey

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

Miles

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)])
 
N

Neil Cerutti

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.

Consult the documentation about time.strptime (to start) and then
datetime.strptime (which refers back to the time.strptime docs,
in a rather unfortunate manner).
 
P

Petr Jakes

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

Ben Finney

Neil Cerutti said:
Consult the documentation about time.strptime (to start) and then
datetime.strptime (which refers back to the time.strptime docs,
in a rather unfortunate manner).

Unfortunately 'strptime' also only seems to parse the components as
integers:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/_strptime.py", line 334, in strptime
data_string[found.end():])
ValueError: unconverted data remains: .064371

The same thing happens with 'time.strptime'. So this isn't yet a
solution for the OP.
 
N

Neil Cerutti

Neil Cerutti said:
Consult the documentation about time.strptime (to start) and then
datetime.strptime (which refers back to the time.strptime docs,
in a rather unfortunate manner).

Unfortunately 'strptime' also only seems to parse the components as
integers:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/_strptime.py", line 334, in strptime
data_string[found.end():])
ValueError: unconverted data remains: .064371

The same thing happens with 'time.strptime'. So this isn't yet
a solution for the OP.

Bummer. Thanks for the info. I should consult the documentation,
too, it seems. ;)
 
G

Gabriel Genellina

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:

py> timestr = "00:00:25.886"
py> h, m, s = timestr.split(":")
py> if '.' in s:
.... s, us = s.split('.')
.... us = us[:6].ljust(6, '0')
.... else:
.... us = 0
....
py> datetime.time(int(h), int(m), int(s), int(us))
datetime.time(0, 0, 25, 886000)
 
J

Jay Loden

Ben said:
Neil Cerutti said:
Consult the documentation about time.strptime (to start) and then
datetime.strptime (which refers back to the time.strptime docs,
in a rather unfortunate manner).

Unfortunately 'strptime' also only seems to parse the components as
integers:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/_strptime.py", line 334, in strptime
data_string[found.end():])
ValueError: unconverted data remains: .064371

The same thing happens with 'time.strptime'. So this isn't yet a
solution for the OP.

How about something like this?
import time
import datetime

time_fmt = "%H:%M:%S"
timestamp = "00:00:25.886411"
stamp,msec = timestamp.split('.')
dtime = datetime.datetime(*(time.strptime(stamp, time_fmt)[0:6] + (int(msec),)))
dtime
datetime.datetime(1900, 1, 1, 0, 0, 25, 886411)


-Jay
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top