Code Monkey home page Code Monkey logo

relative_time's Introduction

RelativeTime

Build Status Code Climate Test 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:

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:

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:

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 for pluralization and translation to other languages. For using specific locale just call #in_words with specific locale:

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 code of conduct.

License

The gem is available as open source under the terms of the MIT License.

relative_time's People

Contributors

agungsetiawan avatar davydovanton avatar dylanwolff avatar jsantos avatar moofkit avatar nbulaj avatar ptico avatar sinankeskin avatar vanadium23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

relative_time's Issues

Error if the second argument isn't instance of Time

Hello!

Here conversion to_time using only for the first argument, but isn't for the second.

So, this code:

RelativeTime.in_words(DateTime.new(2017, 1, 15, 11, 0), DateTime.new(2017, 1, 18, 15, 0))

…raises this exception:

TypeError: expected numeric
from /home/alex/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/relative_time-1.0.0/lib/relative_time/in_words.rb:4:in `-'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.