Skip to content

abhidsm/time_diff

Repository files navigation

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.

About

Gem which calculates the difference between two times

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages