moments 0.0.1.alpha → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f5c9ac306fade5a3e30cf114c2d8d2c5bd6ddef7
4
- data.tar.gz: 01ab04bf9129c934bbcd347a09e71d9681d8ee9d
2
+ SHA256:
3
+ metadata.gz: 0b85363c9163c9935f75833c74b4e7d587cc70d962ca4f86e14e6d8a3bb14196
4
+ data.tar.gz: 1a6421e682139a2cefb0b82d12bcd5d08cb2f8220ab20a65f9646938e0ee1653
5
5
  SHA512:
6
- metadata.gz: d22dbd113c4e13f1efc0b45edc5ef8b1c961bc6400b2c109d659a0d37e8615a011072d42034d71c5dba59c95a9f512fb80aaeff8d6d85c1c4c76bbca0b062d50
7
- data.tar.gz: be8882e85ce70577b6ba4cc8d53109e6d7c5e30e06ed345b9d2c2a48d9aadb7d627d0d217356b5254819e8f9ae207b8aca69cfee7aa3cc9b8ecc79c2023baa92
6
+ metadata.gz: 360367e75466166fc7bb786fa4d0247b23d7ba61b118286bc137c7b697ef25a12bf3d847f0b2907de9a899ee1d3132298b5196ef93387b536e800fca8132c24a
7
+ data.tar.gz: e374f88fd8029894bdeee1e0ea79ae58188552fe43438dfb56e37335a36121b91c6b14e0d8b7a814ff0901c10c9928916051f1ea38a1b28997bce3ff47f932a9
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # http://editorconfig.org
2
+
3
+ root = true
4
+
5
+ [*]
6
+ charset = utf-8
7
+ indent_style = space
8
+ indent_size = 2
9
+ end_of_line = lf
10
+ insert_final_newline = true
11
+ trim_trailing_whitespace = true
12
+ max_line_length = 100
data/.gitignore CHANGED
@@ -24,3 +24,5 @@ mkmf.log
24
24
  *.iml
25
25
  *.ipr
26
26
  *.iws
27
+ .idea/
28
+ .ruby-version
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
+ --require spec_helper
1
2
  --color
2
- --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,81 @@
1
+ # inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+ Exclude:
8
+ - 'gemfiles/*.gemfile'
9
+ - 'vendor/**/*'
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Style/BlockDelimiters:
15
+ Exclude:
16
+ - spec/**/*_spec.rb
17
+
18
+ Style/GuardClause:
19
+ Enabled: false
20
+
21
+ Style/IfUnlessModifier:
22
+ Enabled: false
23
+
24
+ Layout/SpaceInsideHashLiteralBraces:
25
+ Enabled: false
26
+
27
+ Style/Lambda:
28
+ Enabled: false
29
+
30
+ Style/RaiseArgs:
31
+ Enabled: false
32
+
33
+ Style/SignalException:
34
+ Enabled: false
35
+
36
+ Metrics/AbcSize:
37
+ Max: 25
38
+
39
+ Metrics/ClassLength:
40
+ Max: 120
41
+
42
+ Metrics/ModuleLength:
43
+ Max: 100
44
+
45
+ Layout/LineLength:
46
+ Enabled: false
47
+
48
+ Metrics/BlockLength:
49
+ Exclude:
50
+ - spec/**/*_spec.rb
51
+
52
+ Metrics/MethodLength:
53
+ Max: 15
54
+
55
+ Style/SingleLineBlockParams:
56
+ Enabled: false
57
+
58
+ Layout/EndAlignment:
59
+ EnforcedStyleAlignWith: variable
60
+
61
+ Style/FormatString:
62
+ Enabled: false
63
+
64
+ Layout/MultilineMethodCallIndentation:
65
+ EnforcedStyle: indented
66
+
67
+ Layout/MultilineOperationIndentation:
68
+ EnforcedStyle: indented
69
+
70
+ Style/WordArray:
71
+ Enabled: false
72
+
73
+ Style/RedundantSelf:
74
+ Enabled: false
75
+
76
+ Layout/HashAlignment:
77
+ Enabled: true
78
+ EnforcedLastArgumentHashStyle: always_ignore
79
+
80
+ Style/TrivialAccessors:
81
+ AllowPredicates: true
data/.travis.yml CHANGED
@@ -1,7 +1,12 @@
1
+ sudo: false
2
+ cache: bundler
1
3
  language: ruby
