strptime and microseconds

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Oct 18 16:54:09 EDT 2007


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")
py> ms = int(p2.ljust(6,'0')[:6])
py> d1.replace(microsecond=ms)
datetime.datetime(2007, 6, 19, 11, 53, 44, 25000)

--
Gabriel Genellina




More information about the Python-list mailing list