eem*_*ilk 1 python decimal datetime-format python-3.x
我有一个 Json 内容:
{
"Name": "startTime",
"Value": [
{
"Valid": true,
"Time": 43852.491953472221
}
]
}
Run Code Online (Sandbox Code Playgroud)
其中“时间”以小数表示 43852.491953472221 据我所知,表示自 1900 年 1 月 1 日以来的天数。
有没有快速的方法可以将Python中的时间转换为如下格式:
import time
print("Start time: ", time.ctime(time.time()))
Run Code Online (Sandbox Code Playgroud)
输出:
Start time: Wed Jan 22 14:10:50 2020
Run Code Online (Sandbox Code Playgroud)
使用一个datetime.timedelta()对象以及datetime.datetime()纪元的值:
from datetime import datetime, timedelta
EPOCH = datetime(1900, 1, 1) # midnight 1st January 1900
def from_ordinal(ordinal, _epoch=EPOCH):
return _epoch + timedelta(days=ordinal)
Run Code Online (Sandbox Code Playgroud)
演示:
>>> from_ordinal(43852.491953472221)
datetime.datetime(2020, 1, 24, 11, 48, 24, 780000)
Run Code Online (Sandbox Code Playgroud)
请注意,您给出的具体示例是未来 2 天(47 小时 20 分钟或左右)。
如果您的意思是今天,并且有足够的时间来创建您的帖子,那么您的时代解释不太正确。您可能有一个Excel 十进制时间戳值;纪元实际上是 1899-12-31 00:00:00,具有从 Lotus 1-2-3 继承的闰年 bug-as-feature,在这种情况下,对于等于或大于的任何值,您需要减去 1 60:
EXCEL_EPOCH0 = datetime(1899, 12, 31)
def from_excel_ordinal(ordinal, _epoch=EXCEL_EPOCH0):
if ordinal >= 60:
ordinal -= 1 # Excel / Lotus 1-2-3 leap year bug, 1900 is not a leap year!
# Excel doesn't support microseconds, ignore them as mere fp artefacts
return (_epoch + timedelta(days=ordinal)).replace(microsecond=0)
Run Code Online (Sandbox Code Playgroud)
这会产生:
>>> from_excel_ordinal(43852.491953472221)
datetime.datetime(2020, 1, 22, 11, 48, 24)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4719 次 |
| 最近记录: |