moments 0.0.1.alpha → 0.0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5c9ac306fade5a3e30cf114c2d8d2c5bd6ddef7
4
- data.tar.gz: 01ab04bf9129c934bbcd347a09e71d9681d8ee9d
3
+ metadata.gz: 50e1bdbcf9a083bb6213223ff6f9558bb8871c0f
4
+ data.tar.gz: b5657505ec91e49a9ca89de5a0e299a7c5fec436
5
5
  SHA512:
6
- metadata.gz: d22dbd113c4e13f1efc0b45edc5ef8b1c961bc6400b2c109d659a0d37e8615a011072d42034d71c5dba59c95a9f512fb80aaeff8d6d85c1c4c76bbca0b062d50
7
- data.tar.gz: be8882e85ce70577b6ba4cc8d53109e6d7c5e30e06ed345b9d2c2a48d9aadb7d627d0d217356b5254819e8f9ae207b8aca69cfee7aa3cc9b8ecc79c2023baa92
6
+ metadata.gz: e1c3176a225c6674a199dee03fe9d60912fb71f3eaa4adfa9d88f1c2e4a2215e7d3dfc9b5c76136de67a6945cf80fca4f4c96451de34379c347dac377ea10676
7
+ data.tar.gz: df0f0200d5bf1a5b8164751cbc4c242e79dc5de030302931de003fe88c14a5de3a95b511fd2898caabb1e147be8b592c0780052052ec49a11ab965543be1a48d
data/.travis.yml CHANGED
@@ -1,7 +1,10 @@
1
1
  language: ruby
2
2
  script: "bundle exec rspec"
3
3
  rvm:
4
- - 2.1.0
4
+ - 2.1.5
5
5
  - jruby-19mode
6
6
  - ruby-head
7
7
  - jruby-head
8
+ addons:
9
+ code_climate:
10
+ repo_token: 83c2e090282b16d91251419d1258e47c82d725056835a1daef193077cbb2a0e6
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
- # Moments
1
+ # Moments [![Gem Version](https://badge.fury.io/rb/moments.svg)](http://badge.fury.io/rb/moments)
2
2
 
3
3
  [![Build Status](https://travis-ci.org/excpt/moments.svg?branch=master)](https://travis-ci.org/excpt/moments)
4
4
  [![Code Climate](https://codeclimate.com/github/excpt/moments.png)](https://codeclimate.com/github/excpt/moments)
5
+ [![Test Coverage](https://codeclimate.com/github/excpt/moments/badges/coverage.svg)](https://codeclimate.com/github/excpt/moments)
6
+ [![Dependency Status](https://gemnasium.com/excpt/moments.svg)](https://gemnasium.com/excpt/moments)
5
7
 
6
8
  Handles time differences and calculations.
7
9
 
@@ -21,6 +23,19 @@ Or install it yourself as:
21
23
 
22
24
  ## Usage
23
25
 
26
+ ```ruby
27
+ require 'moments'
28
+
29
+ t1 = Time.now
30
+ t2 = Time.new 2020, 1, 1
31
+
32
+ # Important: For now t1 must be equal or less than t2
33
+ diff = Moments.difference t1, t2
34
+
35
+ puts diff.to_hash
36
+ # Output {:years=>5, :months=>7, :days=>5, :hours=>19, :minutes=>29, :seconds=>6}
37
+ ```
38
+
24
39
  ## Contributing
25
40
 
26
41
  1. Fork it ( https://github.com/excpt/moments/fork )
@@ -29,55 +29,55 @@ module Moments
29
29
  end
30
30
 
31
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
32
+ @diff = {
33
+ seconds: @to.sec - @from.sec,
34
+ minutes: @to.min - @from.min,
35
+ hours: @to.hour - @from.hour,
36
+ days: @to.day - @from.day,
37
+ months: @to.month - @from.month,
38
+ years: @to.year - @from.year,
39
+ }
43
40
 
44
- if minutes < 0
45
- minutes += 60
46
- hours -= 1
47
- end
41
+ calculate :seconds, :minutes
42
+ calculate :minutes, :hours
43
+ calculate :hours, :days, 24
44
+ calculate_days
45
+ calculate :months, :years, 12
48
46
 
49
- if hours < 0
50
- hours += 24
51
- days -= 1
52
- end
47
+ @diff
48
+ end
53
49
 
54
- if days < 0
55
- previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
50
+ private :precise_difference
56
51
 
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
52
+ def calculate(attr, subtr, increment = 60)
53
+ if @diff[attr] < 0
54
+ @diff[attr] += increment
55
+ @diff[subtr] -= 1
56
+ end
57
+ end
58
+
59
+ private :calculate
62
60
 
63
- months -= 1
61
+ def calculate_days
62
+ if @diff[:days] < 0
63
+ previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
64
+ @diff[:days] = precise_previous_month_days(@diff[:days], previous_month_days, @from.day)
65
+ @diff[:months] -= 1
64
66
  end
67
+ end
65
68
 
66
- if months < 0
67
- months += 12
68
- years -= 1
69
+ private :calculate_days
70
+
71
+ def precise_previous_month_days(days, previous, from)
72
+ if previous < from
73
+ days = previous + days + (from - previous)
74
+ else
75
+ days = previous + days
69
76
  end
70
77
 
71
- @diff = {
72
- years: years,
73
- months: months,
74
- days: days,
75
- hours: hours,
76
- minutes: minutes,
77
- seconds: seconds
78
- }
78
+ days
79
79
  end
80
80
 
81
- private :precise_difference
81
+ private :precise_previous_month_days
82
82
  end
83
- end
83
+ end
@@ -6,7 +6,7 @@ module Moments
6
6
  module VERSION
7
7
  MAJOR = 0
8
8
  MINOR = 0
9
- TINY = 1
9
+ TINY = 2
10
10
  PRE = 'alpha'
11
11
 
12
12
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
data/moments.gemspec CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "bundler"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "codeclimate-test-reporter"
24
25
  end
@@ -1,3 +1,4 @@
1
+ require 'spec_helper'
1
2
  require_relative '../../../lib/moments/difference'
2
3
 
3
4
  describe Moments::Difference do
@@ -1,3 +1,4 @@
1
+ require 'spec_helper'
1
2
  require_relative '../../lib/moments.rb'
2
3
 
3
4
  describe Moments do
@@ -17,5 +18,11 @@ describe Moments do
17
18
 
18
19
  Moments.difference(from, to).should be_a Moments::Difference
19
20
  end
21
+
22
+ it '#difference should raise an error if :from is greater than :to' do
23
+ expect do
24
+ Moments.difference(to, from)
25
+ end.to raise_error
26
+ end
20
27
  end
21
28
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,11 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  RSpec.configure do |config|
2
- config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.expect_with :rspec do |c|
6
+ c.syntax = [:should, :expect]
7
+ end
8
+
3
9
  config.run_all_when_everything_filtered = true
4
10
  config.filter_run :focus
5
11
 
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.2.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Rudat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-26 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.6'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.6'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: ''
56
70
  email:
57
71
  - timrudat@gmail.com
@@ -93,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
107
  version: 1.3.1
94
108
  requirements: []
95
109
  rubyforge_project:
96
- rubygems_version: 2.2.2
110
+ rubygems_version: 2.4.1
97
111
  signing_key:
98
112
  specification_version: 4
99
113
  summary: Handles time differences and calculations.