10

I am trying to format milliseconds to formatted string preserving the milliseconds part. Here's my code:

import time

time.strftime('%Y-%m-%d %H:%M:%S:%f', time.gmtime(1515694048121/1000.0))

where 1515694048121 is a time in milliseconds. It returns me:

2018-01-11 18:07:28:f

As we can see, the millisecond portion of time is not returned.

What is correct format to include millisecond portion of time? Is '%Y-%m-%d %H:%M:%S:%f' not correct ?

2
  • Fast solution to this problem was posted here -- stackoverflow.com/a/57395466/2107205 Commented Aug 7, 2019 at 13:22
  • that solution is using datetime. This question is about the time library. Commented Apr 7, 2024 at 16:54

3 Answers 3

10

There is the no directive mentioned in time.strftime(...) that will return you the milliseconds. Not sure from where you got the reference to use %f. In fact time.gmtime(...) holds the precision only upto seconds.

As a hack, in order to achieve this, you may explicitly format your string by preserving your milli second value as:

>>> import time

>>> time_in_ms = 1515694048121
>>> time.strftime('%Y-%m-%d %H:%M:%S:{}'.format(time_in_ms%1000), time.gmtime(time_in_ms/1000.0))
'2018-01-11 18:07:28:121'

Here's the list of valid directives:

+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| Directive |                                                                                                   Meaning                                                                                                   | Notes |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| %a        | Locale’s abbreviated weekday name.                                                                                                                                                                          |       |
| %A        | Locale’s full weekday name.                                                                                                                                                                                 |       |
| %b        | Locale’s abbreviated month name.                                                                                                                                                                            |       |
| %B        | Locale’s full month name.                                                                                                                                                                                   |       |
| %c        | Locale’s appropriate date and time representation.                                                                                                                                                          |       |
| %d        | Day of the month as a decimal number [01,31].                                                                                                                                                               |       |
| %H        | Hour (24-hour clock) as a decimal number [00,23].                                                                                                                                                           |       |
| %I        | Hour (12-hour clock) as a decimal number [01,12].                                                                                                                                                           |       |
| %j        | Day of the year as a decimal number [001,366].                                                                                                                                                              |       |
| %m        | Month as a decimal number [01,12].                                                                                                                                                                          |       |
| %M        | Minute as a decimal number [00,59].                                                                                                                                                                         |       |
| %p        | Locale’s equivalent of either AM or PM.                                                                                                                                                                     | (1)   |
| %S        | Second as a decimal number [00,61].                                                                                                                                                                         | (2)   |
| %U        | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.                                | (3)   |
| %w        | Weekday as a decimal number [0(Sunday),6].                                                                                                                                                                  |       |
| %W        | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.                                | (3)   |
| %x        | Locale’s appropriate date representation.                                                                                                                                                                   |       |
| %X        | Locale’s appropriate time representation.                                                                                                                                                                   |       |
| %y        | Year without century as a decimal number [00,99].                                                                                                                                                           |       |
| %Y        | Year with century as a decimal number.                                                                                                                                                                      |       |
| %z        | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |       |
| %Z        | Time zone name (no characters if no time zone exists).                                                                                                                                                      |       |
| %%        |                                                                                                                                                                                                             |       |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
Sign up to request clarification or add additional context in comments.

1 Comment

Where can I pull time_in_ms value externally?
5

You can easily use the datetime library to format microseconds using %f.

│    >>> from datetime import datetime

     >>> datetime.utcnow().strftime("%Y%m%d-%H:%M:%S")
     '20211018-16:02:38'
    
     >>> datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")
     '20211018-16:02:38.986417'

Comments

2

In fact time.gmtime gets a number of seconds as input parameter. As a consequence, the milliseconds are not taken into account:

>>> import time
>>> time.gmtime(1515694048121/1000.0) == time.gmtime(1515694048000/1000.0)
True

The function documentation clearly mentions it:

Fractions of a second are ignored.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.