strptime and microseconds

mathieu mathieu.malaterre at gmail.com
Fri Oct 19 03:42:43 EDT 2007


On Oct 18, 10:54 pm, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
> On 18 oct, 13:46, mathieu <mathieu.malate... at gmail.com> wrote:
>
>
>
> > On Oct 18, 6:36 pm, mathieu <mathieu.malate... at gmail.com> wrote:
> > > >   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:
> > 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




More information about the Python-list mailing list