小编典典
这取决于整数应编码的内容。您可以将日期转换为以前某个时间的毫秒数。人们通常将其粘贴到1970年1月1日上午12:00或1900等,然后以该点之后的整数毫秒为单位来度量时间。该datetime模块(或其他类似模块)将具有为您执行此操作的功能:例如,您可以使用int(datetime.datetime.utcnow().timestamp())。
如果要对年,月和日进行语义编码,一种方法是将这些分量乘以足以将它们并置在整数位数内的量级值:
2012-06-13-> 20120613 = 10,000 (2012)+ 100(6)+ 1 *(13)
def to_integer(dt_time):
return 10000*dt_time.year + 100*dt_time.month + dt_time.day
例如
In [1]: import datetime
In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def to_integer(dt_time):
: return 10000*dt_time.year + 100*dt_time.month + dt_time.day
: # Or take the appropriate chars from a string date representation.
:--
In [3]: to_integer(datetime.date(2012, 6, 13))
Out[3]: 20120613
如果您还需要分钟和秒,则只需根据需要添加更多数量级即可显示数字。</