humane 0.4.0 → 0.5.0

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
  SHA256:
3
- metadata.gz: 572a6a4a29165d48eec68db31af6cb222a88de6f989099d353811225898c4673
4
- data.tar.gz: cfc60adb65c39694c156f7b173d053431d469ec26d1bf98a584c83b05bfc5887
3
+ metadata.gz: 94b64259f8b2dc0d78f3f6feb6b48b4d908f23bfa74f133d901c57f5232294bd
4
+ data.tar.gz: 65e61cee90061fb0eba0e08053c2d24104d7e7d8bc711b8789661a5b27dd9f52
5
5
  SHA512:
6
- metadata.gz: baa6946270a2a9bcaa4636993324df9cae89da3ff2a753552bc21bcdc5569a9fe10075dc48e4e2e910a2f50d811ff324bf7e7ce59168c03025cc2f2157626d5e
7
- data.tar.gz: 4dc6ee4899fe1ae32d33c36307a5a01bef49cf34fb368668b28bd584cae6bce38d9a00be13d9aef532020015d40f00375c1c9ad8299380882bdcc29935469f78
6
+ metadata.gz: a3320656c10207ff6e941325d26085bc16acb4a56807cf4daafd2bb7560396cd36a37afcc77b270114b330d83ae9e0a7b8b394444958b0a7f96207a775c9492a
7
+ data.tar.gz: 84d09dac947371900807718502d17055a914b91e58833a10fc2868c0179794fbf8ba80a42d14b359aef4fa4c32c2dbdcaa9265d737195f90c7978bc381a2dce2
data/README.md CHANGED
@@ -33,6 +33,11 @@ let formatter = RelativeDateTimeFormatter(); formatter.unitsStyle = .full
33
33
  formatter.localizedString(for: time, relativeTo: now) // "3 minutes ago"
34
34
  ```
35
35
 
36
+ If you're writing Swift directly rather than calling Foundation by hand,
37
+ [`humane-swift`](https://github.com/woodie/humane-swift) wraps these same two
38
+ formatters with the identical API shape -- including the
39
+ `includeSeconds`/`approximate` options below.
40
+
36
41
  ## Install
37
42
 
38
43
  ```
@@ -50,13 +55,15 @@ gem "humane"
50
55
  Two options on `Humane::TimeFormatter`, both off by default so it matches
51
56
  `RelativeDateTimeFormatter` exactly out of the box:
52
57
 
53
- - `include_seconds` (default `false`): below a minute, collapses to "less than a
58
+ - `include_seconds` (default `false`): under 30 seconds, collapses to "less than a
54
59
  minute ago"/"in less than a minute" instead of an exact second count. Named after
55
60
  ActionView's `include_seconds`, which defaults the same way.
56
- - `approximate` (default `false`): prefixes "about"/"in about" on buckets of an hour
57
- or larger, the way ActionView's `distance_of_time_in_words` does past that same
58
- boundary -- for a render that can't refresh itself and shouldn't overstate its own
59
- precision.
61
+ - `approximate` (default `false`): prefixes "about"/"in about" on the hour-scale
62
+ buckets (1 hour, and 2..24 hours), the way ActionView's `distance_of_time_in_words`
63
+ does for those same buckets -- for a render that can't refresh itself and shouldn't
64
+ overstate its own precision. Matches ActionView's own table exactly (down to its
65
+ 44:30/89:30 rounding cutoffs), through the "1 day" bucket; week/month/year buckets
66
+ are out of scope. See [issue #1](https://github.com/woodie/humane-ruby/issues/1).
60
67
 
61
68
  ```ruby
62
69
  Humane::TimeFormatter.new(approximate: true).string(at: t - 15 * 3600, relative_to: t)
@@ -4,7 +4,7 @@ module Humane
4
4
  # Formats one time relative to another the way Finder-adjacent tools do.
5
5
  class TimeFormatter
6
6
  # include_seconds shows exact seconds under a minute instead of collapsing to "less than a minute ago/in less than a minute". Defaults to false, matching ActionView's include_seconds.
7
- # approximate prefixes "about"/"in about" on buckets of an hour or more, matching ActionView's distance_of_time_in_words past that same boundary. Defaults to false.
7
+ # approximate prefixes "about"/"in about" on the hour-scale buckets (1 hour, and 2..24 hours), matching ActionView's distance_of_time_in_words wording for those buckets. Defaults to false. See docs/COMMENTS.md and humane-ruby issue #1 for the full bucket table this ports.
8
8
  def initialize(include_seconds: false, approximate: false)
9
9
  @include_seconds = include_seconds
10
10
  @approximate = approximate
@@ -16,28 +16,37 @@ module Humane
16
16
  future = seconds.negative?
17
17
  seconds = seconds.abs
18
18
 
19
- if !@include_seconds && seconds < 60
19
+ if !@include_seconds && seconds < 30
20
20
  return future ? "in less than a minute" : "less than a minute ago"
21
21
  end
22
22
 
23
- text =
24
- if seconds < 60
25
- pluralize(seconds.to_i, "second")
26
- elsif seconds < 3600
27
- pluralize((seconds / 60.0).round, "minute")
28
- elsif seconds < 86_400
29
- pluralize((seconds / 3600.0).round, "hour")
30
- else
31
- pluralize((seconds / 86_400.0).round, "day")
32
- end
23
+ if @include_seconds && seconds < 60
24
+ return wrap(pluralize(seconds.to_i, "second"), future: future)
25
+ end
33
26
 
34
- text = "about #{text}" if @approximate && seconds >= 3600
27
+ # Buckets come from distance_in_minutes, not raw seconds re-divided per unit -- see docs/COMMENTS.md.
28
+ distance_in_minutes = (seconds / 60.0).round
29
+
30
+ text, approximable =
31
+ case distance_in_minutes
32
+ when 1 then ["1 minute", false]
33
+ when 2..44 then [pluralize(distance_in_minutes, "minute"), false]
34
+ when 45..89 then ["1 hour", true]
35
+ when 90..1439 then [pluralize((distance_in_minutes / 60.0).round, "hour"), true]
36
+ when 1440..2519 then ["1 day", false]
37
+ else [pluralize((distance_in_minutes / 1440.0).round, "day"), false]
38
+ end
35
39
 
36
- future ? "in #{text}" : "#{text} ago"
40
+ text = "about #{text}" if @approximate && approximable
41
+ wrap(text, future: future)
37
42
  end
38
43
 
39
44
  private
40
45
 
46
+ def wrap(text, future:)
47
+ future ? "in #{text}" : "#{text} ago"
48
+ end
49
+
41
50
  def pluralize(count, unit)
42
51
  count == 1 ? "1 #{unit}" : "#{count} #{unit}s"
43
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Humane
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woodell