Skip to content

Returns a time span splitted in desired units given two timestamps.

License

Notifications You must be signed in to change notification settings

shluboto/time_spanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Returns a time span splitted in desired units given two timestamps.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages