https://github.com/davydovanton/relative_time
Micro lib without any dependency for getting relative time
https://github.com/davydovanton/relative_time
relative-time ruby
Last synced: 8 months ago
JSON representation
Micro lib without any dependency for getting relative time
- Host: GitHub
- URL: https://github.com/davydovanton/relative_time
- Owner: davydovanton
- License: mit
- Created: 2017-01-24T21:18:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-05-26T10:51:44.000Z (over 4 years ago)
- Last Synced: 2024-04-26T09:03:13.836Z (over 1 year ago)
- Topics: relative-time, ruby
- Language: Ruby
- Homepage:
- Size: 137 KB
- Stars: 22
- Watchers: 3
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# RelativeTime
[](https://travis-ci.org/davydovanton/relative_time) [](https://codeclimate.com/github/davydovanton/relative_time) [](https://codeclimate.com/github/davydovanton/relative_time/coverage)
Micro lib without any dependency for getting relative time.
## Motivation
I love rails but sometimes we need to create web apps (or CLI) without rails and AS. That's why I created `#time_ago_in_words` analog on pure ruby without any dependencies.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'relative_time'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install relative_time
## Usage
Just call `#in_words` method with any time object:
```ruby
RelativeTime.in_words(Date.today)
RelativeTime.in_words(Time.now) # => less than a minute
RelativeTime.in_words(Time.now - 1 * 60) # => a minute ago
RelativeTime.in_words(Time.now - 3 * 60) # => 3 minutes ago
RelativeTime.in_words(Time.now + 1 * 60) # => in a minute
RelativeTime.in_words(Time.now + 3 * 60) # => in 3 minutes
```
Also you can use second argument as a from date:
```ruby
date_from = Time.now - 60 * 60 # one hour ago
RelativeTime.in_words(Time.now, date_from) # => an hour ago
```
### Supported intervals
* minutes
* hours
* days
* weeks
* months
* years
### I18n
Relative time use [i18n](https://github.com/ruby-i18n/i18n) for pluralization and translation to other languages. For using specific locale just call `#in_words` with specific locale:
```ruby
RelativeTime.in_words(Time.now, locale: :ru) # => через 3 часа
RelativeTime.in_words(Time.now, date_from, locale: :ru) # => минуту назад
```
List of supported locales:
* `en`
* `id`
* `ru`
* `tr`
* `uk`
* `be`
* `pt`
* `fa`
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/davydovanton/relative_time. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).