moments 0.1.0 → 0.3.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
2
  SHA256:
3
- metadata.gz: 2cfe9d93c489151bacb9c2c4b8de955a038db05cddf2a6d7edcb60c24532f8f3
4
- data.tar.gz: be776d177f3878449fb8aabab929cbe84bb31965e428cef9c31bd72d3ce964a7
3
+ metadata.gz: 77dbf9115233ea35f1e79c6592da59fde4ec415e7b7a74400a9789a407cd3bdf
4
+ data.tar.gz: fae4d1e2330151cfc27cb64d63bc10a9d5123e0dd6005a6dfebaa4b77038fd32
5
5
  SHA512:
6
- metadata.gz: 6b231e18ee429b37402e44571866f860a57e8b86957db6a18a75463fb6c3e325b7b7ec5dfe1da62d01259d78b749f3272bbd1c3078c268b8d1fbfcb70a3e55db
7
- data.tar.gz: fab01ca909e9c5418ad1ca734a4709e11a81effe6bc283bf8cb5174f2d62548d29bd7a3bedc85be2c2b23902f6f614694bc45dc229184bb657c00bf0373dfdf4
6
+ metadata.gz: b4e344eafc3286656abb581eef7cffdc21278b58a7b24c62e7e5e3d64bbb28f213e7fd1eb7e518bc10887336deecb6cf63a3ab4754a7712bedf667f68714530a
7
+ data.tar.gz: 6c0d50f01f547d3a6b219e5f2d2c34cd2ad277f6a4ec17016fc047280f740e9123c00e60a0ae0f06a503d84e78edae22b709d0d0167345a97e801bbed5536fcc
data/.rspec CHANGED
@@ -1,4 +1,2 @@
1
- --warnings
2
- --color
3
- --format documentation
4
1
  --require spec_helper
2
+ --color
data/.rubocop.yml CHANGED
@@ -1,42 +1,82 @@
1
- Layout/LineLength:
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
+ - 'spec/**/*'
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/BlockDelimiters:
16
+ Exclude:
17
+ - spec/**/*_spec.rb
18
+
19
+ Style/GuardClause:
20
+ Enabled: false
21
+
22
+ Style/IfUnlessModifier:
23
+ Enabled: false
24
+
25
+ Layout/SpaceInsideHashLiteralBraces:
26
+ Enabled: false
27
+
28
+ Style/Lambda:
29
+ Enabled: false
30
+
31
+ Style/RaiseArgs:
32
+ Enabled: false
33
+
34
+ Style/SignalException:
35
+ Enabled: false
36
+
37
+ Metrics/AbcSize:
38
+ Max: 25
39
+
40
+ Metrics/ClassLength:
41
+ Max: 120
42
+
43
+ Metrics/ModuleLength:
2
44
  Max: 100
45
+
46
+ Layout/LineLength:
47
+ Enabled: false
48
+
49
+ Metrics/BlockLength:
50
+ Exclude:
51
+ - spec/**/*_spec.rb
52
+
53
+ Metrics/MethodLength:
54
+ Max: 15
55
+
56
+ Style/SingleLineBlockParams:
57
+ Enabled: false
58
+
59
+ Layout/EndAlignment:
60
+ EnforcedStyleAlignWith: variable
61
+
62
+ Style/FormatString:
63
+ Enabled: false
64
+
65
+ Layout/MultilineMethodCallIndentation:
66
+ EnforcedStyle: indented
67
+
3
68
  Layout/MultilineOperationIndentation:
4
69
  EnforcedStyle: indented
5
- Layout/ArgumentAlignment:
6
- EnforcedStyle: with_fixed_indentation
7
- Layout/EmptyLinesAroundAttributeAccessor:
8
- Enabled: true
9
- Layout/SpaceAroundMethodCallOperator:
10
- Enabled: true
11
70
 
