time_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/README.md +34 -0
- data/lib/time_helper.rb +46 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/time_helpers_spec.rb +92 -0
- metadata +65 -0
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.2@time_extras
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
rspec (2.7.0)
|
6
|
+
rspec-core (~> 2.7.0)
|
7
|
+
rspec-expectations (~> 2.7.0)
|
8
|
+
rspec-mocks (~> 2.7.0)
|
9
|
+
rspec-core (2.7.1)
|
10
|
+
rspec-expectations (2.7.0)
|
11
|
+
diff-lcs (~> 1.1.2)
|
12
|
+
rspec-mocks (2.7.0)
|
13
|
+
time_travel (0.1.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
rspec
|
20
|
+
time_travel
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# time_helper
|
2
|
+
|
3
|
+
time_helper is a slim gem that adds some of the methods that I really liked from working with Rails projects. I still to a lot of work that require date and time manipulation so I decided to finally replicate some of the methods since I don't want to rely on ActiveSupport.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
None really. As long as you don't rely on ActiveSupport already it shouldn't affect your code. You'll see some warnings and this gem won't do anything if this is the case.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
gem install time_helper
|
12
|
+
|
13
|
+
## Examples
|
14
|
+
|
15
|
+
Most of the time I use the methods in combination with Time objects but by default they return the number of seconds.
|
16
|
+
|
17
|
+
3.minutes == 180
|
18
|
+
|
19
|
+
But they become more useful when used in combination of a Time object or with the ago/from_now methods.
|
20
|
+
At noon of April 17, 1964 `Time.utc(1964, 4, 17, 12)` these would be the results
|
21
|
+
|
22
|
+
3.days.ago == Time.utc(1964, 4, 14, 12)
|
23
|
+
|
24
|
+
and
|
25
|
+
|
26
|
+
5.hours.from_now == Time.utc(1964, 4, 17, 17)
|
27
|
+
|
28
|
+
If you have a Time object you can use the methods to alter it i.e. using `t = Time.utc(1964, 4, 17, 12)` you will get
|
29
|
+
|
30
|
+
t - 3.days == Time.utc(1964, 4, 14, 12)
|
31
|
+
|
32
|
+
and
|
33
|
+
|
34
|
+
t + 5 hours == Time.utc(1964, 4, 17, 17)
|
data/lib/time_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module TimeHelper
|
2
|
+
SECOND = 1.freeze
|
3
|
+
MINUTE = (60 * SECOND).freeze
|
4
|
+
HOUR = (60 * MINUTE).freeze
|
5
|
+
DAY = (24 * HOUR).freeze
|
6
|
+
|
7
|
+
def second
|
8
|
+
self * SECOND
|
9
|
+
end
|
10
|
+
alias :seconds :second
|
11
|
+
|
12
|
+
def minute
|
13
|
+
self * MINUTE
|
14
|
+
end
|
15
|
+
alias :minutes :minute
|
16
|
+
|
17
|
+
def hour
|
18
|
+
self * HOUR
|
19
|
+
end
|
20
|
+
alias :hours :hour
|
21
|
+
|
22
|
+
def day
|
23
|
+
self * DAY
|
24
|
+
end
|
25
|
+
alias :days :day
|
26
|
+
|
27
|
+
def ago(time = Time.now)
|
28
|
+
time - self
|
29
|
+
end
|
30
|
+
|
31
|
+
def since(time = Time.now)
|
32
|
+
time + self
|
33
|
+
end
|
34
|
+
alias :from_now :since
|
35
|
+
end
|
36
|
+
|
37
|
+
already_included_methods = TimeHelper.instance_methods.map { |method| method if 1.respond_to?(method)}.compact
|
38
|
+
if already_included_methods.empty?
|
39
|
+
class Numeric
|
40
|
+
include TimeHelper
|
41
|
+
end
|
42
|
+
else
|
43
|
+
already_included_methods.each do |method|
|
44
|
+
puts "WARNING: Numeric has already been altered to include a method called #{method}. TimeHelper will not be included"
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeHelper do
|
4
|
+
describe '#minutes' do
|
5
|
+
it 'converts minutes to seconds' do
|
6
|
+
1.minute.should == 60
|
7
|
+
2.minutes.should == 120
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#hours' do
|
12
|
+
it 'converts hours to seconds' do
|
13
|
+
1.hour.should == 60 * 60
|
14
|
+
2.hours.should == 2 * 60 * 60
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#days' do
|
19
|
+
it 'converts days to seconds' do
|
20
|
+
1.day.should == 24 * 60 * 60
|
21
|
+
2.days.should == 2 * 24 * 60 * 60
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#ago' do
|
26
|
+
it 'subtracts seconds' do
|
27
|
+
at_time Time.utc(1964, 04, 17) do
|
28
|
+
one_second_ago = Time.utc(1964, 04, 16, 23, 59, 59)
|
29
|
+
1.second.ago.should == one_second_ago
|
30
|
+
(Time.now - 1.second).should == one_second_ago
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'subtracts minutes' do
|
35
|
+
at_time Time.utc(1964, 04, 17) do
|
36
|
+
thirty_minutes_ago = Time.utc(1964, 04, 16, 23, 30)
|
37
|
+
30.minutes.ago.should == thirty_minutes_ago
|
38
|
+
(Time.now - 30.minutes).should == thirty_minutes_ago
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'subtracts hours' do
|
43
|
+
at_time Time.utc(1964, 04, 17) do
|
44
|
+
twelve_hours_ago = Time.utc(1964, 04, 16, 12)
|
45
|
+
12.hours.ago.should == twelve_hours_ago
|
46
|
+
(Time.now - 12.hours).should == twelve_hours_ago
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'subtracts days' do
|
51
|
+
at_time Time.utc(1964, 04, 17) do
|
52
|
+
three_days_ago = Time.utc(1964, 04, 14)
|
53
|
+
3.days.ago.should == three_days_ago
|
54
|
+
(Time.now - 3.days).should == three_days_ago
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#from_now' do
|
60
|
+
it 'adds seconds' do
|
61
|
+
at_time Time.utc(1964, 04, 17) do
|
62
|
+
one_second_from_now = Time.utc(1964, 04, 17, 0, 0, 1)
|
63
|
+
1.second.from_now.should == one_second_from_now
|
64
|
+
(Time.now + 1.second).should == one_second_from_now
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'adds minutes' do
|
69
|
+
at_time Time.utc(1964, 04, 17) do
|
70
|
+
thirty_minutes_from_now = Time.utc(1964, 04, 17, 0, 30)
|
71
|
+
30.minute.from_now.should == thirty_minutes_from_now
|
72
|
+
(Time.now + 30.minute).should == thirty_minutes_from_now
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'adds hours' do
|
77
|
+
at_time Time.utc(1964, 04, 17) do
|
78
|
+
twelve_hours_from_now = Time.utc(1964, 04, 17, 12)
|
79
|
+
12.hours.from_now.should == twelve_hours_from_now
|
80
|
+
(Time.now + 12.hours).should == twelve_hours_from_now
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'adds days' do
|
85
|
+
at_time Time.utc(1964, 04, 17) do
|
86
|
+
three_days_from_now = Time.utc(1964, 04, 20)
|
87
|
+
3.days.from_now.should == three_days_from_now
|
88
|
+
(Time.now + 3.days).should == three_days_from_now
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Gaiottino
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70205896711280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70205896711280
|
25
|
+
description: ''
|
26
|
+
email:
|
27
|
+
- daniel@burtcorp.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .rspec
|
33
|
+
- .rvmrc
|
34
|
+
- Gemfile
|
35
|
+
- Gemfile.lock
|
36
|
+
- README.md
|
37
|
+
- lib/time_helper.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/time_helpers_spec.rb
|
40
|
+
homepage: https://github.com/gaiottino/time_helper
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: time_helper
|
60
|
+
rubygems_version: 1.8.6
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Some of the functions I've really liked in ActiveSupport but without relying
|
64
|
+
on ActiveSupport
|
65
|
+
test_files: []
|