| 1 |
timeago |
| 2 |
======= |
| 3 |
|
| 4 |
A very simple python lib, used to format datetime with ``*** time ago`` |
| 5 |
statement. Javascript version here. `timeago.js`_. |
| 6 |
|
| 7 |
|Build Status| |PyPi Status| |Python Versions| |
| 8 |
|
| 9 |
Such as: |
| 10 |
|
| 11 |
:: |
| 12 |
|
| 13 |
just now |
| 14 |
12 seconds ago |
| 15 |
3 minutes ago |
| 16 |
2 hours ago |
| 17 |
24 days ago |
| 18 |
6 months ago |
| 19 |
2 years ago |
| 20 |
|
| 21 |
in 12 seconds |
| 22 |
in 3 minutes |
| 23 |
in 2 hours |
| 24 |
in 24 days |
| 25 |
in 6 months |
| 26 |
in 2 years |
| 27 |
|
| 28 |
For other languages see below. |
| 29 |
|
| 30 |
Install |
| 31 |
------- |
| 32 |
|
| 33 |
.. code:: sh |
| 34 |
|
| 35 |
pip install timeago |
| 36 |
|
| 37 |
.. _usage--example: |
| 38 |
|
| 39 |
Usage & Example |
| 40 |
--------------- |
| 41 |
|
| 42 |
.. code:: py |
| 43 |
|
| 44 |
# -*- coding: utf-8 -*- |
| 45 |
import timeago, datetime |
| 46 |
|
| 47 |
now = datetime.datetime.now() + datetime.timedelta(seconds = 60 * 3.4) |
| 48 |
|
| 49 |
date = datetime.datetime.now() |
| 50 |
|
| 51 |
# locale |
| 52 |
print (timeago.format(date, now, 'zh_CN')) # will print `3分钟前` |
| 53 |
|
| 54 |
# input datetime |
| 55 |
print (timeago.format(date, now)) # will print 3 minutes ago |
| 56 |
|
| 57 |
# input timedelta |
| 58 |
print (timeago.format(datetime.timedelta(seconds = 60 * 3.4))) # will print 3 minutes ago |
| 59 |
|
| 60 |
# input date, auto add time(0, 0, 0) |
| 61 |
print (timeago.format(datetime.date(2016, 5, 27), now)) |
| 62 |
|
| 63 |
# input datetime formatted string |
| 64 |
print (timeago.format('2016-05-27 12:12:03', '2016-05-27 12:12:12')) # will print just now |
| 65 |
|
| 66 |
# inverse two parameters |
| 67 |
print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print a while |
| 68 |
|
| 69 |
.. _method--parameter: |
| 70 |
|
| 71 |
Method & Parameter |
| 72 |
------------------ |
| 73 |
|
| 74 |
only one API ``format``. |
| 75 |
|
| 76 |
Three parameters of method ``format``: |
| 77 |
|
| 78 |
- ``date``: the parameter which will be formatted, must be instance |
| 79 |
of ``datetime`` / ``timedelta`` or datetime formatted string. |
| 80 |
- ``now``: reference time, must be instance of ``datetime`` or |
| 81 |
datetime formatted string. |
| 82 |
- ``locale``: the locale code, default ``en``. |
| 83 |
|
| 84 |
Locale |
| 85 |
------ |
| 86 |
|
| 87 |
At the time we're speaking, `following locale`_ are available: |
| 88 |
|
| 89 |
- ``ar`` |
| 90 |
- ``bg`` |
| 91 |
- ``ca`` |
| 92 |
- ``da`` |
| 93 |
- ``de`` |
| 94 |
- ``el`` |
| 95 |
- ``en`` |
| 96 |
- ``en_short`` |
| 97 |
- ``es`` |
| 98 |
- ``eu`` |
| 99 |
- ``fa_IR`` |
| 100 |
- ``fi`` |
| 101 |
- ``fr`` |
| 102 |
- ``gl`` |
| 103 |
- ``guj_IN`` |
| 104 |
- ``he`` |
| 105 |
- ``hu`` |
| 106 |
- ``in_BG`` |
| 107 |
- ``in_HI`` |
| 108 |
- ``in_ID`` |
| 109 |
- ``is`` |
| 110 |
- ``it`` |
| 111 |
- ``ja`` |
| 112 |
- ``ko`` |
| 113 |
- ``lt`` |
| 114 |
- ``ml`` |
| 115 |
- ``my`` |
| 116 |
- ``nb_NO`` |
| 117 |
- ``nl`` |
| 118 |
- ``nn_NO`` |
| 119 |
- ``pl`` |
| 120 |
- ``pt_PT`` |
| 121 |
- ``pt_BR`` |
| 122 |
- ``ro`` |
| 123 |
- ``ru`` |
| 124 |
- ``sk`` |
| 125 |
- ``sv_SE`` |
| 126 |
- ``ta`` |
| 127 |
- ``th`` |
| 128 |
- ``tr`` |
| 129 |
- ``uk`` |
| 130 |
- ``vi`` |
| 131 |
- ``zh_CN`` |
| 132 |
- ``zh_TW`` |
| 133 |
|
| 134 |
Localization |
| 135 |
------------ |
| 136 |
|
| 137 |
1. Fork the project |
| 138 |
2. Create a locale python script called ``[name_of_your_locale].py`` |
| 139 |
following the existing other locales. |
| 140 |
3. Add the name of your locale in the Readme to keep it updated |
| 141 |
(**alphabetically**). |
| 142 |
4. Add test case following the `english model`_ |
| 143 |
5. Create the Pull Request. |
| 144 |
|
| 145 |
Notes |
| 146 |
~~~~~ |
| 147 |
|
| 148 |
For complicated plurals, you can take example on the PL (Polish) locale |
| 149 |
`here`_ |
| 150 |
|
| 151 |
.. _timeago.js: https://github.com/hustcc/timeago.js |
| 152 |
.. _following locale: https://github.com/hustcc/timeago/tree/master/src/timeago/locales |
| 153 |
.. _english model: https://github.com/hustcc/timeago/tree/master/test/testcase.py#L50 |
| 154 |
.. _here: https://github.com/hustcc/timeago/tree/master/src/timeago/locales/pl.py |
| 155 |
|
| 156 |
.. |Build Status| image:: https://travis-ci.org/hustcc/timeago.svg?branch=master |
| 157 |
:target: https://travis-ci.org/hustcc/timeago |
| 158 |
.. |PyPi Status| image:: https://img.shields.io/pypi/v/timeago.svg |
| 159 |
:target: https://pypi.python.org/pypi/timeago |
| 160 |
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/timeago.svg |
| 161 |
:target: https://pypi.python.org/pypi/timeago |