12
- Lint/DeprecatedOpenSSLConstant:
13
- Enabled: true
14
- Lint/MixedRegexpCaptureTypes:
15
- Enabled: true
16
- Lint/RaiseException:
17
- Enabled: true
18
- Lint/StructNewOverride:
19
- Enabled: true
71
+ Style/WordArray:
72
+ Enabled: false
20
73
 
21
- Style/ExponentialNotation:
22
- Enabled: true
23
- Style/HashEachMethods:
24
- Enabled: true
25
- Style/HashTransformKeys:
26
- Enabled: true
27
- Style/HashTransformValues:
28
- Enabled: true
29
- Style/RedundantFetchBlock:
30
- Enabled: true
31
- Style/RedundantRegexpCharacterClass:
32
- Enabled: true
33
- Style/RedundantRegexpEscape:
34
- Enabled: true
35
- Style/SlicingWithRange:
74
+ Style/RedundantSelf:
75
+ Enabled: false
76
+
77
+ Layout/HashAlignment:
36
78
  Enabled: true
79
+ EnforcedLastArgumentHashStyle: always_ignore
37
80
 
38
- Metrics/ParameterLists:
39
- CountKeywordArgs: false
40
- Metrics/BlockLength:
41
- Exclude:
42
- - spec/**/*
81
+ Style/TrivialAccessors:
82
+ AllowPredicates: true
data/CHANGELOG.md CHANGED
@@ -1,20 +1,48 @@
1
1
  # Changelog
2
2
 
3
- ## [v0.1.0](https://github.com/excpt/moments/tree/v0.1.0) (2020-08-18)
3
+ ## [v0.3.0](https://github.com/excpt/moments/tree/v0.3.0) (2023-11-29)
4
4
 
5
- [Full Changelog](https://github.com/excpt/moments/compare/v0.0.2.alpha...v0.1.0)
5
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.2.0...v0.3.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Precision? [\#11](https://github.com/excpt/moments/issues/11)
6
10
 
7
11
  **Closed issues:**
8
12
 
9
- - Replace Travis CI with another CI [\#3](https://github.com/excpt/moments/issues/3)
13
+ - Add documentation for new precision parameter [\#13](https://github.com/excpt/moments/issues/13)
10
14
 
11
15
  **Merged pull requests:**
12
16
 
13
- - Fix `\#in\_\*` for times with miliseconds [\#6](https://github.com/excpt/moments/pull/6) ([AlexWayfer](https://github.com/AlexWayfer))
14
- - Fix diffs with different timezones, `Date` diffs and `\#in\_years` [\#5](https://github.com/excpt/moments/pull/5) ([AlexWayfer](https://github.com/AlexWayfer))
15
- - Add `\#in\_\*` methods, like `\#in\_seconds` [\#4](https://github.com/excpt/moments/pull/4) ([AlexWayfer](https://github.com/AlexWayfer))
17
+ - A few enhancements [\#15](https://github.com/excpt/moments/pull/15) ([Kris-LIBIS](https://github.com/Kris-LIBIS))
18
+ - Intruduce optional mode parameter [\#12](https://github.com/excpt/moments/pull/12) ([excpt](https://github.com/excpt))
19
+
20
+ ## [v0.2.0](https://github.com/excpt/moments/tree/v0.2.0) (2022-05-29)
21
+
22
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.1.0...v0.2.0)
23
+
24
+ **Closed issues:**
25
+
26
+ - Make a new release [\#7](https://github.com/excpt/moments/issues/7)
27
+
28
+ **Merged pull requests:**
29
+
30
+ - Parse string input whenever possible [\#10](https://github.com/excpt/moments/pull/10) ([RomanVanLoo](https://github.com/RomanVanLoo))
31
+ - Add `in_weeks` [\#9](https://github.com/excpt/moments/pull/9) ([RomanVanLoo](https://github.com/RomanVanLoo))
32
+ - Release 0.1.0 [\#8](https://github.com/excpt/moments/pull/8) ([excpt](https://github.com/excpt))
33
+ - Fix `#in_*` for times with miliseconds [\#6](https://github.com/excpt/moments/pull/6) ([AlexWayfer](https://github.com/AlexWayfer))
34
+ - Fix diffs with different timezones, `Date` diffs and `#in_years` [\#5](https://github.com/excpt/moments/pull/5) ([AlexWayfer](https://github.com/AlexWayfer))
35
+ - Add `#in_*` methods, like `#in_seconds` [\#4](https://github.com/excpt/moments/pull/4) ([AlexWayfer](https://github.com/AlexWayfer))
16
36
  - Many improvements [\#2](https://github.com/excpt/moments/pull/2) ([AlexWayfer](https://github.com/AlexWayfer))
17
37
 
38
+ ## [v0.1.0](https://github.com/excpt/moments/tree/v0.1.0) (2020-08-18)
39
+
40
+ [Full Changelog](https://github.com/excpt/moments/compare/v0.0.2.alpha...v0.1.0)
41
+
42
+ **Closed issues:**
43
+
44
+ - Replace Travis CI with another CI [\#3](https://github.com/excpt/moments/issues/3)
45
+
18
46
  ## [v0.0.2.alpha](https://github.com/excpt/moments/tree/v0.0.2.alpha) (2014-11-14)
19
47
 
20
48
  [Full Changelog](https://github.com/excpt/moments/compare/v0.0.1.alpha...v0.0.2.alpha)
data/README.md CHANGED
@@ -1,11 +1,5 @@
1
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)
4
- [![Build Status](https://travis-ci.org/excpt/moments.svg?branch=master)](https://travis-ci.org/excpt/moments)
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)
8
-
9
3
  Handles time differences and calculations.
10
4
 
11
5
  ## Installation
@@ -44,6 +38,25 @@ puts diff.to_hash
44
38
  puts diff.in_months
45
39
  # 67 (5 * 12 + 7)
46
40
  # there are also methods for each component
41
+
42
+ from = Time.new(2022, 8, 4, 15, 52, 25, '-06:00')
43
+ to = Time.new(2022, 8, 18, 15, 52, 31, '-06:00')
44
+
45
+ difference = Moments.difference(from, to, :precise).in_minutes
46
+ # returns: 20_160.1
47
+ # supports: in_weeks, in_days, in_hours, in_minutes
48
+
49
+ ago = Moments.ago t1
50
+
51
+ puts ago.to_hash
52
+ # { years: 5, months: 7, days: 5, hours: 19, minutes: 29, seconds: 6 }
53
+
54
+ puts ago.humanized
55
+ # 5 years, 7 months, 5 days, 19 hours, 29 minutes and 6 seconds
56
+
57
+ # #humanized takes care of signularity and omits 0 values
58
+ puts Moments.difference(t1, Time.new(2015, 2, 1, 19, 0, 1)).humanized
59
+ # 1 month, 19 hours and 1 second
47
60
  ```
48
61
 
49
62
  ## Contributing
@@ -21,12 +21,15 @@ module Moments
21
21
 
22
22
  # == Parameters:
23
23
  # from::
24
- # A instance of Time
24
+ # An instance of Time
25
25
  # to::
26
- # A instance of Time
27
- def initialize(from, to)
26
+ # An instance of Time
27
+ # precise::
28
+ # Option to return minutes, hours, days, years as decimals intead of integer
29
+ def initialize(from, to, mode = :normal)
28
30
  @from = parse_argument from
29
31
  @to = parse_argument to
32
+ @precise = mode == :precise
30
33
 
31
34
  @ordered_from, @ordered_to = [@from, @to].sort
32
35
 
@@ -54,22 +57,26 @@ module Moments
54
57
  end
55
58
 
56
59
  def in_minutes
57
- in_seconds / 60
60
+ in_seconds / (@precise ? 60.0 : 60)
58
61
  end
59
62
 
60
63
  def in_hours
61
- in_minutes / 60
64
+ in_minutes / (@precise ? 60.0 : 60)
62
65
  end
63
66
 
64
67
  def in_days
65
- in_hours / 24
68
+ in_hours / (@precise ? 24.0 : 24)
69
+ end
70
+
71
+ def in_weeks
72
+ in_days / (@precise ? 7.0 : 7)
66
73
  end
67
74
 
68
75
  def in_months
69
76
  months_diff = @ordered_to.month - @ordered_from.month
70
77
  months_diff -= 1 if months_diff.positive? && @ordered_to.mday < @ordered_from.mday
71
78
 
72
- (@ordered_to.year - @ordered_from.year) * 12 + months_diff
79
+ ((@ordered_to.year - @ordered_from.year) * 12) + months_diff
73
80
  end
74
81
 
75
82
  def in_years
@@ -85,6 +92,16 @@ module Moments
85
92
  years_diff
86
93
  end
87
94
 
95
+ def humanized
96
+ diff_parts = to_hash.each_with_object([]) do |(unit, quantity), parts|
97
+ parts << "#{quantity} #{quantity == 1 ? unit.to_s[0..-2] : unit}" if quantity.positive?
98
+ end
99
+ last_part = diff_parts.pop
100
+ return last_part if diff_parts.empty?
101
+
102
+ [diff_parts.join(', '), last_part].join(' and ')
103
+ end
104
+
88
105
  private
89
106
 
90
107
  TIME_CLASSES = [Time, DateTime].freeze
@@ -96,12 +113,22 @@ module Moments
96
113
  when *TIME_CLASSES
97
114
  value.to_time.getutc
98
115
  when Date
99
- value
116
+ value.to_time
117
+ when String
118
+ begin
119
+ Time.parse(value).getutc
120
+ rescue ArgumentError
121
+ unsupported_format
122
+ end
100
123
  else
101
- raise ArgumentError, 'Unsupported format'
124
+ unsupported_format
102
125
  end
103
126
  end
104
127
 
128
+ def unsupported_format
129
+ raise ArgumentError, 'Unsupported format'
130
+ end
131
+
105
132
  def precise_difference
106
133
  @diff = calculate_diff
107
134
 
@@ -11,7 +11,7 @@ module Moments
11
11
  # major version
12
12
  MAJOR = 0
13
13
  # minor version
14
- MINOR = 1
14
+ MINOR = 3
15
15
  # patch version
16
16
  PATCH = 0
17
17
  # alpha, beta, etc. tag
data/lib/moments.rb CHANGED
@@ -5,7 +5,11 @@ require_relative 'moments/difference'
5
5
 
6
6
  # Entrypoint for the moments gem
7
7
  module Moments
8
- def self.difference(from, to)
9
- Moments::Difference.new from, to
8
+ def self.difference(from, to, mode = :normal)
9
+ Moments::Difference.new from, to, mode
10
+ end
11
+
12
+ def self.ago(from, mode = :normal)
13
+ Moments::Difference.new from, Time.now, mode
10
14
  end
11
15
  end
data/moments.gemspec CHANGED
@@ -11,15 +11,16 @@ Gem::Specification.new do |spec|
11
11
  spec.description = ''
12
12
  spec.homepage = 'https://github.com/excpt/moments'
13
13
  spec.license = 'MIT'
14
+ spec.metadata['rubygems_mfa_required'] = 'true'
14
15
 
15
16
  spec.files = `git ls-files -z`.split("\x0")
16
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.add_development_dependency 'bundler', '>= 1', '< 3'
21
- spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
22
- spec.add_development_dependency 'rake', '~> 13.0'
23
- spec.add_development_dependency 'rspec', '~> 3.0'
24
- spec.add_development_dependency 'rubocop', '~> 0.86.0'
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'
25
+ spec.add_development_dependency 'timecop'
25
26
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Moments::Difference do
4
+ # Thu, 04 Aug 2022 15:52:25 MDT -06:00
5
+ let(:from) { Time.new(2022, 8, 4, 15, 52, 25, '-06:00') }
6
+ # Thu, 18 Aug 2022 15:52:31 MDT -06:00
7
+ let(:to) { Time.new(2022, 8, 18, 15, 52, 31, '-06:00') }
8
+
9
+ describe 'minutes' do
10
+ context 'precision' do
11
+ subject { Moments.difference(from, to, :precise).in_minutes }
12
+
13
+ it { should eq 20_160.1 }
14
+ end
15
+ end
16
+
17
+ describe 'hours' do
18
+ context 'precision' do
19
+ subject { Moments.difference(from, to, :precise).in_hours }
20
+
21
+ it { should eq 336.001666666666667 }
22
+ end
23
+ end
24
+
25
+ describe 'days' do
26
+ context 'precision' do
27
+ subject { Moments.difference(from, to, :precise).in_days }
28
+
29
+ it { should eq 14.000069444444444 }
30
+ end
31
+ end
32
+
33
+ describe 'weeks' do
34
+ context 'precision' do
35
+ subject { Moments.difference(from, to, :precise).in_weeks }
36
+
37
+ it { should eq 2.0000099206349207 }
38
+ end
39
+ end
40
+ end
@@ -333,7 +333,10 @@ describe Moments::Difference do
333
333
  {
334
334
  years: 6,
335
335
  months: 5,
336
- days: 4
336
+ days: 4,
337
+ hours: 0,
338
+ minutes: 0,
339
+ seconds: 0
337
340
  }
338
341
  end
339
342
 
@@ -444,14 +447,7 @@ describe Moments::Difference do
444
447
  end
445
448
  end
446
449
 
447
- shared_examples 'in a component' do |
448
- when_seconds:,
449
- when_minutes:,
450
- when_hours:,
451
- when_days:,
452
- when_months:,
453
- when_years:
454
- |
450
+ shared_examples 'in a component' do |when_seconds:, when_minutes:, when_hours:, when_days:, when_months:, when_years:|
455
451
  context 'with equal dates' do
456
452
  let(:to) { from }
457
453
 
@@ -668,23 +664,23 @@ describe Moments::Difference do
668
664
  subject { Moments::Difference.new(from, to).in_seconds }
669
665
 
670
666
  include_examples 'in a component',
671
- when_seconds: 15,
672
- when_minutes: 12 * 60 + 15,
673
- when_hours: 8 * 60 * 60 + 12 * 60 + 15,
674
- when_days: 6 * 24 * 60 * 60 + 8 * 60 * 60 + 12 * 60 + 15,
675
- when_months:
676
- (31 + 28 + 31 + 30) * 24 * 60 * 60 +
677
- 6 * 24 * 60 * 60 +
678
- 8 * 60 * 60 +
679
- 12 * 60 +
680
- 15,
681
- when_years:
682
- 2 * 365 * 24 * 60 * 60 +
683
- (31 + 28 + 31 + 30) * 24 * 60 * 60 +
684
- 6 * 24 * 60 * 60 +
685
- 8 * 60 * 60 +
686
- 12 * 60 +
687
- 15
667
+ when_seconds: 15,
668
+ when_minutes: (12 * 60) + 15,
669
+ when_hours: (8 * 60 * 60) + (12 * 60) + 15,
670
+ when_days: (6 * 24 * 60 * 60) + (8 * 60 * 60) + (12 * 60) + 15,
671
+ when_months:
672
+ ((31 + 28 + 31 + 30) * 24 * 60 * 60) +
673
+ (6 * 24 * 60 * 60) +
674
+ (8 * 60 * 60) +
675
+ (12 * 60) +
676
+ 15,
677
+ when_years:
678
+ (2 * 365 * 24 * 60 * 60) +
679
+ ((31 + 28 + 31 + 30) * 24 * 60 * 60) +
680
+ (6 * 24 * 60 * 60) +
681
+ (8 * 60 * 60) +
682
+ (12 * 60) +
683
+ 15
688
684
 
689
685
  context 'with miliseconds' do
690
686
  context 'when `to` is a bit greater' do
@@ -721,81 +717,81 @@ describe Moments::Difference do
721
717
  subject { Moments::Difference.new(from, to).in_minutes }
722
718
 
723
719
  include_examples 'in a component',
724
- when_seconds: 0,
725
- when_minutes: 12,
726
- when_hours: 8 * 60 + 12,
727
- when_days: 6 * 24 * 60 + 8 * 60 + 12,
728
- when_months:
729
- (31 + 28 + 31 + 30) * 24 * 60 +
730
- 6 * 24 * 60 +
731
- 8 * 60 +
732
- 12,
733
- when_years:
734
- 2 * 365 * 24 * 60 +
735
- (31 + 28 + 31 + 30) * 24 * 60 +
736
- 6 * 24 * 60 +
737
- 8 * 60 +
738
- 12
720
+ when_seconds: 0,
721
+ when_minutes: 12,
722
+ when_hours: (8 * 60) + 12,
723
+ when_days: (6 * 24 * 60) + (8 * 60) + 12,
724
+ when_months:
725
+ ((31 + 28 + 31 + 30) * 24 * 60) +
726
+ (6 * 24 * 60) +
727
+ (8 * 60) +
728
+ 12,
729
+ when_years:
730
+ (2 * 365 * 24 * 60) +
731
+ ((31 + 28 + 31 + 30) * 24 * 60) +
732
+ (6 * 24 * 60) +
733
+ (8 * 60) +
734
+ 12
739
735
  end
740
736
 
741
737
  context '#in_hours' do
742
738
  subject { Moments::Difference.new(from, to).in_hours }
743
739
 
744
740
  include_examples 'in a component',
745
- when_seconds: 0,
746
- when_minutes: 0,
747
- when_hours: 8,
748
- when_days: 6 * 24 + 8,
749
- when_months:
750
- (31 + 28 + 31 + 30) * 24 +
751
- 6 * 24 +
752
- 8,
753
- when_years:
754
- 2 * 365 * 24 +
755
- (31 + 28 + 31 + 30) * 24 +
756
- 6 * 24 +
757
- 8
741
+ when_seconds: 0,
742
+ when_minutes: 0,
743
+ when_hours: 8,
744
+ when_days: (6 * 24) + 8,
745
+ when_months:
746
+ ((31 + 28 + 31 + 30) * 24) +
747
+ (6 * 24) +
748
+ 8,
749
+ when_years:
750
+ (2 * 365 * 24) +
751
+ ((31 + 28 + 31 + 30) * 24) +
752
+ (6 * 24) +
753
+ 8
758
754
  end
759
755
 
760
756
  context '#in_days' do
761
757
  subject { Moments::Difference.new(from, to).in_days }
762
758
 
763
759
  include_examples 'in a component',
764
- when_seconds: 0,
765
- when_minutes: 0,
766
- when_hours: 0,
767
- when_days: 6,
768
- when_months:
769
- (31 + 28 + 31 + 30) +
770
- 6,
771
- when_years:
772
- 2 * 365 +
773
- (31 + 28 + 31 + 30) +
774
- 6
760
+ when_seconds: 0,
761
+ when_minutes: 0,
762
+ when_hours: 0,
763
+ when_days: 6,
764
+ when_months:
765
+ (31 + 28 + 31 + 30) +
766
+ 6,
767
+ when_years:
768
+ (2 * 365) +
769
+ (31 + 28 + 31 + 30) +
770
+ 6
775
771
  end
776
772
 
777
773
  context '#in_months' do
778
774
  subject { Moments::Difference.new(from, to).in_months }
779
775
 
780
776
  include_examples 'in a component',
781
- when_seconds: 0,
782
- when_minutes: 0,
783
- when_hours: 0,
784
- when_days: 0,
785
- when_months: 4,
786
- when_years: 2 * 12 + 4
777
+ when_seconds: 0,
778
+ when_minutes: 0,
779
+ when_hours: 0,
780
+ when_days: 0,
781
+ when_months: 4,
782
+ when_years: (2 * 12) + 4
787
783
  end
788
784
 
789
785
  context '#in_years' do
790
786
  subject { Moments::Difference.new(from, to).in_years }
791
787
 
792
788
  include_examples 'in a component',
793
- when_seconds: 0,
794
- when_minutes: 0,
795
- when_hours: 0,
796
- when_days: 0,
797
- when_months: 0,
798
- when_years: 2
789
+ when_seconds: 0,
790
+ when_minutes: 0,
791
+ when_hours: 0,
792
+ when_days: 0,
793
+ when_months: 0,
794
+ when_years: 2
799
795
 
800
796
  context 'when `to` day is greater than `from` day' do
801
797
  let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
@@ -811,4 +807,39 @@ describe Moments::Difference do
811
807
  it { is_expected.to eq 1 }
812
808
  end
813
809
  end
810
+
811
+ context '#humanized' do
812
+ subject { Moments::Difference.new(from, to).humanized }
813
+
814
+ let (:from) { Time.utc 2020, 1, 1, 0, 0, 0 }
815
+
816
+ {
817
+ [2021, 1, 1, 0, 0, 0] => '1 year',
818
+ [2020, 2, 1, 0, 0, 0] => '1 month',
819
+ [2020, 1, 2, 0, 0, 0] => '1 day',
820
+ [2020, 1, 1, 1, 0, 0] => '1 hour',
821
+ [2020, 1, 1, 0, 1, 0] => '1 minute',
822
+ [2020, 1, 1, 0, 0, 1] => '1 second',
823
+ [2022, 1, 1, 0, 0, 0] => '2 years',
824
+ [2020, 4, 1, 0, 0, 0] => '3 months',
825
+ [2020, 1, 5, 0, 0, 0] => '4 days',
826
+ [2020, 1, 1, 5, 0, 0] => '5 hours',
827
+ [2020, 1, 1, 0, 6, 0] => '6 minutes',
828
+ [2020, 1, 1, 0, 0, 7] => '7 seconds',
829
+ [2021, 2, 1, 0, 0, 0] => '1 year and 1 month',
830
+ [2021, 1, 2, 0, 0, 0] => '1 year and 1 day',
831
+ [2021, 1, 1, 1, 0, 0] => '1 year and 1 hour',
832
+ [2021, 1, 1, 0, 1, 0] => '1 year and 1 minute',
833
+ [2021, 1, 1, 0, 0, 1] => '1 year and 1 second',
834
+ [2021, 2, 2, 0, 0, 0] => '1 year, 1 month and 1 day',
835
+ [2021, 2, 1, 1, 0, 0] => '1 year, 1 month and 1 hour',
836
+ [2022, 4, 5, 5, 6, 7] => '2 years, 3 months, 4 days, 5 hours, 6 minutes and 7 seconds'
837
+ }.each do |time_array, string|
838
+ context "with #{string}" do
839
+ let (:to) { Time.utc *time_array }
840
+
841
+ it { is_expected.to eq string}
842
+ end
843
+ end
844
+ end
814
845
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'timecop'
4
+
3
5
  describe Moments do
4
6
  describe '#difference' do
5
7
  subject { Moments.difference(from, to) }
@@ -9,4 +11,39 @@ describe Moments do
9
11
 
10
12
  it { is_expected.to be_a Moments::Difference }
11
13
  end
14
+
15
+ describe '#ago' do
16
+ subject { Moments.ago(from) }
17
+
18
+ before do
19
+ Timecop.freeze(to)
20
+ end
21
+
22
+ after do
23
+ Timecop.return
24
+ end
25
+
26
+ let(:from) { Time.utc 2010, 1, 1, 0, 0, 0 }
27
+ let(:to) { Time.utc 2016, 6, 5, 3, 2, 1 }
28
+
29
+ it { is_expected.to be_a Moments::Difference }
30
+
31
+ context '#to_hash' do
32
+
33
+ subject { Moments.ago(from).to_hash }
34
+ let (:expected_result) do
35
+ {
36
+ years: 6,
37
+ months: 5,
38
+ days: 4,
39
+ hours: 3,
40
+ minutes: 2,
41
+ seconds: 1
42
+ }
43
+ end
44
+
45
+ it { is_expected.to eq expected_result}
46
+ end
47
+
48
+ end
12
49
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'codeclimate-test-reporter'
4
-
5
3
  require_relative '../lib/moments'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Rudat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-18 00:00:00.000000000 Z
11
+ date: 2023-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,76 +16,84 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '3'
19
+ version: '0'
23
20
  type: :development
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '1'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '3'
26
+ version: '0'
33
27
  - !ruby/object:Gem::Dependency
34
- name: codeclimate-test-reporter
28
+ name: github_changelog_generator
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
- - - "~>"
31
+ - - ">="
38
32
  - !ruby/object:Gem::Version
39
- version: '1.0'
33
+ version: '0'
40
34
  type: :development
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
- - - "~>"
38
+ - - ">="
45
39
  - !ruby/object:Gem::Version
46
- version: '1.0'
40
+ version: '0'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: rake
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
- - - "~>"
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: '13.0'
47
+ version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
- - - "~>"
52
+ - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: '13.0'
54
+ version: '0'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: rspec
63
57
  requirement: !ruby/object:Gem::Requirement
64
58
  requirements:
65
- - - "~>"
59
+ - - ">="
66
60
  - !ruby/object:Gem::Version
67
- version: '3.0'
61
+ version: '0'
68
62
  type: :development
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
- - - "~>"
66
+ - - ">="
73
67
  - !ruby/object:Gem::Version
74
- version: '3.0'
68
+ version: '0'
75
69
  - !ruby/object:Gem::Dependency
76
70
  name: rubocop
77
71
  requirement: !ruby/object:Gem::Requirement
78
72
  requirements:
79
- - - "~>"
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
80
88
  - !ruby/object:Gem::Version
81
- version: 0.86.0
89
+ version: '0'
82
90
  type: :development
83
91
  prerelease: false
84
92
  version_requirements: !ruby/object:Gem::Requirement
85
93
  requirements:
86
- - - "~>"
94
+ - - ">="
87
95
  - !ruby/object:Gem::Version
88
- version: 0.86.0
96
+ version: '0'
89
97
  description: ''
90
98
  email:
91
99
  - timrudat@gmail.com
@@ -107,13 +115,15 @@ files:
107
115
  - lib/moments/difference.rb
108
116
  - lib/moments/version.rb
109
117
  - moments.gemspec
118
+ - spec/lib/moments/difference_precision_spec.rb
110
119
  - spec/lib/moments/difference_spec.rb
111
120
  - spec/lib/moments_spec.rb
112
121
  - spec/spec_helper.rb
113
122
  homepage: https://github.com/excpt/moments
114
123
  licenses:
115
124
  - MIT
116
- metadata: {}
125
+ metadata:
126
+ rubygems_mfa_required: 'true'
117
127
  post_install_message:
118
128
  rdoc_options: []
119
129
  require_paths:
@@ -129,11 +139,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
139
  - !ruby/object:Gem::Version
130
140
  version: '0'
131
141
  requirements: []
132
- rubygems_version: 3.1.2
142
+ rubygems_version: 3.4.22
133
143
  signing_key:
134
144
  specification_version: 4
135
145
  summary: Handles time differences.
136
- test_files:
137
- - spec/lib/moments/difference_spec.rb
138
- - spec/lib/moments_spec.rb
139
- - spec/spec_helper.rb
146
+ test_files: []