moments 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f5c9ac306fade5a3e30cf114c2d8d2c5bd6ddef7
4
+ data.tar.gz: 01ab04bf9129c934bbcd347a09e71d9681d8ee9d
5
+ SHA512:
6
+ metadata.gz: d22dbd113c4e13f1efc0b45edc5ef8b1c961bc6400b2c109d659a0d37e8615a011072d42034d71c5dba59c95a9f512fb80aaeff8d6d85c1c4c76bbca0b062d50
7
+ data.tar.gz: be8882e85ce70577b6ba4cc8d53109e6d7c5e30e06ed345b9d2c2a48d9aadb7d627d0d217356b5254819e8f9ae207b8aca69cfee7aa3cc9b8ecc79c2023baa92
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ .rakeTasks
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
24
+ *.iml
25
+ *.ipr
26
+ *.iws
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: "bundle exec rspec"
3
+ rvm:
4
+ - 2.1.0
5
+ - jruby-19mode
6
+ - ruby-head
7
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tim Rudat <timrudat@gmail.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Moments
2
+
3
+ [![Build Status](https://travis-ci.org/excpt/moments.svg?branch=master)](https://travis-ci.org/excpt/moments)
4
+ [![Code Climate](https://codeclimate.com/github/excpt/moments.png)](https://codeclimate.com/github/excpt/moments)
5
+
6
+ Handles time differences and calculations.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'moments'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install moments
21
+
22
+ ## Usage
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/excpt/moments/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,83 @@
1
+ module Moments
2
+ class Difference
3
+ def initialize(from, to)
4
+ @from = from
5
+ @to = to
6
+
7
+ if past?
8
+ message = "Start date (#{from}) must be less or equal than the end date (#{@to})."
9
+ raise ArgumentError.new(message)
10
+ end
11
+
12
+ precise_difference
13
+ end
14
+
15
+ def to_hash
16
+ @diff
17
+ end
18
+
19
+ def future?
20
+ @from < @to
21
+ end
22
+
23
+ def same?
24
+ @from == @to
25
+ end
26
+
27
+ def past?
28
+ @from > @to
29
+ end
30
+
31
+ def precise_difference
32
+ seconds = @to.sec - @from.sec
33
+ minutes = @to.min - @from.min
34
+ hours = @to.hour - @from.hour
35
+ days = @to.day - @from.day
36
+ months = @to.month - @from.month
37
+ years = @to.year - @from.year
38
+
39
+ if seconds < 0
40
+ seconds += 60
41
+ minutes -= 1
42
+ end
43
+
44
+ if minutes < 0
45
+ minutes += 60
46
+ hours -= 1
47
+ end
48
+
49
+ if hours < 0
50
+ hours += 24
51
+ days -= 1
52
+ end
53
+
54
+ if days < 0
55
+ previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
56
+
57
+ if previous_month_days < @from.day
58
+ days = previous_month_days + days + (@from.day - previous_month_days)
59
+ else
60
+ days = previous_month_days + days
61
+ end
62
+
63
+ months -= 1
64
+ end
65
+
66
+ if months < 0
67
+ months += 12
68
+ years -= 1
69
+ end
70
+
71
+ @diff = {
72
+ years: years,
73
+ months: months,
74
+ days: days,
75
+ hours: hours,
76
+ minutes: minutes,
77
+ seconds: seconds
78
+ }
79
+ end
80
+
81
+ private :precise_difference
82
+ end
83
+ end
@@ -0,0 +1,14 @@
1
+ module Moments
2
+ def self.gem_version
3
+ Gem::Version.new VERSION::STRING
4
+ end
5
+
6
+ module VERSION
7
+ MAJOR = 0
8
+ MINOR = 0
9
+ TINY = 1
10
+ PRE = 'alpha'
11
+
12
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
13
+ end
14
+ end
data/lib/moments.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'moments/version'
2
+ require 'moments/difference'
3
+
4
+ module Moments
5
+ def self.difference(from, to)
6
+ Moments::Difference.new from, to
7
+ end
8
+ end
data/moments.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'moments/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "moments"
8
+ spec.version = Moments.gem_version
9
+ spec.authors = ["Tim Rudat"]
10
+ spec.email = ["timrudat@gmail.com"]
11
+ spec.summary = %q{Handles time differences and calculations.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/excpt/moments"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,194 @@
1
+ require_relative '../../../lib/moments/difference'
2
+
3
+ describe Moments::Difference do
4
+ let(:from) do
5
+ Time.new 2007, 1, 1
6
+ end
7
+
8
+ let(:to) do
9
+ Time.new 2012, 1, 1
10
+ end
11
+
12
+ it 'requires from and to times' do
13
+ expect do
14
+ Moments::Difference.new
15
+ end.to raise_error
16
+
17
+ expect do
18
+ Moments::Difference.new from, to
19
+ end.to_not raise_error
20
+ end
21
+
22
+ it 'raise error if from > to' do
23
+ expect do
24
+ Moments::Difference(to, from)
25
+ end.to raise_error
26
+ end
27
+
28
+ context '#to_hash' do
29
+ context 'equal dates' do
30
+ it '2013-01-01, 2013-01-01' do
31
+ from = Time.new 2013, 1, 1
32
+ to = from
33
+
34
+ expectation = { years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0 }
35
+
36
+ Moments::Difference.new(from, to).to_hash.should == expectation
37
+ end
38
+ end
39
+
40
+ context 'seconds' do
41
+ it '2013-01-01 00:00:00, 2013-01-01 00:00:01' do
42
+ from = Time.new 2013, 1, 1, 0, 0, 0
43
+ to = Time.new 2013, 1, 1, 0, 0, 1
44
+
45
+ expectation = { years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 1 }
46
+
47
+ Moments::Difference.new(from, to).to_hash.should == expectation
48
+ end
49
+
50
+ it '2013-01-31 23:59:59, 2013-02-01 00:00:00' do
51
+ from = Time.new 2013, 1, 31, 23, 59, 59
52
+ to = Time.new 2013, 2, 1, 0, 0, 0
53
+
54
+ expectation = { years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 1 }
55
+
56
+ Moments::Difference.new(from, to).to_hash.should == expectation
57
+ end
58
+ end
59
+
60
+ context 'minutes' do
61
+ it '2013-01-01 00:00:00, 2013-01-01 00:01:01' do
62
+ from = Time.new 2013, 1, 1, 0, 0, 0
63
+ to = Time.new 2013, 1, 1, 0, 1, 1
64
+
65
+ expectation = { years: 0, months: 0, days: 0, hours: 0, minutes: 1, seconds: 1 }
66
+
67
+ Moments::Difference.new(from, to).to_hash.should == expectation
68
+ end
69
+
70
+ it '2013-01-31 23:59:59, 2013-02-01 00:01:00' do
71
+ from = Time.new 2013, 1, 31, 23, 59, 59
72
+ to = Time.new 2013, 2, 1, 0, 1, 0
73
+
74
+ expectation = { years: 0, months: 0, days: 0, hours: 0, minutes: 1, seconds: 1 }
75
+
76
+ Moments::Difference.new(from, to).to_hash.should == expectation
77
+ end
78
+ end
79
+
80
+ context 'hours' do
81
+ it '2013-01-01 00:00:00, 2013-01-01 01:01:01' do
82
+ from = Time.new 2013, 1, 1, 0, 0, 0
83
+ to = Time.new 2013, 1, 1, 1, 1, 1
84
+
85
+ expectation = { years: 0, months: 0, days: 0, hours: 1, minutes: 1, seconds: 1 }
86
+
87
+ Moments::Difference.new(from, to).to_hash.should == expectation
88
+ end
89
+
90
+ it '2013-01-31 23:59:59, 2013-02-01 01:01:00' do
91
+ from = Time.new 2013, 1, 31, 23, 59, 59
92
+ to = Time.new 2013, 2, 1, 1, 1, 0
93
+
94
+ expectation = { years: 0, months: 0, days: 0, hours: 1, minutes: 1, seconds: 1 }
95
+
96
+ Moments::Difference.new(from, to).to_hash.should == expectation
97
+ end
98
+ end
99
+
100
+ context 'days' do
101
+ it '2013-01-01, 2013-01-02' do
102
+ from = Time.new 2013, 1, 1
103
+ to = Time.new 2013, 1, 2
104
+
105
+ expectation = { years: 0, months: 0, days: 1, hours: 0, minutes: 0, seconds: 0 }
106
+
107
+ Moments::Difference.new(from, to).to_hash.should == expectation
108
+ end
109
+
110
+ it '2013-01-31, 2013-02-01' do
111
+ from = Time.new 2013, 1, 31
112
+ to = Time.new 2013, 2, 1
113
+
114
+ expectation = { years: 0, months: 0, days: 1, hours: 0, minutes: 0, seconds: 0 }
115
+
116
+ Moments::Difference.new(from, to).to_hash.should == expectation
117
+ end
118
+ end
119
+
120
+ context 'months' do
121
+ it '2013-01-01, 2013-02-01' do
122
+ from = Time.new 2013, 1, 1
123
+ to = Time.new 2013, 2, 2
124
+
125
+ expectation = { years: 0, months: 1, days: 1, hours: 0, minutes: 0, seconds: 0 }
126
+
127
+ Moments::Difference.new(from, to).to_hash.should == expectation
128
+ end
129
+
130
+ it '2013-01-31, 2013-03-01' do
131
+ from = Time.new 2013, 1, 31
132
+ to = Time.new 2013, 3, 1
133
+
134
+ expectation = { years: 0, months: 1, days: 1, hours: 0, minutes: 0, seconds: 0 }
135
+
136
+ Moments::Difference.new(from, to).to_hash.should == expectation
137
+ end
138
+ end
139
+
140
+ context 'years' do
141
+ it '2013-01-01, 2014-01-01' do
142
+ from = Time.new 2013, 1, 1
143
+ to = Time.new 2014, 2, 2
144
+
145
+ expectation = { years: 1, months: 1, days: 1, hours: 0, minutes: 0, seconds: 0 }
146
+
147
+ Moments::Difference.new(from, to).to_hash.should == expectation
148
+ end
149
+
150
+ it '2013-01-31, 2014-03-01' do
151
+ from = Time.new 2013, 1, 31
152
+ to = Time.new 2014, 3, 1
153
+
154
+ expectation = { years: 1, months: 1, days: 1, hours: 0, minutes: 0, seconds: 0 }
155
+
156
+ Moments::Difference.new(from, to).to_hash.should == expectation
157
+ end
158
+ end
159
+
160
+ context 'leap year' do
161
+ it '2008-02-27, 2008-02-029' do
162
+ from = Time.new 2008, 2, 27
163
+ to = Time.new 2008, 2, 29
164
+
165
+ expectation = { years: 0, months: 0, days: 2, hours: 0, minutes: 0, seconds: 0 }
166
+
167
+ Moments::Difference.new(from, to).to_hash.should == expectation
168
+ end
169
+
170
+ it '2008-02-27, 2008-03-01' do
171
+ from = Time.new 2008, 2, 27
172
+ to = Time.new 2008, 3, 1
173
+
174
+ expectation = { years: 0, months: 0, days: 3, hours: 0, minutes: 0, seconds: 0 }
175
+
176
+ Moments::Difference.new(from, to).to_hash.should == expectation
177
+ end
178
+ end
179
+ end
180
+
181
+ it '#same?' do
182
+ moments = Moments::Difference.new(from, from)
183
+
184
+ moments.same?.should == true
185
+ moments.past?.should == false
186
+ end
187
+
188
+ it '#future?' do
189
+ moments = Moments::Difference.new(from, to)
190
+
191
+ moments.future?.should == true
192
+ moments.past?.should == false
193
+ end
194
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../../lib/moments.rb'
2
+
3
+ describe Moments do
4
+ context 'calculates the distance between two given times' do
5
+ let(:from) do
6
+ Time.new 2012, 1, 1
7
+ end
8
+
9
+ let(:to) do
10
+ Time.new 2013, 1, 1
11
+ end
12
+
13
+ it '#difference requires a start and end time' do
14
+ expect do
15
+ Moments.difference(from, to)
16
+ end.to_not raise_error
17
+
18
+ Moments.difference(from, to).should be_a Moments::Difference
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+
6
+ config.order = 'random'
7
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Tim Rudat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - timrudat@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/moments.rb
70
+ - lib/moments/difference.rb
71
+ - lib/moments/version.rb
72
+ - moments.gemspec
73
+ - spec/lib/moments/difference_spec.rb
74
+ - spec/lib/moments_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/excpt/moments
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">"
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Handles time differences and calculations.
100
+ test_files:
101
+ - spec/lib/moments/difference_spec.rb
102
+ - spec/lib/moments_spec.rb
103
+ - spec/spec_helper.rb