簡體   English   中英

Python使用`time.strftime`以格式化字符串顯示毫秒

[英]Python display milliseconds in formatted string using `time.strftime`

我正在嘗試將毫秒格式化為保留毫秒部分的格式化字符串。 這是我的代碼:

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'不正確嗎?

time.strftime(...)中提到的no指令將返回毫秒數。 不確定從何處獲得使用%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.gmtime會獲得秒數作為輸入參數。 結果,不考慮毫秒:

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

功能文檔中明確提到了它:

小數秒被忽略。

您可以輕松地使用 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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM