Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string to time with decimal seconds

Say I have a string with format HHMMSS.SS how do I convert this to a time object?

This is how I thought you would do it:

import time
time.strptime(timestring, '%H%M%S')

However%S does not take take into account fractions of seconds according to the time documentation.

like image 735
Richard Avatar asked Jan 25 '13 18:01

Richard


1 Answers

You will have to use %f

time.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')

Use of datetime will be right

>>> from datetime import datetime
>>> a = datetime.strptime('26/01/12 23:50:32.123', '%d/%m/%y %H:%M:%S.%f')
>>> a.microsecond
like image 98
ganesh Avatar answered Nov 15 '22 16:11

ganesh