human-date 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +28 -0
- data/Rakefile +6 -0
- data/human-date.gemspec +21 -0
- data/lib/human-date.rb +2 -0
- data/lib/human-date/date_translator.rb +35 -0
- data/lib/human-date/version.rb +5 -0
- data/test/test_date_translator.rb +63 -0
- metadata +68 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
human-date
|
2
|
+
==========
|
3
|
+
|
4
|
+
Installation:
|
5
|
+
-------------
|
6
|
+
```
|
7
|
+
gem install human-date
|
8
|
+
```
|
9
|
+
|
10
|
+
Usage:
|
11
|
+
------
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
date_translator = HumanDate::DateTranslator.new
|
15
|
+
from = DateTime.now
|
16
|
+
sleep 10
|
17
|
+
to = DateTime.now
|
18
|
+
|
19
|
+
date_translator.translate(from, to)
|
20
|
+
=> '10 seconds'
|
21
|
+
|
22
|
+
date_translator.translate(to, from)
|
23
|
+
=> '10 seconds ago'
|
24
|
+
|
25
|
+
date_translator.tolerance = 10
|
26
|
+
date_translator.translate(from, to)
|
27
|
+
=> 'now'
|
28
|
+
```
|
data/Rakefile
ADDED
data/human-date.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "human-date/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "human-date"
|
7
|
+
s.version = Human::Date::VERSION
|
8
|
+
s.authors = ["Andrew Vos"]
|
9
|
+
s.email = ["andrew.vos@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/AndrewVos/human-date"
|
11
|
+
s.summary = %q{Human readable date differences}
|
12
|
+
s.description = %q{Human readable date differences}
|
13
|
+
|
14
|
+
s.rubyforge_project = "human-date"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.add_development_dependency "minitest"
|
21
|
+
end
|
data/lib/human-date.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module HumanDate
|
4
|
+
class DateTranslator
|
5
|
+
attr_accessor :tolerance
|
6
|
+
|
7
|
+
TIME = {
|
8
|
+
:year => 365 * 24 * 60 * 60,
|
9
|
+
:month => 30 * 24 * 60 * 60,
|
10
|
+
:day => 24 * 60 * 60,
|
11
|
+
:hour => 60 * 60,
|
12
|
+
:minute => 60,
|
13
|
+
:second => 1
|
14
|
+
}
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@tolerance = 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def translate(from, to)
|
21
|
+
total_difference_in_seconds = ((to-from) * 60 * 60 * 24).to_i
|
22
|
+
return 'now' if total_difference_in_seconds.abs <= tolerance
|
23
|
+
|
24
|
+
TIME.keys.each do |key|
|
25
|
+
if total_difference_in_seconds.abs >= TIME[key]
|
26
|
+
result = "#{total_difference_in_seconds.abs / TIME[key]} #{key}"
|
27
|
+
result += 's' if total_difference_in_seconds.abs >= (TIME[key] * 2)
|
28
|
+
result += ' ago' if total_difference_in_seconds < 0
|
29
|
+
return result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'minitest/pride'
|
2
|
+
require 'minitest/unit'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require_relative '../lib/human-date'
|
5
|
+
|
6
|
+
class DateTranslatorTest < MiniTest::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@date_translator = HumanDate::DateTranslator.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def translate to
|
12
|
+
from = DateTime.parse("2011-10-23T00:00:00")
|
13
|
+
to = DateTime.parse(to)
|
14
|
+
@date_translator.translate(from, to)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_translate_returns_now_if_dates_are_equal
|
18
|
+
assert_equal "now", translate("2011-10-23T00:00:00")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_translate_returns_now_if_date_difference_is_within_tolerance
|
22
|
+
@date_translator.tolerance = 15
|
23
|
+
assert_equal "now", translate("2011-10-23T00:00:15")
|
24
|
+
assert_equal "now", translate("2011-10-23T00:00:10")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_translate_returns_years
|
28
|
+
assert_equal "1 year", translate("2012-10-23T00:00:00")
|
29
|
+
assert_equal "2 years", translate("2013-10-23T00:00:00")
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_translate_returns_months
|
33
|
+
assert_equal "1 month", translate("2011-11-23T00:00:00")
|
34
|
+
assert_equal "2 months", translate("2011-12-23T00:00:00")
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_translate_returns_days
|
38
|
+
assert_equal "1 day", translate("2011-10-24T00:00:00")
|
39
|
+
assert_equal "2 days", translate("2011-10-25T00:00:00")
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_translate_returns_hours
|
43
|
+
assert_equal "1 hour", translate("2011-10-23T01:00:00")
|
44
|
+
assert_equal "2 hours", translate("2011-10-23T02:00:00")
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_translate_returns_minutes
|
48
|
+
assert_equal "1 minute", translate("2011-10-23T00:01:00")
|
49
|
+
assert_equal "2 minutes", translate("2011-10-23T00:02:00")
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_translate_returns_seconds
|
53
|
+
@date_translator.tolerance = 0
|
54
|
+
assert_equal "1 second", translate("2011-10-23T00:00:01")
|
55
|
+
assert_equal "2 seconds", translate("2011-10-23T00:00:02")
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_translate_appends_ago_if_to_is_before_from
|
59
|
+
assert_equal "20 seconds ago", translate("2011-10-22T23:59:40")
|
60
|
+
assert_equal "1 day ago", translate("2011-10-22T00:00:00")
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human-date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Vos
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-07-31 00:00:00.000000000 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: minitest
|
17
|
+
requirement: &70305826377680 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70305826377680
|
26
|
+
description: Human readable date differences
|
27
|
+
email:
|
28
|
+
- andrew.vos@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- human-date.gemspec
|
38
|
+
- lib/human-date.rb
|
39
|
+
- lib/human-date/date_translator.rb
|
40
|
+
- lib/human-date/version.rb
|
41
|
+
- test/test_date_translator.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: https://github.com/AndrewVos/human-date
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: human-date
|
63
|
+
rubygems_version: 1.6.2
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Human readable date differences
|
67
|
+
test_files:
|
68
|
+
- test/test_date_translator.rb
|