人类可读时间增量
ago的Python项目详细描述
什么是人类可读的时间增量?
ago.py生成可自定义的可读时间增量,例如:
测试过去时:
Russell commented 1 year, 127 days, 16 hours ago You replied 1 year, 127 days ago
测试将来时:
Program will shutdown in 2 days, 3 hours, 27 minutes Job will run 2 days, 3 hours from now
如何安装
有多种方法可以安装此软件包。
您可以运行此特别命令:
pip install ago
或者在您的 setuptools-兼容项目的setup.py文件。
如何使用
AGO模块具有三个功能:
- 人
- Delta2dict
- 从主题获取增量
你只需要担心human。
以下是所有可用的参数和默认值:
human(subject, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False):
- 主题
- 一个DateTime、TimeDelta或Timestamp(整数/浮点)对象,使其成为人类可读的对象
- 精度(默认值2):
- 所需的单位精度量
- 过去时(默认为{}ago'):
- 用于过去时间增量的格式字符串
- 将来时(默认为{}'):
- 用于未来时间增量的格式字符串
- 缩写(默认为false):
- 布尔到缩写单位
下面是一个如何使用human的示例:
from ago import human from ago import delta2dict from datetime import datetime from datetime import timedelta # pretend this was stored in database db_date = datetime(year=2010, month=5, day=4, hour=6, minute=54, second=33, microsecond=4000) # to find out how long ago, use the human function print 'Created ' + human( db_date ) # optionally pass a precision print 'Created ' + human( db_date, 3 ) print 'Created ' + human( db_date, 6 )
我们还支持将来的日期和时间:
PRESENT = datetime.now() PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, ms FUTURE = PRESENT + timedelta( 2, 12447, 963 ) # days, secs, ms print human( FUTURE )
过去时态和将来时态关键字参数示例:
output1 = human( PAST, past_tense = 'titanic sunk {0} ago', future_tense = 'titanic will sink in {0} from now' ) output2 = human( FUTURE, past_tense = 'titanic sunk {0} ago', future_tense = 'titanic will sink in {0} from now' ) print output1 # titanic sunk 1 year, 127 days ago print output2 # titanic will sink in 2 days, 3 hours from now
需要更多的例子吗?
你应该看看test_ago.py
我该怎么感谢你?
你应该在twitter上关注我http://twitter.com/russellbal
许可证
公共域