strptime and microseconds

M

mathieu

Hi there,

I am trying to use strptime to parse my microseconds but I was not
able the documentation for it. The only list I found was:

http://docs.python.org/lib/module-time.html

So I can get seconds with %S, but nowhere is there a microsecond
symbol...

Thanks for pointer to doc,
-Mathieu


s1 = "20070619"
s2 = "150348.62"
s = s1+s2
d = datetime(*strptime(s, "%Y%m%d%H%M%S.%?"))
 
M

mathieu

Hi there,

I am trying to use strptime to parse my microseconds but I was not
able the documentation for it. The only list I found was:

http://docs.python.org/lib/module-time.html

So I can get seconds with %S, but nowhere is there a microsecond
symbol...

Thanks for pointer to doc,
-Mathieu

s1 = "20070619"
s2 = "150348.62"
s = s1+s2
d = datetime(*strptime(s, "%Y%m%d%H%M%S.%?"))


Getting closer...

s1 = "20070619"
s2 = "115344.51"
s3 = "115445.123456"

ms2 = eval(s2) % 1
mms2 = int(ms2 * 1000000 + 0.5)
ms3 = eval(s3) % 1
mms3 = int(ms3 * 1000000 + 0.5)

s = s1 + s2
d1 = datetime(*strptime(s[:12], "%Y%m%d%H%M%S")[0:6])
d1.replace(microsecond = mms2)
#print d1.microsecond

s = s1 + s3
d2 = datetime(*strptime(s[:12], "%Y%m%d%H%M%S")[0:6])
d2.replace(microsecond = mms3)
#print d2.microsecond

d = d2 - d1
print d.seconds
print d.microseconds

why would d.microseconds be 0 ??

Thanks,
-Mathieu
 
M

mathieu

Hi there,
I am trying to use strptime to parse my microseconds but I was not
able the documentation for it. The only list I found was:

So I can get seconds with %S, but nowhere is there a microsecond
symbol...
Thanks for pointer to doc,
-Mathieu
s1 = "20070619"
s2 = "150348.62"
s = s1+s2
d = datetime(*strptime(s, "%Y%m%d%H%M%S.%?"))

Getting closer...

s1 = "20070619"
s2 = "115344.51"
s3 = "115445.123456"

ms2 = eval(s2) % 1
mms2 = int(ms2 * 1000000 + 0.5)
ms3 = eval(s3) % 1
mms3 = int(ms3 * 1000000 + 0.5)

s = s1 + s2
d1 = datetime(*strptime(s[:12], "%Y%m%d%H%M%S")[0:6])
d1.replace(microsecond = mms2)
#print d1.microsecond

s = s1 + s3
d2 = datetime(*strptime(s[:12], "%Y%m%d%H%M%S")[0:6])
d2.replace(microsecond = mms3)
#print d2.microsecond

d = d2 - d1
print d.seconds
print d.microseconds

why would d.microseconds be 0 ??

Thanks,
-Mathieu


D'oh !

Ok final version is simply:

s1 = "20070619"
s2 = "115344.51"
s3 = "115446.123456"

ms2 = eval(s2) % 1
mms2 = int(ms2 * 1000000 + 0.5)
ms3 = eval(s3) % 1
mms3 = int(ms3 * 1000000 + 0.5)

s = s1 + s2
d1 = datetime(*strptime(s[:14], "%Y%m%d%H%M%S")[0:6])
d1 = d1.replace(microsecond = mms2)
#
s = s1 + s3
d2 = datetime(*strptime(s[:14], "%Y%m%d%H%M%S")[0:6])
d2 = d2.replace(microsecond = mms3)
#
d = d2 - d1
print d.seconds
print d.microseconds

sorry for the noise :))
-Mathieu
 
G

Gabriel Genellina

Ok final version is simply:

s1 = "20070619"
s2 = "115344.51"
s3 = "115446.123456"

ms2 = eval(s2) % 1
mms2 = int(ms2 * 1000000 + 0.5)
ms3 = eval(s3) % 1
mms3 = int(ms3 * 1000000 + 0.5)

s = s1 + s2
d1 = datetime(*strptime(s[:14], "%Y%m%d%H%M%S")[0:6])
d1 = d1.replace(microsecond = mms2)

What about this:

py> import datetime
py> s1 = "20070619 115344.025"
py> p1, p2 = s1.split(".", 1)
py> d1 = datetime.datetime.strptime(p1, "%Y%m%d %H%M%S")
py> ms = int(p2.ljust(6,'0')[:6])
py> d1.replace(microsecond=ms)
datetime.datetime(2007, 6, 19, 11, 53, 44, 25000)
 
M

mathieu

Ok final version is simply:
s1 = "20070619"
s2 = "115344.51"
s3 = "115446.123456"
ms2 = eval(s2) % 1
mms2 = int(ms2 * 1000000 + 0.5)
ms3 = eval(s3) % 1
mms3 = int(ms3 * 1000000 + 0.5)
s = s1 + s2
d1 = datetime(*strptime(s[:14], "%Y%m%d%H%M%S")[0:6])
d1 = d1.replace(microsecond = mms2)

What about this:

py> import datetime
py> s1 = "20070619 115344.025"
py> p1, p2 = s1.split(".", 1)
py> d1 = datetime.datetime.strptime(p1, "%Y%m%d %H%M%S")

python2.3:
from time import strptime
py> ms = int(p2.ljust(6,'0')[:6])

ljust padds with space only in python 2.3. But thanks anyway your
solution is much cleaner !

-Mathieu
 

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
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top