strptime and microseconds

mathieu mathieu.malaterre at gmail.com
Thu Oct 18 18:46:16 CEST 2007


On Oct 18, 6:36 pm, mathieu <mathieu.malate... at gmail.com> wrote:
> On Oct 18, 6:00 pm, mathieu <mathieu.malate... at gmail.com> wrote:
>
>
>
> > 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


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







More information about the Python-list mailing list