moments 0.2.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: 0b85363c9163c9935f75833c74b4e7d587cc70d962ca4f86e14e6d8a3bb14196
4
- data.tar.gz: 1a6421e682139a2cefb0b82d12bcd5d08cb2f8220ab20a65f9646938e0ee1653
3
+ metadata.gz: 77dbf9115233ea35f1e79c6592da59fde4ec415e7b7a74400a9789a407cd3bdf
4
+ data.tar.gz: fae4d1e2330151cfc27cb64d63bc10a9d5123e0dd6005a6dfebaa4b77038fd32
5
5
  SHA512:
6
- metadata.gz: 360367e75466166fc7bb786fa4d0247b23d7ba61b118286bc137c7b697ef25a12bf3d847f0b2907de9a899ee1d3132298b5196ef93387b536e800fca8132c24a
7
- data.tar.gz: e374f88fd8029894bdeee1e0ea79ae58188552fe43438dfb56e37335a36121b91c6b14e0d8b7a814ff0901c10c9928916051f1ea38a1b28997bce3ff47f932a9
6
+ metadata.gz: b4e344eafc3286656abb581eef7cffdc21278b58a7b24c62e7e5e3d64bbb28f213e7fd1eb7e518bc10887336deecb6cf63a3ab4754a7712bedf667f68714530a
7
+ data.tar.gz: 6c0d50f01f547d3a6b219e5f2d2c34cd2ad277f6a4ec17016fc047280f740e9123c00e60a0ae0f06a503d84e78edae22b709d0d0167345a97e801bbed5536fcc
data/.rubocop.yml CHANGED
@@ -7,6 +7,7 @@ AllCops:
7
7
  Exclude:
8
8
  - 'gemfiles/*.gemfile'
9
9
  - 'vendor/**/*'
10
+ - 'spec/**/*'
10
11
 
11
12
  Style/Documentation:
12
13
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,8 +1,25 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased](https://github.com/excpt/moments/tree/HEAD)
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.1.0...HEAD)
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)
10
+
11
+ **Closed issues:**
12
+
13
+ - Add documentation for new precision parameter [\#13](https://github.com/excpt/moments/issues/13)
14
+
15
+ **Merged pull requests:**
16
+
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)
6
23
 
7
24
  **Closed issues:**
8
25
 
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,19 +57,19 @@ 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)
66
69
  end
67
70
 
68
71
  def in_weeks
69
- in_days / 7
72
+ in_days / (@precise ? 7.0 : 7)
70
73
  end
71
74
 
72
75
  def in_months
@@ -89,6 +92,16 @@ module Moments
89
92
  years_diff
90
93
  end
91
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
+
92
105
  private
93
106
 
94
107
  TIME_CLASSES = [Time, DateTime].freeze
@@ -11,7 +11,7 @@ module Moments
11
11
  # major version
12
12
  MAJOR = 0
13
13
  # minor version
14
- MINOR = 2
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
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec'
24
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
@@ -807,4 +807,39 @@ describe Moments::Difference do
807
807
  it { is_expected.to eq 1 }
808
808
  end
809
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
810
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
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.2.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: 2022-05-29 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
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: ''
84
98
  email:
85
99
  - timrudat@gmail.com
@@ -101,6 +115,7 @@ files:
101
115
  - lib/moments/difference.rb
102
116
  - lib/moments/version.rb
103
117
  - moments.gemspec
118
+ - spec/lib/moments/difference_precision_spec.rb
104
119
  - spec/lib/moments/difference_spec.rb
105
120
  - spec/lib/moments_spec.rb
106
121
  - spec/spec_helper.rb
@@ -124,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
139
  - !ruby/object:Gem::Version
125
140
  version: '0'
126
141
  requirements: []
127
- rubygems_version: 3.3.7
142
+ rubygems_version: 3.4.22
128
143
  signing_key:
129
144
  specification_version: 4
130
145
  summary: Handles time differences.