Giter VIP home page Giter VIP logo

time_diff's Introduction

time_diff

Gem which calculates the difference between two time

<img src=“https://badge.fury.io/rb/time_diff.png” alt=“Gem Version” /> <img src=“https://travis-ci.org/abhidsm/time_diff.png” /> <img src=“https://codeclimate.com/github/abhidsm/time_diff.png” /> <img alt=“” border=“0” src=“https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif” />

Installation

gem install time_diff

Usage

require 'time_diff'

time_diff_components = Time.diff(start_date_time, end_date_time)

This will return the hash of time difference in terms of years, month, week, day, hour, minute and second.

You can use the difference like:

time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]

Formatted Time difference

%y - year

%M - month

%w - week

%d - day

%H - hour

%N - minute

%S - second

%h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours)

%m - minute (without adding 'minute' text)

%s - second (without adding 'second' text)

By default the format is

'%y, %M, %w, %d and %h:%m:%s'

this will return

'1 year, 2 months, 3 weeks, 4 days and 12:05:52'.

You will get the result from the output hash, time_diff_components

You can pass your own format as third parameter to this function.

If you give ‘%d %h’ as the third parameter to the Time.diff() method, then the difference(time_diff_components) will be calculated only in days and hours.

Examples

> Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
=> {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}
> Time.diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s')
=> {:year => 1, :month => 0, :week => 0, :day => 0, :hour => 18, :minute => 0, :second => 30, :diff => '1 year and 18:00:30'}
> Time.diff(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%H %N %S')
=> {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30, :diff => '24 hours 0 minute 30 seconds'}

i18n support

Add translations for day, days, week, weeks, year, hour, hours, minute, minutes, second, and seconds in your YAML file. For eg:

en:
  day: divasam
  days: divasangal

Contributing to time_diff

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 abhidsm. See LICENSE.txt for further details.

time_diff's People

Contributors

abhidsm avatar

Watchers

 avatar  avatar

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.