Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Express intervals between any two times
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/time
test fix #eql?, #==
.gitignore
.travis.yml
Gemfile
History.rdoc
LICENSE
Manifest.txt
README.rdoc
Rakefile
time-unit.gemspec

README.rdoc

time-unit

Description

Express intervals between any two times

Usage

Setup

require 'time/unit'

Construction

sec_30 = Time::Unit.new(30) #=> 30sec
# aliased
day_1 = Time.Unit(1, :day) #=> 1day

Get other units

sec_30.millisecond #=> 30000
sec_30.second      #=> 30
sec_30.minute      #=> 1/2
sec_30.hour        #=> 1/120
sec_30.day         #=> 1/2880

Extend API

last = Time.now
sleep 1
Time.now - last #=> 1.0(Float)

require 'time/unit/import'
Time.now - last #=> 1sec(Time::Unit)

Construct with unit name

min_55_and_more = Time.Unit(3333333, :msec) #=> 55min 33sec 333msec

Verbose formatable

min_55_and_more.to_s(true) #=> 55minutes 33seconds 333milliseconds
Time.Unit(99, :min).to_s(true) #=> 1hour 39minutes
(Time.Unit(99, :min) + Time.Unit(22, :min)).to_s(true) #=> 2hours 1minute

Calculatable

sec_30 + min_55_and_more #=> 56min 3sec 333msec
min_55_and_more - sec_30 #=> 55min 3sec 333msec
Time.now + sec_30 #=> (increased)
Time.now - sec_30 #=> (decreased)
# cast arguments to seconds when Integer or Rational
Time.Unit(1, :hour) + 330 #=> 1hour 5min 30sec

Max unit is day(not mounth or year)

hour_900 = Time.Unit(900, :hour) #=> 37day 12hour

Sort and remove empty field when display

Time.Unit(400, :msec) + 20 + Time.Unit(20, :day) + 40 #=> 20day 1min 400msec (removed 0sec)

Comparable

sec_30 < hour_900 #=> true
[sec_30, min_55_and_more, hour_900].sort #=> ["30sec", "55min 33sec 333msec", "37day 12hour"]

Parse from strings

unit_1 = Time::Unit.parse('55minutes 33seconds 333milliseconds') #=> 55min 33sec 333msec
unit_2 = Time::Unit.parse('55min 33sec 333msec') #=> 55min 33sec 333msec

And more

sec_30.sleep #=> wait for 30 seconds

Requirements

  • Ruby 1.9.2 or later

Installation

gem install time-unit

Links

code

github.com/kachick/time-unit

issues

github.com/kachick/time-unit/issues

CI

travis-ci.org/#!/kachick/time-unit

gem

rubygems.org/gems/time-unit

gem+

metagem.info/gems/time-unit

License

The MIT X License

Copyright (C) 2010 Kenichi Kamiya

See the file LICENSE for further details.

Something went wrong with that request. Please try again.