使用`time.strftime`在Python中显示毫秒的格式化字符串

7
我正在尝试将毫秒格式化为保留毫秒部分的格式化字符串。以下是我的代码:
import time

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

其中1515694048121是以毫秒为单位的时间。 它返回:

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

我们可以看到,毫秒部分的时间没有被返回。

正确的时间格式应该如何包含毫秒部分? '%Y-%m-%d %H:%M:%S:%f'不正确吗?


这个问题的快速解决方案已经在这里发布了--https://dev59.com/SWsz5IYBdhLWcg3w5ME0#57395466 - mato
3个回答

7
time.strftime(...) 中没有提到任何指令可以返回毫秒。不确定你从哪里得到使用 %f 的参考。实际上,time.gmtime(...) 只精确到秒。

为了达到这个目的,您可以通过保留毫秒值来显式地格式化字符串:

>>> 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'

以下是有效指令的列表:

+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+
| 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).                                                                                                                                                      |       |
| %%        |                                                                                                                                                                                                             |       |
+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+

我可以从哪里外部获取 time_in_ms 值? - alper

4
您可以轻松使用datetime库,使用%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'

1

实际上,time.gmtime 的输入参数是秒数。因此,毫秒不会被考虑在内:

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

函数文档清楚地指出:

忽略秒的小数部分。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接