Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, strptime is skipping zeros in the millisecond section

I want to extract the timestamp from a string, but the milliseconds part is not being read properly

datetime.strptime('20130629110924095','%Y%m%d%H%M%S%f')

produces the following output

datetime.datetime(2013, 6, 29, 11, 9, 24, 95000)

instead of

datetime.datetime(2013, 6, 29, 11, 9, 24, 95)

to be clear: 95 milliseconds

What am I doing wrong?

like image 423
tokyoCoder Avatar asked Jun 24 '15 12:06

tokyoCoder


People also ask

How do I show milliseconds in Python?

strptime() function in python converts the string into DateTime objects. The strptime() is a class method that takes two arguments : string that should be converted to datetime object.

What does Strptime mean in Python?

Description. Python time method strptime() parses a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime().

How do you add milliseconds to a timestamp in Python?

Use the timedelta() class from the datetime module to add milliseconds to datetime, e.g. result = dt + timedelta(milliseconds=300) . The timedelta class can be passed a milliseconds argument and adds the specified number of milliseconds to the datetime.

What does datetime Strptime do in Python?

Python time strptime() function The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it.

How strptime () works in Python?

How strptime () works? This function takes two arguments, a string in which some time is given and a format code, to change the string into, the string is changed to the DateTime object as per the list of codes given below. 1, 2..

Why does the Python strptime function throw an error?

If you specify the abbreviates in the wrong order or use the wrong format, then the Python strptime function throws an error. from datetime import datetime as rdm dt_string = '31/12/18' dval = rdm.strptime (dt_string, '%d %m %y') print (dval)

Why does strptime() not work with the missing piece?

The missing piece was an extra space after the string which was causing a format mismatch. So the solution was to call strip () on the string before passing it to strptime (). Read the format strip syntax of strptime (). datetime objects are very handy and general operations such as comparison are inbuilt.

How to use function to produce milliseconds in Python?

To use this function to produce milliseconds in python %f is used in format code. Given below are some implementations. # object with milliseconds.. Writing code in comment?


2 Answers

The documentation says:

%f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available). When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right.

So the result you get is expected behaviour, your '095' is padded to '095000'.

like image 142
Bernhard Avatar answered Sep 26 '22 03:09

Bernhard


Microseconds consist of six digits. 95 milliseconds = 95000 microseconds. So to get datetime with 95 milliseconds like datetime.datetime(2013, 6, 29, 11, 9, 24, 95000) write:

datetime.strptime('20130629110924095000','%Y%m%d%H%M%S%f')
like image 32
Delimitry Avatar answered Sep 22 '22 03:09

Delimitry