0.0
No commit activity in last 3 years
No release in over 3 years
Returns a time span splitted in desired units given two timestamps.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

time_spanner

Returns a time span, split into desired units, given two timestamps.

Build Status Gem Version Code Climate Test Coverage Ruby Style Guide

Supported rubies

2.5.8 and above

Installation

Add this line to your application"s Gemfile:

gem "time_spanner"

And then execute:

$ bundle

Or install it yourself as:

$ gem install time_spanner

Usage

require "time_spanner"

from = Time.parse("2012-04-05 07:05:05")
to = Time.parse("3024-11-14 12:06:49")

TimeSpan.new(from, to)

returns:

{
  millenniums: 1,
  centuries: 0,
  decades:1,
  years: 2,
  months: 7,
  weeks: 1,
  days: 2,
  hours: 6,
  minutes: 1,
  seconds: 44,
  milliseconds: 0,
  microseconds: 0,
  nanoseconds: 0,
}

Specifying units

Define which time units should be calculated. Pass them within an Array as a third parameter.

Available units are:

:millenniums
:centuries
:decades
:years
:months
:weeks
:days
:hours
:minutes
:seconds
:milliseconds
:microseconds
:nanoseconds

If no units are supplied it defaults to all units.

Example

require "time_spanner"

from = Time.parse("2012-04-05 07:05:05")
to = Time.at(Time.parse("2012-04-05 07:10:12").to_r, 1243.345)

TimeSpan.new(from, to, [:seconds, :milliseconds, :microseconds, :nanoseconds])

returns:

{
  seconds: 307,
  milliseconds: 1,
  microseconds: 243,
  nanoseconds: 345,
}

You can specify units in any combination.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am "Add some feature")
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request