这是一个小的Python包,它使用字母符号将标准时间戳编码为缩写形式。
alphabetic-timestamp的Python项目详细描述
字母时间戳
这是一个小Python包,它使用字母符号将标准时间戳编码为短格式。在
安装
pipinstallalphabetic-timestamp
说明
用户可以使用两个基进行编码。它用另外两个不同的符号列表来编码。在
- 在
基准36
在 - 在
基准62
在
编码示例
此示例显示日期时间2020-01-01 20:20:20.002000的编码。意思是时间戳1577906420.002。在
Time Units | Base36 | Base62 |
---|---|---|
seconds | q3g0dw | 1IMJxy |
deciseconds | 78yg3uw | hdRlpu |
centiseconds | 20hkh2kw | 2MeBs6Q |
milliseconds | k4voqpsy | rMm2x6q |
基数36的符号
^{pr2}$基数62的符号
importalphabetic_timestampasatsprint(ats.base62.symbols)>>>0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
接口
这个例子显示了base36的接口。base62的接口相同。在
importdatetimeimportalphabetic_timestampasatsdt=datetime.datetime.now()ts=dt.timestamp()ats.base36.now(time_unit=ats.TimeUnit.seconds)ats.base36.from_datetime(dt,time_unit=ats.TimeUnit.seconds)ats.base36.from_timestamp(ts,time_unit=ats.TimeUnit.seconds)ats.base36.to_datetime("q67vhw",time_unit=ats.TimeUnit.seconds,time_zone=None)ats.base36.to_timestamp("q67vhw",time_unit=ats.TimeUnit.seconds)
注意:字符串“q67vhw”是编码时间戳的唯一示例。在
示例
此软件包与Python2.7和Python3.4+兼容。但是这些例子是用Python3.6编写的。在
编码和打印
importalphabetic_timestampasatsprint(ats.base36.now())# Current DT: 2020-02-24 18:34:44.349162>>>q67vhw
编码和解码
importdatetimeimportalphabetic_timestampasats# DATETIME -> ALPHABETIC_TIMESTAMP -> DATETIMEdt=datetime.datetime.now()alphabetic_ts_36=ats.base36.from_datetime(dt)decoded_ts_36=ats.base36.to_datetime(alphabetic_ts_36)alphabetic_ts_62=ats.base62.from_datetime(dt)decoded_ts_62=ats.base62.to_datetime(alphabetic_ts_62)# TIMESTAMP -> ALPHABETIC_TIMESTAMP -> TIMESTAMP ts=dt.timestamp()alphabetic_ts_36=ats.base36.from_timestamp(ts)decoded_ts_36=ats.base36.to_timestamp(alphabetic_ts_36)alphabetic_ts_62=ats.base62.from_timestamp(ts)decoded_ts_62=ats.base62.to_timestamp(alphabetic_ts_62)
时间单位
importdatetimeimportalphabetic_timestampasatsdt=datetime.datetime.now()# Set time unit for current timestampnow36_ts=ats.base36.now()# default: ats.TimeUnit.secondsnow36_ts=ats.base36.now(time_unit=ats.TimeUnit.seconds)# Set time unit for specific datetime and timestampalphabetic_ts_36=ats.base36.from_datetime(dt,time_unit=ats.TimeUnit.seconds)alphabetic_ts_36=ats.base36.from_timestamp(dt.timestamp(),time_unit=ats.TimeUnit.seconds)# Examples of available time unitsnow36_ts=ats.base36.now(time_unit=ats.TimeUnit.seconds)now36_ts=ats.base36.now(time_unit=ats.TimeUnit.deciseconds)now36_ts=ats.base36.now(time_unit=ats.TimeUnit.centiseconds)now36_ts=ats.base36.now(time_unit=ats.TimeUnit.milliseconds)
可能的问题
可能存在由bug in standard datetime module引起的问题。在
- 项目
标签: