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