dotiw 4.0.0 → 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +45 -0
- data/.rspec +2 -0
- data/Appraisals +19 -0
- data/CHANGELOG.md +38 -6
- data/Gemfile +2 -0
- data/MIT-LICENSE +1 -1
- data/README.markdown +151 -63
- data/Rakefile +3 -1
- data/benchmarks/time_hash_bench.rb +19 -0
- data/dotiw.gemspec +17 -19
- data/gemfiles/rails_4.gemfile +5 -3
- data/gemfiles/rails_5.0.gemfile +5 -3
- data/gemfiles/rails_5.1.gemfile +5 -3
- data/gemfiles/rails_5.2.gemfile +9 -0
- data/gemfiles/rails_6.0.gemfile +9 -0
- data/lib/dotiw.rb +33 -14
- data/lib/dotiw/action_view/helpers/date_helper.rb +32 -0
- data/lib/dotiw/locale/ar.yml +104 -0
- data/lib/dotiw/locale/da.yml +35 -0
- data/lib/dotiw/locale/de.yml +36 -1
- data/lib/dotiw/locale/en.yml +35 -0
- data/lib/dotiw/locale/es.yml +36 -1
- data/lib/dotiw/locale/fr.yml +35 -0
- data/lib/dotiw/locale/id.yml +35 -0
- data/lib/dotiw/locale/it.yml +35 -0
- data/lib/dotiw/locale/ja.yml +35 -0
- data/lib/dotiw/locale/ko.yml +48 -0
- data/lib/dotiw/locale/nb.yml +36 -1
- data/lib/dotiw/locale/nl.yml +35 -0
- data/lib/dotiw/locale/pl.yml +42 -0
- data/lib/dotiw/locale/pt-BR.yml +35 -0
- data/lib/dotiw/locale/ru.yml +37 -0
- data/lib/dotiw/locale/sv.yml +48 -0
- data/lib/dotiw/locale/vi.yml +48 -0
- data/lib/dotiw/locale/zh-CN.yml +48 -0
- data/lib/dotiw/locale/zh-TW.yml +60 -0
- data/lib/dotiw/methods.rb +108 -0
- data/lib/dotiw/time_hash.rb +52 -40
- data/lib/dotiw/version.rb +2 -2
- data/spec/lib/dotiw_spec.rb +303 -159
- data/spec/lib/i18n/ar.yml +2 -0
- data/spec/lib/i18n/da.yml +2 -0
- data/spec/lib/i18n/de.yml +2 -0
- data/spec/lib/i18n/en.yml +2 -0
- data/spec/lib/i18n/es.yml +3 -0
- data/spec/lib/i18n/fr.yml +2 -0
- data/spec/lib/i18n/id.yml +2 -0
- data/spec/lib/i18n/it.yml +3 -0
- data/spec/lib/i18n/ja.yml +2 -0
- data/spec/lib/i18n/ko.yml +2 -0
- data/spec/lib/i18n/nb.yml +2 -0
- data/spec/lib/i18n/nl.yml +2 -0
- data/spec/lib/i18n/pl.yml +2 -0
- data/spec/lib/i18n/pt-BR.yml +2 -0
- data/spec/lib/i18n/ru.yml +5 -0
- data/spec/lib/i18n/sv.yml +2 -0
- data/spec/lib/i18n/vi.yml +22 -0
- data/spec/lib/i18n/zh-CN.yml +2 -0
- data/spec/lib/i18n/zh-TW.yml +2 -0
- data/spec/spec_helper.rb +2 -9
- metadata +79 -31
- data/.travis.yml +0 -27
- data/lib/dotiw/action_view_ext/helpers/date_helper.rb +0 -103
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e1c8a832bcdb740a88002dec2f5c843daeca75b2674b2e31c26abc17a144c6a2
|
4
|
+
data.tar.gz: 163a34f6e15e34026375176118399cc65ed73b2f723dab436bc9aedaf36dc0b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e1a5291d51d629e4fc27d3f915f84586089be0869a676107154ef5f865c46fac768c4a2e7d86ac5fdfc2aadc879b668923f130f2f75ad0c355cbb86a5b18cc4
|
7
|
+
data.tar.gz: f575e5e232a5adfefd8f81aa15f132dc352d020233bd1f66f6cddca473031d5cce0abf76bfd0842e0b55c3c60d4a44e2e7cf56a28b6e6fc89245e9fd17705bd7
|
@@ -0,0 +1,45 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
branches:
|
9
|
+
- master
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
build:
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
strategy:
|
15
|
+
fail-fast: false
|
16
|
+
matrix:
|
17
|
+
include:
|
18
|
+
- ruby-version: 2.5
|
19
|
+
- ruby-version: 2.6
|
20
|
+
- ruby-version: 2.4
|
21
|
+
bundler-version: 1.17.3
|
22
|
+
gemfile: gemfiles/rails_4.gemfile
|
23
|
+
- ruby-version: 2.6
|
24
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
25
|
+
- ruby-version: 2.6
|
26
|
+
gemfile: gemfiles/rails_5.1.gemfile
|
27
|
+
- ruby-version: 2.6
|
28
|
+
gemfile: gemfiles/rails_5.2.gemfile
|
29
|
+
- ruby-version: 2.6
|
30
|
+
gemfile: gemfiles/rails_6.0.gemfile
|
31
|
+
steps:
|
32
|
+
- uses: actions/checkout@v2
|
33
|
+
- name: Set up Ruby
|
34
|
+
uses: ruby/setup-ruby@v1
|
35
|
+
with:
|
36
|
+
ruby-version: ${{ matrix.ruby-version }}
|
37
|
+
- name: Build and test
|
38
|
+
env:
|
39
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
40
|
+
BUNDLER: ${{ matrix.bundler-version }}
|
41
|
+
run: |
|
42
|
+
gem uninstall bundler
|
43
|
+
gem install bundler -v ${BUNDLER:-2.1.4}
|
44
|
+
bundle install --jobs 4 --retry 3
|
45
|
+
bundle exec rake
|
data/.rspec
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
appraise 'rails_4' do
|
2
|
+
gem 'rails', '~> 4.0'
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise 'rails_5.0' do
|
6
|
+
gem 'rails', '~> 5.0.0'
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise 'rails_5.1' do
|
10
|
+
gem 'rails', '~> 5.1.0'
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise 'rails_5.2' do
|
14
|
+
gem 'rails', '~> 5.2.0'
|
15
|
+
end
|
16
|
+
|
17
|
+
appraise 'rails_6.0' do
|
18
|
+
gem 'rails', '~> 6.0.0'
|
19
|
+
end
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,42 @@
|
|
1
|
+
## 5.3.0 (2021/03/18)
|
2
|
+
|
3
|
+
* [#115](https://github.com/radar/distance_of_time_in_words/pull/115): Use constants for time durations (2x faster, 3.5x less memory) - [@krzysiek1507](https://github.com/krzysiek1507).
|
4
|
+
* [#117](https://github.com/radar/distance_of_time_in_words/pull/117): Support `#distance_of_time_in_words_to_now` with `vague: true` - [@joshuapinter](https://github.com/joshuapinter).
|
5
|
+
* [#118](https://github.com/radar/distance_of_time_in_words/pull/118): Raise `ArgumentError` when `nil` is passed for time, to match original `distance_of_time_in_words` - [@joshuapinter](https://github.com/joshuapinter).
|
6
|
+
* [#119](https://github.com/radar/distance_of_time_in_words/pull/119): Do not mutate input options - [@joshuapinter](https://github.com/joshuapinter).
|
7
|
+
|
8
|
+
## 5.2.0 (2020/10/03)
|
9
|
+
|
10
|
+
* [#94](https://github.com/radar/distance_of_time_in_words/pull/94): Add :compact formatting option - [@booty](https://github.com/booty).
|
11
|
+
* [#112](https://github.com/radar/distance_of_time_in_words/pull/112): Add Swedish language support - [@davidwessman](https://github.com/davidwessman).
|
12
|
+
* [#114](https://github.com/radar/distance_of_time_in_words/pull/114): Add Traditional Chinese (Taiwan) language support - [@sibevin](https://github.com/sibevin).
|
13
|
+
|
14
|
+
## 5.1.0 (2020/08/05)
|
15
|
+
|
16
|
+
* [#111](https://github.com/radar/distance_of_time_in_words/pull/111): Fix: `NoMethodError` when calling `ActionController::Base.helpers.distance_of_time_in_words` - [@denisahearn](https://github.com/denisahearn).
|
17
|
+
* [#106](https://github.com/radar/distance_of_time_in_words/pull/106): Add Vietnamese language support - [@runlevel5 ](https://github.com/runlevel5).
|
18
|
+
|
19
|
+
## 5.0.0 (2020/05/07)
|
20
|
+
|
21
|
+
* [#105](https://github.com/radar/distance_of_time_in_words/pull/105): Support for Ruby w/o Rails - [@dblock](https://github.com/dblock).
|
22
|
+
* [#108](https://github.com/radar/distance_of_time_in_words/pull/108): Fix negative weeks - [@ivanovaleksey](https://github.com/ivanovaleksey).
|
23
|
+
|
24
|
+
## 4.0.1 (2018/06/01)
|
25
|
+
|
1
26
|
## 3.1.1 (2016/03/08)
|
2
|
-
* Add CHANGELOG.md
|
3
27
|
|
4
28
|
## 3.1.0 (2016/03/07)
|
5
29
|
|
6
|
-
* [#68](https://github.com/radar/dotiw/pull/68): Add support for weeks - [@lauranjansen](https://github.com/lauranjansen)
|
7
|
-
* [#69](https://github.com/radar/dotiw/pull/69): Add Indonesian language support - [@avidmaulanas](https://github.com/avidmaulanas)
|
8
|
-
* [#70](https://github.com/radar/dotiw/pull/70): Add French language support - [@geo1004](https://github.com/geo1004)
|
9
|
-
* [#72](https://github.com/radar/dotiw/pull/72): Add Danish language support - [@kaspernj](https://github.com/kaspernj)
|
10
|
-
* [#71](https://github.com/radar/dotiw/pull/71): Update bundler and ruby versions for Travis CI - [@lauranjansen](https://github.com/lauranjansen)
|
30
|
+
* [#68](https://github.com/radar/dotiw/pull/68): Add support for weeks - [@lauranjansen](https://github.com/lauranjansen).
|
31
|
+
* [#69](https://github.com/radar/dotiw/pull/69): Add Indonesian language support - [@avidmaulanas](https://github.com/avidmaulanas).
|
32
|
+
* [#70](https://github.com/radar/dotiw/pull/70): Add French language support - [@geo1004](https://github.com/geo1004).
|
33
|
+
* [#72](https://github.com/radar/dotiw/pull/72): Add Danish language support - [@kaspernj](https://github.com/kaspernj).
|
34
|
+
* [#71](https://github.com/radar/dotiw/pull/71): Update bundler and ruby versions for Travis CI - [@lauranjansen](https://github.com/lauranjansen).
|
35
|
+
|
36
|
+
## 3.0.1 (2015/04/09)
|
37
|
+
|
38
|
+
## 3.0 (2015/04/09)
|
39
|
+
|
40
|
+
## 0.2.4 (2009/10/18)
|
41
|
+
|
42
|
+
* Initial public release - [@radar](https://github.com/radar).
|
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
data/README.markdown
CHANGED
@@ -1,65 +1,99 @@
|
|
1
|
-
# dotiw
|
1
|
+
# dotiw ![Ruby](https://github.com/radar/distance_of_time_in_words/workflows/Ruby/badge.svg)
|
2
2
|
|
3
|
-
dotiw
|
3
|
+
The `dotiw` library that adds `distance_of_time_in_words` to any Ruby project, or overrides the default implementation in Rails with more accurate output.
|
4
4
|
|
5
|
-
|
6
|
-
=> "1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds"
|
5
|
+
Do you crave accuracy down to the second? So do I. That's why I made this gem.
|
7
6
|
|
8
|
-
|
7
|
+
## Install
|
9
8
|
|
10
|
-
|
11
|
-
=> "1 year, 2 months, 4 days, 6 minutes, and 7 seconds"
|
9
|
+
Add to your `Gemfile`.
|
12
10
|
|
13
|
-
|
11
|
+
```ruby
|
12
|
+
gem 'dotiw'
|
13
|
+
```
|
14
14
|
|
15
|
-
|
16
|
-
"No!" I triumphantly reply:
|
15
|
+
Run `bundle install`.
|
17
16
|
|
18
|
-
|
19
|
-
=> "1 second"
|
17
|
+
### Pure Ruby
|
20
18
|
|
21
|
-
|
19
|
+
```ruby
|
20
|
+
require 'dotiw'
|
22
21
|
|
23
|
-
|
24
|
-
|
22
|
+
include DOTIW::Methods
|
23
|
+
```
|
25
24
|
|
26
|
-
|
27
|
-
because that's just an insanely radical thing to do. \m/
|
25
|
+
### Rails
|
28
26
|
|
29
|
-
|
27
|
+
```ruby
|
28
|
+
require 'dotiw'
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
include ActionView::Helpers::DateHelper
|
31
|
+
include ActionView::Helpers::TextHelper
|
32
|
+
include ActionView::Helpers::NumberHelper
|
33
|
+
```
|
33
34
|
|
34
|
-
|
35
|
-
=> "3 days, and 1 second"
|
35
|
+
## distance\_of\_time\_in\_words
|
36
36
|
|
37
|
-
|
37
|
+
Take this for a totally kick-ass example:
|
38
38
|
|
39
|
-
|
39
|
+
```ruby
|
40
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, true)
|
41
|
+
=> "1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds"
|
42
|
+
```
|
40
43
|
|
41
|
-
|
44
|
+
Also if one of the measurement is zero it will not output it:
|
42
45
|
|
43
46
|
```ruby
|
44
|
-
|
47
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 2.months + 5.hours + 6.minutes + 7.seconds, true)
|
48
|
+
=> "1 year, 2 months, 4 days, 6 minutes, and 7 seconds"
|
45
49
|
```
|
46
50
|
|
47
|
-
|
51
|
+
Better than "about 1 year", am I right? Of course I am.
|
52
|
+
|
53
|
+
"But Ryan!", you say, "What happens if the time is only in seconds but because of the default the seconds aren't shown? Won't it be blank?"
|
54
|
+
"No!" I triumphantly reply:
|
48
55
|
|
49
56
|
```ruby
|
50
|
-
|
57
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.second, false)
|
58
|
+
=> "1 second"
|
51
59
|
```
|
52
60
|
|
61
|
+
The third argument for this method is whether or not to include seconds. By default this is `false` (because in Rails' `distance_of_time_in_words` it is), you can turn it on though by passing `true` as the third argument:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 1.second, true)
|
65
|
+
=> "1 year, and 1 second"
|
66
|
+
```
|
67
|
+
|
68
|
+
Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility", because that's just an insanely radical thing to do. \m/
|
69
|
+
|
70
|
+
The last argument is an optional options hash that can be used to manipulate behavior and (which uses `to_sentence`).
|
71
|
+
|
72
|
+
Don't like having to pass in `Time.now` all the time? Then use `time_ago_in_words` or `distance_of_time_in_words_to_now` which also will *rock your
|
73
|
+
world*:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
>> time_ago_in_words(Time.now + 3.days + 1.second)
|
77
|
+
=> "3 days, and 1 second"
|
78
|
+
|
79
|
+
>> distance_of_time_in_words_to_now(Time.now + 3.days + 1.second)
|
80
|
+
=> "3 days, and 1 second"
|
81
|
+
```
|
82
|
+
|
83
|
+
Oh, and did I mention it supports I18n? Oh yeah. Rock on!
|
84
|
+
|
53
85
|
### Options
|
54
86
|
|
55
87
|
#### :locale
|
56
88
|
|
57
89
|
You can pass in a locale and it'll output it in whatever language you want (provided you have translations, otherwise it'll default to your app's default locale (the `config.i18n.default_locale` you have set in `/config/application.rb`):
|
58
90
|
|
59
|
-
|
60
|
-
|
91
|
+
```ruby
|
92
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.minute, false, locale: :es)
|
93
|
+
=> "1 minuto"
|
94
|
+
```
|
61
95
|
|
62
|
-
This will also be passed to `to_sentence
|
96
|
+
This will also be passed to `to_sentence`.
|
63
97
|
|
64
98
|
#### :vague
|
65
99
|
|
@@ -67,46 +101,60 @@ Specify this if you want it to use the old `distance_of_time_in_words`. The valu
|
|
67
101
|
|
68
102
|
#### :accumulate_on
|
69
103
|
|
70
|
-
Specifies the maximum output unit which will accumulate all the surplus. Say you set it to seconds and your time difference is of 2 minutes then the output would be 120 seconds.
|
104
|
+
Specifies the maximum output unit which will accumulate all the surplus. Say you set it to seconds and your time difference is of 2 minutes then the output would be 120 seconds.
|
71
105
|
|
72
|
-
|
73
|
-
|
106
|
+
```ruby
|
107
|
+
>> distance_of_time_in_words(Time.now, Time.now + 2.hours + 70.seconds, true, accumulate_on: :minutes)
|
108
|
+
=> "121 minutes and 10 seconds"
|
109
|
+
```
|
74
110
|
|
75
111
|
#### :only
|
76
112
|
|
77
113
|
Only want a specific measurement of time? No problem!
|
78
114
|
|
79
|
-
|
80
|
-
|
115
|
+
```ruby
|
116
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, false, only: :minutes)
|
117
|
+
=> "1 minute"
|
118
|
+
```
|
81
119
|
|
82
120
|
You only want some? No problem too!
|
83
121
|
|
84
|
-
|
85
|
-
|
122
|
+
```ruby
|
123
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.day + 1.minute, false, only: [:minutes, :hours])
|
124
|
+
=> "1 hour and 1 minute"
|
125
|
+
```
|
86
126
|
|
87
127
|
#### :except
|
88
128
|
|
89
129
|
Don't want a measurement of time? No problem!
|
90
130
|
|
91
|
-
|
92
|
-
|
131
|
+
```ruby
|
132
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, false, except: :minutes)
|
133
|
+
=> "1 hour"
|
134
|
+
```
|
93
135
|
|
94
136
|
Culling a whole group of measurements of time:
|
95
137
|
|
96
|
-
|
97
|
-
|
138
|
+
```ruby
|
139
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.day + 1.minute, false, except: [:minutes, :hours])
|
140
|
+
=> "1 day"
|
141
|
+
```
|
98
142
|
|
99
143
|
#### :highest\_measure\_only
|
100
144
|
|
101
145
|
For times when Rails `distance_of_time_in_words` is not precise enough and `DOTIW` is too precise. For instance, if you only want to know the highest time part (measure) that elapsed between two dates.
|
102
146
|
|
103
|
-
|
104
|
-
|
147
|
+
```ruby
|
148
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, highest_measure_only: true)
|
149
|
+
=> "1 hour"
|
150
|
+
```
|
105
151
|
|
106
152
|
Notice how minutes and seconds were removed from the output. Another example:
|
107
153
|
|
108
|
-
|
109
|
-
|
154
|
+
```ruby
|
155
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.minute + 1.second, true, highest_measure_only: true)
|
156
|
+
=> "1 minute"
|
157
|
+
```
|
110
158
|
|
111
159
|
Minutes are the highest measure, so seconds were discarded from the output.
|
112
160
|
|
@@ -114,58 +162,97 @@ Minutes are the highest measure, so seconds were discarded from the output.
|
|
114
162
|
|
115
163
|
When you want variable precision from `DOTIW`:
|
116
164
|
|
117
|
-
|
118
|
-
|
165
|
+
```ruby
|
166
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, highest_measures: 2)
|
167
|
+
=> "1 hour and 1 minute"
|
168
|
+
```
|
119
169
|
|
120
170
|
#### :words_connector
|
121
171
|
|
122
|
-
|
172
|
+
This is an option for `to_sentence`, defaults to ', '.
|
123
173
|
|
124
174
|
Using something other than a comma:
|
125
175
|
|
126
|
-
|
127
|
-
|
176
|
+
```ruby
|
177
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, words_connector: ' - ')
|
178
|
+
=> "1 hour - 1 minute, and 1 second"
|
179
|
+
```
|
128
180
|
|
129
181
|
#### :two\_words\_connector
|
130
182
|
|
131
|
-
|
183
|
+
This is an option for `to_sentence`, defaults to ' and '.
|
132
184
|
|
133
185
|
Using something other than 'and':
|
134
186
|
|
135
|
-
|
136
|
-
|
187
|
+
```ruby
|
188
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, true, two_words_connector: ' plus ')
|
189
|
+
=> "1 hour plus 1 minute"
|
190
|
+
```
|
137
191
|
|
138
192
|
#### :last\_word\_connector
|
139
193
|
|
140
|
-
|
194
|
+
This is an option for `to_sentence`, defaults to ', and '.
|
141
195
|
|
142
196
|
Using something other than ', and':
|
143
197
|
|
144
|
-
|
145
|
-
|
198
|
+
```ruby
|
199
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, last_word_connector: ', finally ')
|
200
|
+
=> "1 hour, 1 minute, finally 1 second"
|
201
|
+
```
|
146
202
|
|
147
203
|
## distance\_of\_time
|
148
204
|
|
149
205
|
If you have simply a number of seconds you can get the "stringified" version of this by using `distance_of_time`:
|
150
206
|
|
151
|
-
|
152
|
-
|
207
|
+
```ruby
|
208
|
+
>> distance_of_time(300)
|
209
|
+
=> "5 minutes"
|
210
|
+
```
|
153
211
|
|
154
212
|
## distance\_of\_time\_in\_words\_hash
|
155
213
|
|
156
214
|
Don't like any format you're given? That's cool too! Here, have an indifferent hash version:
|
157
215
|
|
158
|
-
|
159
|
-
|
216
|
+
```ruby
|
217
|
+
>> distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds)
|
218
|
+
=> { days: 4, weeks: 3, seconds: 7, minutes: 6, years: 1, hours: 5, months: 2 }
|
219
|
+
```
|
160
220
|
|
161
221
|
Indifferent means that you can access all keys by their `String` or `Symbol` version.
|
162
222
|
|
163
223
|
## distance\_of\_time\_in\_percent
|
164
224
|
|
165
|
-
|
225
|
+
This method is only available with Rails ActionView.
|
166
226
|
|
167
|
-
|
227
|
+
If you want to calculate a distance of time in percent, use `distance_of_time_in_percent`. The first argument is the beginning time, the second argument the "current" time and the third argument is the end time.
|
168
228
|
|
229
|
+
```ruby
|
230
|
+
>> distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time)
|
231
|
+
=> '15%'
|
232
|
+
```
|
233
|
+
|
234
|
+
This method takes the same options as [`number_with_precision`](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision).
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
>> distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time, precision: 1)
|
238
|
+
=> '15.3%'
|
239
|
+
```
|
240
|
+
|
241
|
+
## :compact
|
242
|
+
|
243
|
+
Pressed for space? Try `compact: true`.
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
>> distance_of_time_in_words(Time.now, Time.now + 2.year + 1.day + 1.second, compact: true)
|
247
|
+
=> "2y1d"
|
248
|
+
```
|
249
|
+
|
250
|
+
Pairs well with `words_connector`, `last_word_connector`, and `two_words_connector` if you can spare just a little more room:
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
>> distance_of_time_in_words(Time.now, Time.now + 5.years + 1.day + 23.seconds, words_connector: " ", last_word_connector: " ", two_words_connector: " ", compact: true)
|
254
|
+
=> "5y 1d 23s"
|
255
|
+
```
|
169
256
|
|
170
257
|
## Contributors
|
171
258
|
|
@@ -173,3 +260,4 @@ If you want to calculate a distance of time in percent, use `distance_of_time_in
|
|
173
260
|
* [Derander](http://github.com/derander) - correct Spanish translations
|
174
261
|
* [DBA](http://github.com/dba) - commits leading up to the 0.7 release
|
175
262
|
* [Sija](http://github.com/Sija) - rails 4 support, v2.0 release
|
263
|
+
* [dblock](http://github.com/dblock) - Ruby w/o Rails support
|