2
4
  script: "bundle exec rspec"
5
+ before_install:
6
+ - gem update --system
7
+ - gem --version
8
+ - bundle install
3
9
  rvm:
4
- - 2.1.0
5
- - jruby-19mode
6
- - ruby-head
7
- - jruby-head
10
+ - 2.5.0
11
+ - 2.6.0
12
+ - 2.7.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,43 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased](https://github.com/excpt/moments/tree/HEAD)
4
+
5
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.1.0...HEAD)
6
+
7
+ **Closed issues:**
8
+
9
+ - Make a new release [\#7](https://github.com/excpt/moments/issues/7)
10
+
11
+ **Merged pull requests:**
12
+
13
+ - Parse string input whenever possible [\#10](https://github.com/excpt/moments/pull/10) ([RomanVanLoo](https://github.com/RomanVanLoo))
14
+ - Add `in_weeks` [\#9](https://github.com/excpt/moments/pull/9) ([RomanVanLoo](https://github.com/RomanVanLoo))
15
+ - Release 0.1.0 [\#8](https://github.com/excpt/moments/pull/8) ([excpt](https://github.com/excpt))
16
+ - Fix `#in_*` for times with miliseconds [\#6](https://github.com/excpt/moments/pull/6) ([AlexWayfer](https://github.com/AlexWayfer))
17
+ - Fix diffs with different timezones, `Date` diffs and `#in_years` [\#5](https://github.com/excpt/moments/pull/5) ([AlexWayfer](https://github.com/AlexWayfer))
18
+ - Add `#in_*` methods, like `#in_seconds` [\#4](https://github.com/excpt/moments/pull/4) ([AlexWayfer](https://github.com/AlexWayfer))
19
+ - Many improvements [\#2](https://github.com/excpt/moments/pull/2) ([AlexWayfer](https://github.com/AlexWayfer))
20
+
21
+ ## [v0.1.0](https://github.com/excpt/moments/tree/v0.1.0) (2020-08-18)
22
+
23
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.0.2.alpha...v0.1.0)
24
+
25
+ **Closed issues:**
26
+
27
+ - Replace Travis CI with another CI [\#3](https://github.com/excpt/moments/issues/3)
28
+
29
+ ## [v0.0.2.alpha](https://github.com/excpt/moments/tree/v0.0.2.alpha) (2014-11-14)
30
+
31
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.0.1.alpha...v0.0.2.alpha)
32
+
33
+ ## [v0.0.1.alpha](https://github.com/excpt/moments/tree/v0.0.1.alpha) (2014-05-26)
34
+
35
+ [Full Changelog](https://github.com/excpt/moments/compare/9b7612483150b52000ab4948d5c2217945230a1d...v0.0.1.alpha)
36
+
37
+ **Closed issues:**
38
+
39
+ - Test ticket from Code Climate [\#1](https://github.com/excpt/moments/issues/1)
40
+
41
+
42
+
43
+ \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
File without changes
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
- # Moments
1
+ # Moments [![Gem Version](https://badge.fury.io/rb/moments.svg)](http://badge.fury.io/rb/moments)
2
2
 
3
+ [![Codeship Status for excpt/moments](https://codeship.com/projects/54ff9970-675f-0132-a1ba-760b27c1e7ed/status?branch=master)](https://codeship.com/projects/53081)
3
4
  [![Build Status](https://travis-ci.org/excpt/moments.svg?branch=master)](https://travis-ci.org/excpt/moments)
4
5
  [![Code Climate](https://codeclimate.com/github/excpt/moments.png)](https://codeclimate.com/github/excpt/moments)
6
+ [![Test Coverage](https://codeclimate.com/github/excpt/moments/badges/coverage.svg)](https://codeclimate.com/github/excpt/moments)
7
+ [![Dependency Status](https://gemnasium.com/excpt/moments.svg)](https://gemnasium.com/excpt/moments)
5
8
 
6
9
  Handles time differences and calculations.
7
10
 
@@ -9,18 +12,40 @@ Handles time differences and calculations.
9
12
 
10
13
  Add this line to your application's Gemfile:
11
14
 
12
- gem 'moments'
15
+ ```ruby
16
+ gem 'moments'
17
+ ```
13
18
 
14
19
  And then execute:
15
20
 
16
- $ bundle
21
+ ```sh
22
+ $ bundle
23
+ ```
17
24
 
18
25
  Or install it yourself as:
19
26
 
20
- $ gem install moments
27
+ ```sh
28
+ $ gem install moments
29
+ ```
21
30
 
22
31
  ## Usage
23
32
 
33
+ ```ruby
34
+ require 'moments'
35
+
36
+ t1 = Time.new 2015, 1, 1, 0, 0, 0
37
+ t2 = Time.now # 2020, 8, 6, 19, 29, 6
38
+
39
+ diff = Moments.difference t1, t2
40
+
41
+ puts diff.to_hash
42
+ # { years: 5, months: 7, days: 5, hours: 19, minutes: 29, seconds: 6 }
43
+
44
+ puts diff.in_months
45
+ # 67 (5 * 12 + 7)
46
+ # there are also methods for each component
47
+ ```
48
+
24
49
  ## Contributing
25
50
 
26
51
  1. Fork it ( https://github.com/excpt/moments/fork )
data/Rakefile CHANGED
@@ -1,2 +1,3 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
2
 
3
+ require 'bundler/gem_tasks'
@@ -1,13 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
1
5
  module Moments
6
+ # Calculates differences between two given Time instances.
2
7
  class Difference
8
+ DATE_PARTS = {
9
+ years: :year,
10
+ months: :month,
11
+ days: :day
12
+ }.freeze
13
+
14
+ TIME_PARTS = {
15
+ hours: :hour,
16
+ minutes: :min,
17
+ seconds: :sec
18
+ }.freeze
19
+
20
+ private_constant :DATE_PARTS, :TIME_PARTS
21
+
22
+ # == Parameters:
23
+ # from::
24
+ # A instance of Time
25
+ # to::
26
+ # A instance of Time
3
27
  def initialize(from, to)
4
- @from = from
5
- @to = to
28
+ @from = parse_argument from
29
+ @to = parse_argument to
6
30
 
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
31
+ @ordered_from, @ordered_to = [@from, @to].sort
11
32
 
12
33
  precise_difference
13
34
  end
@@ -28,56 +49,121 @@ module Moments
28
49
  @from > @to
29
50
  end
30
51
 
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
52
+ def in_seconds
53
+ @ordered_to.to_i - @ordered_from.to_i
54
+ end
43
55
 
44
- if minutes < 0
45
- minutes += 60
46
- hours -= 1
47
- end
56
+ def in_minutes
57
+ in_seconds / 60
58
+ end
59
+
60
+ def in_hours
61
+ in_minutes / 60
62
+ end
63
+
64
+ def in_days
65
+ in_hours / 24
66
+ end
67
+
68
+ def in_weeks
69
+ in_days / 7
70
+ end
71
+
72
+ def in_months
73
+ months_diff = @ordered_to.month - @ordered_from.month
74
+ months_diff -= 1 if months_diff.positive? && @ordered_to.mday < @ordered_from.mday
75
+
76
+ ((@ordered_to.year - @ordered_from.year) * 12) + months_diff
77
+ end
48
78
 
49
- if hours < 0
50
- hours += 24
51
- days -= 1
79
+ def in_years
80
+ years_diff = @ordered_to.year - @ordered_from.year
81
+
82
+ return years_diff unless years_diff.positive?
83
+ return years_diff if @ordered_to.month > @ordered_from.month
84
+
85
+ if (@ordered_to.month < @ordered_from.month) || (@ordered_to.mday < @ordered_from.mday)
86
+ years_diff -= 1
52
87
  end
53
88
 
54
- if days < 0
55
- previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
89
+ years_diff
90
+ end
56
91
 
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
92
+ private
93
+
94
+ TIME_CLASSES = [Time, DateTime].freeze
62
95
 
63
- months -= 1
96
+ private_constant :TIME_CLASSES
97
+
98
+ def parse_argument(value)
99
+ case value
100
+ when *TIME_CLASSES
101
+ value.to_time.getutc
102
+ when Date
103
+ value.to_time
104
+ when String
105
+ begin
106
+ Time.parse(value).getutc
107
+ rescue ArgumentError
108
+ unsupported_format
109
+ end
110
+ else
111
+ unsupported_format
64
112
  end
113
+ end
114
+
115
+ def unsupported_format
116
+ raise ArgumentError, 'Unsupported format'
117
+ end
118
+
119
+ def precise_difference
120
+ @diff = calculate_diff
121
+
122
+ calculate :seconds, :minutes
123
+ calculate :minutes, :hours
124
+ calculate :hours, :days, 24
125
+ calculate_days
126
+ calculate :months, :years, 12
127
+
128
+ @diff
129
+ end
65
130
 
66
- if months < 0
67
- months += 12
68
- years -= 1
131
+ def calculate_diff
132
+ are_time_parts = [@from, @to].all? do |value|
133
+ TIME_CLASSES.any? { |time_class| value.is_a?(time_class) }
69
134
  end
70
135
 
71
- @diff = {
72
- years: years,
73
- months: months,
74
- days: days,
75
- hours: hours,
76
- minutes: minutes,
77
- seconds: seconds
78
- }
136
+ (are_time_parts ? DATE_PARTS.merge(TIME_PARTS) : DATE_PARTS)
137
+ .transform_values do |method_name|
138
+ @ordered_to.public_send(method_name) - @ordered_from.public_send(method_name)
139
+ end
140
+ end
141
+
142
+ def calculate(attribute, difference, stepping = 60)
143
+ return unless @diff.key?(attribute) && @diff.key?(difference)
144
+
145
+ return if @diff[attribute] >= 0
146
+
147
+ @diff[attribute] += stepping
148
+ @diff[difference] -= 1
79
149
  end
80
150
 
81
- private :precise_difference
151
+ def calculate_days
152
+ return if @diff[:days] >= 0
153
+
154
+ previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
155
+ @diff[:days] = precise_previous_month_days(
156
+ @diff[:days], previous_month_days, @from.day
157
+ )
158
+ @diff[:months] -= 1
159
+ end
160
+
161
+ def precise_previous_month_days(days, previous, from)
162
+ if previous < from
163
+ previous + days + (from - previous)
164
+ else
165
+ previous + days
166
+ end
167
+ end
82
168
  end
83
- end
169
+ end
@@ -1,14 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Moments::Version module
1
4
  module Moments
2
5
  def self.gem_version
3
6
  Gem::Version.new VERSION::STRING
4
7
  end
5
8
 
9
+ # Moments version builder module
6
10
  module VERSION
11
+ # major version
7
12
  MAJOR = 0
8
- MINOR = 0
9
- TINY = 1
10
- PRE = 'alpha'
13
+ # minor version
14
+ MINOR = 2
15
+ # patch version
16
+ PATCH = 0
17
+ # alpha, beta, etc. tag
18
+ PRE = nil
11
19
 
12
- STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
20
+ # Build version string
21
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
13
22
  end
14
23
  end
data/lib/moments.rb CHANGED
@@ -1,6 +1,9 @@
1
- require 'moments/version'
2
- require 'moments/difference'
1
+ # frozen_string_literal: true
3
2
 
3
+ require_relative 'moments/version'
4
+ require_relative 'moments/difference'
5
+
6
+ # Entrypoint for the moments gem
4
7
  module Moments
5
8
  def self.difference(from, to)
6
9
  Moments::Difference.new from, to
data/moments.gemspec CHANGED
@@ -1,24 +1,25 @@
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'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/moments/version'
5
4
 
6
5
  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"
6
+ spec.name = 'moments'
7
+ spec.version = Moments.gem_version
8
+ spec.authors = ['Tim Rudat']
9
+ spec.email = ['timrudat@gmail.com']
10
+ spec.summary = 'Handles time differences.'
11
+ spec.description = ''
12
+ spec.homepage = 'https://github.com/excpt/moments'
13
+ spec.license = 'MIT'
14
+ spec.metadata['rubygems_mfa_required'] = 'true'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
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"]
18
+ spec.require_paths = ['lib']
20
19
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'github_changelog_generator'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'rubocop'
24
25
  end