rails-diff-time 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +12 -0
- data/app/views/rails-diff-time/_diff_time.html.erb +5 -0
- data/lib/rails/diff/time/helpers.rb +179 -0
- data/lib/rails/diff/time/version.rb +5 -0
- data/lib/rails/diff/time.rb +25 -0
- data/sig/rails/diff/time.rbs +38 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: be36e9c0f9c1bc6a0566f4a346b4106196028880c654497dab69d32730c17091
|
|
4
|
+
data.tar.gz: 65097dbff040d4f111dacbc1c823324af51f3bcfefaeb9f01fd16a2c34b2e2f2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 88326fa6a91bd65e87e8998e88cd7d2d581f96d75c237221089f0c8153f8e12a7a6b1839400ce4a66780a83abd815d351e3765574fb14d89fffa170e3f3e265f
|
|
7
|
+
data.tar.gz: f98c3a29233b51ec321c1d473f128d0fd918a0570c7acb34c7681c583154f65ca9ac67207bc22d244ed8493de4ddd4aeda26da7502439c9d593ef948935ecdb5
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.4.5
|
|
3
|
+
NewCops: enable
|
|
4
|
+
|
|
5
|
+
Style/StringLiterals:
|
|
6
|
+
EnforcedStyle: double_quotes
|
|
7
|
+
|
|
8
|
+
Style/StringLiteralsInInterpolation:
|
|
9
|
+
EnforcedStyle: double_quotes
|
|
10
|
+
|
|
11
|
+
# メトリクス系のルールをオプトアウト
|
|
12
|
+
Metrics/ModuleLength:
|
|
13
|
+
Enabled: false
|
|
14
|
+
|
|
15
|
+
Metrics/AbcSize:
|
|
16
|
+
Enabled: false
|
|
17
|
+
|
|
18
|
+
Metrics/CyclomaticComplexity:
|
|
19
|
+
Enabled: false
|
|
20
|
+
|
|
21
|
+
Metrics/MethodLength:
|
|
22
|
+
Enabled: false
|
|
23
|
+
|
|
24
|
+
Metrics/BlockLength:
|
|
25
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 dhq_boiler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Rails::Diff::Time
|
|
2
|
+
|
|
3
|
+
A Rails helper gem to display time differences in a human-readable format.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'rails-diff-time'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install rails-diff-time
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
In your Rails views, you can use the `diff_time` helper to display time differences:
|
|
24
|
+
|
|
25
|
+
```erb
|
|
26
|
+
<%= diff_time(Time.now + 3.days) %>
|
|
27
|
+
# Output: <span>3 days later</span>
|
|
28
|
+
|
|
29
|
+
<%= diff_time(Time.now - 2.hours) %>
|
|
30
|
+
# Output: <span>2 hours ago</span>
|
|
31
|
+
|
|
32
|
+
<%= diff_time(Time.now + 1.year + 2.months, "div", class: "time-diff") %>
|
|
33
|
+
# Output: <div class="time-diff">1 year 2 months later</div>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Parameters
|
|
37
|
+
|
|
38
|
+
- `certain_time` (required): The time to compare with the current time
|
|
39
|
+
- `element_name` (optional, default: "span"): The HTML element to wrap the output
|
|
40
|
+
- `attributes` (optional, default: {}): HTML attributes to add to the element
|
|
41
|
+
|
|
42
|
+
## Examples
|
|
43
|
+
|
|
44
|
+
```erb
|
|
45
|
+
# Simple usage
|
|
46
|
+
<%= diff_time(user.created_at) %>
|
|
47
|
+
|
|
48
|
+
# Custom element
|
|
49
|
+
<%= diff_time(event.starts_at, "p") %>
|
|
50
|
+
|
|
51
|
+
# With CSS classes
|
|
52
|
+
<%= diff_time(post.published_at, "span", class: "text-muted", id: "post-time") %>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Development
|
|
56
|
+
|
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
58
|
+
|
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
60
|
+
|
|
61
|
+
## Contributing
|
|
62
|
+
|
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dhq_boiler/rails-diff-time.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
68
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsDiffTime
|
|
4
|
+
# Helper methods for displaying time differences in a human-readable format.
|
|
5
|
+
#
|
|
6
|
+
# This module provides methods to format time differences as relative strings
|
|
7
|
+
# (e.g., "2 hours ago", "3 days later") and integrates with Rails views.
|
|
8
|
+
# The helpers can be used directly in templates or through the provided
|
|
9
|
+
# render helper method.
|
|
10
|
+
#
|
|
11
|
+
# @example Usage in a Rails view
|
|
12
|
+
# <%= diff_time(user.created_at) %>
|
|
13
|
+
# <%= diff_time(event.start_time, "div", class: "timestamp") %>
|
|
14
|
+
module Helpers
|
|
15
|
+
def diff_time(certain_time, element_name = "span", attributes = {})
|
|
16
|
+
render "rails-diff-time/diff_time", locals: {
|
|
17
|
+
diff_time: diff_time_str(certain_time),
|
|
18
|
+
element_name: element_name,
|
|
19
|
+
attributes: attributes
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def diff_time_str(certain_time)
|
|
26
|
+
now = ::Time.now
|
|
27
|
+
diff = certain_time - now
|
|
28
|
+
difference_in_seconds = diff.abs
|
|
29
|
+
|
|
30
|
+
# Display "now" if within 5 seconds
|
|
31
|
+
return "now" if difference_in_seconds <= 5
|
|
32
|
+
|
|
33
|
+
case difference_in_seconds
|
|
34
|
+
when year_threshold..Float::INFINITY
|
|
35
|
+
format_years difference_in_seconds, diff
|
|
36
|
+
when month_threshold...year_threshold
|
|
37
|
+
format_months difference_in_seconds, diff
|
|
38
|
+
when week_threshold...month_threshold
|
|
39
|
+
format_weeks difference_in_seconds, diff
|
|
40
|
+
when day_threshold...week_threshold
|
|
41
|
+
format_days difference_in_seconds, diff
|
|
42
|
+
when hour_threshold...day_threshold
|
|
43
|
+
format_hours difference_in_seconds, diff
|
|
44
|
+
when minute_threshold...hour_threshold
|
|
45
|
+
format_minutes difference_in_seconds, diff
|
|
46
|
+
else
|
|
47
|
+
format_seconds difference_in_seconds, diff
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def format_years(difference_in_seconds, diff)
|
|
52
|
+
years = (difference_in_seconds / year_in_seconds).floor
|
|
53
|
+
remaining_seconds = difference_in_seconds - (years * year_in_seconds)
|
|
54
|
+
|
|
55
|
+
if remaining_seconds >= month_in_seconds
|
|
56
|
+
months = (remaining_seconds / month_in_seconds).floor
|
|
57
|
+
remaining_seconds -= months * month_in_seconds
|
|
58
|
+
|
|
59
|
+
if remaining_seconds >= day_in_seconds
|
|
60
|
+
days = (remaining_seconds / day_in_seconds).floor
|
|
61
|
+
"#{time_str(years, "year")} #{time_str(months, "month")} #{time_str(days, "day")} #{ago_or_later(diff)}"
|
|
62
|
+
else
|
|
63
|
+
"#{time_str(years, "year")} #{time_str(months, "month")} #{ago_or_later(diff)}"
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
"#{time_str(years, "year")} #{ago_or_later(diff)}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def format_months(difference_in_seconds, diff)
|
|
71
|
+
months = (difference_in_seconds / month_in_seconds).floor
|
|
72
|
+
remaining_seconds = difference_in_seconds - (months * month_in_seconds)
|
|
73
|
+
|
|
74
|
+
if remaining_seconds >= day_in_seconds
|
|
75
|
+
days = (remaining_seconds / day_in_seconds).floor
|
|
76
|
+
"#{time_str(months, "month")} #{time_str(days, "day")} #{ago_or_later(diff)}"
|
|
77
|
+
else
|
|
78
|
+
"#{time_str(months, "month")} #{ago_or_later(diff)}"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def format_weeks(difference_in_seconds, diff)
|
|
83
|
+
weeks = (difference_in_seconds / week_in_seconds).floor
|
|
84
|
+
"#{time_str(weeks, "week")} #{ago_or_later(diff)}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def format_days(difference_in_seconds, diff)
|
|
88
|
+
days = (difference_in_seconds / day_in_seconds).floor
|
|
89
|
+
"#{time_str(days, "day")} #{ago_or_later(diff)}"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def format_hours(difference_in_seconds, diff)
|
|
93
|
+
hours = (difference_in_seconds / hour_in_seconds).floor
|
|
94
|
+
remaining_seconds = difference_in_seconds - (hours * hour_in_seconds)
|
|
95
|
+
|
|
96
|
+
if remaining_seconds >= minute_in_seconds
|
|
97
|
+
minutes = (remaining_seconds / minute_in_seconds).floor
|
|
98
|
+
"#{time_str(hours, "hour")} #{time_str(minutes, "minute")} #{ago_or_later(diff)}"
|
|
99
|
+
else
|
|
100
|
+
"#{time_str(hours, "hour")} #{ago_or_later(diff)}"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def format_minutes(difference_in_seconds, diff)
|
|
105
|
+
minutes = (difference_in_seconds / minute_in_seconds).floor
|
|
106
|
+
remaining_seconds = difference_in_seconds - (minutes * minute_in_seconds)
|
|
107
|
+
|
|
108
|
+
if remaining_seconds.positive?
|
|
109
|
+
seconds = remaining_seconds.floor
|
|
110
|
+
"#{time_str(minutes, "minute")} #{time_str(seconds, "second")} #{ago_or_later(diff)}"
|
|
111
|
+
else
|
|
112
|
+
"#{time_str(minutes, "minute")} #{ago_or_later(diff)}"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def format_seconds(difference_in_seconds, diff)
|
|
117
|
+
seconds = difference_in_seconds.floor
|
|
118
|
+
"#{time_str(seconds, "second")} #{ago_or_later(diff)}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Time unit constant methods
|
|
122
|
+
def year_in_seconds
|
|
123
|
+
365.25 * 24 * 3600 # Considering leap years
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def month_in_seconds
|
|
127
|
+
30 * 24 * 3600 # Average 30 days
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def week_in_seconds
|
|
131
|
+
7 * 24 * 3600
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def day_in_seconds
|
|
135
|
+
24 * 3600
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def hour_in_seconds
|
|
139
|
+
3600
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def minute_in_seconds
|
|
143
|
+
60
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Threshold methods
|
|
147
|
+
def year_threshold
|
|
148
|
+
year_in_seconds
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def month_threshold
|
|
152
|
+
month_in_seconds
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def week_threshold
|
|
156
|
+
week_in_seconds
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def day_threshold
|
|
160
|
+
day_in_seconds
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def hour_threshold
|
|
164
|
+
hour_in_seconds
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def minute_threshold
|
|
168
|
+
minute_in_seconds
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def ago_or_later(diff_seconds)
|
|
172
|
+
diff_seconds.negative? ? "ago" : "later"
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def time_str(count, singular)
|
|
176
|
+
"#{count} #{count == 1 ? singular : singular.pluralize}"
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "time/version"
|
|
4
|
+
require_relative "time/helpers"
|
|
5
|
+
|
|
6
|
+
module RailsDiffTime
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# Define Engine class only when Rails::Engine is available
|
|
10
|
+
if defined?(::Rails::Engine)
|
|
11
|
+
# Rails engine for integrating RailsDiffTime into Rails applications.
|
|
12
|
+
#
|
|
13
|
+
# This engine automatically includes the time difference helpers into
|
|
14
|
+
# ActionView, making them available in all views and templates.
|
|
15
|
+
class Engine < ::Rails::Engine
|
|
16
|
+
isolate_namespace RailsDiffTime
|
|
17
|
+
|
|
18
|
+
initializer "rails_diff_time.helpers" do
|
|
19
|
+
ActiveSupport.on_load(:action_view) do
|
|
20
|
+
include RailsDiffTime::Helpers
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module RailsDiffTime
|
|
2
|
+
VERSION: String
|
|
3
|
+
|
|
4
|
+
module Helpers
|
|
5
|
+
def diff_time: (Time certain_time, ?String element_name, ?Hash[Symbol, untyped] attributes) -> String
|
|
6
|
+
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def diff_time_str: (Time certain_time) -> String
|
|
10
|
+
def format_years: (Float difference_in_seconds, Float diff) -> String
|
|
11
|
+
def format_months: (Float difference_in_seconds, Float diff) -> String
|
|
12
|
+
def format_weeks: (Float difference_in_seconds, Float diff) -> String
|
|
13
|
+
def format_days: (Float difference_in_seconds, Float diff) -> String
|
|
14
|
+
def format_hours: (Float difference_in_seconds, Float diff) -> String
|
|
15
|
+
def format_minutes: (Float difference_in_seconds, Float diff) -> String
|
|
16
|
+
def format_seconds: (Float difference_in_seconds, Float diff) -> String
|
|
17
|
+
def year_in_seconds: () -> Float
|
|
18
|
+
def month_in_seconds: () -> Float
|
|
19
|
+
def week_in_seconds: () -> Float
|
|
20
|
+
def day_in_seconds: () -> Float
|
|
21
|
+
def hour_in_seconds: () -> Float
|
|
22
|
+
def minute_in_seconds: () -> Float
|
|
23
|
+
def year_threshold: () -> Float
|
|
24
|
+
def month_threshold: () -> Float
|
|
25
|
+
def week_threshold: () -> Float
|
|
26
|
+
def day_threshold: () -> Float
|
|
27
|
+
def hour_threshold: () -> Float
|
|
28
|
+
def minute_threshold: () -> Float
|
|
29
|
+
def ago_or_later: (Float diff_seconds) -> String
|
|
30
|
+
def time_str: (Integer count, String singular) -> String
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Error < StandardError
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class Engine < Rails::Engine
|
|
37
|
+
end
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails-diff-time
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- dhq_boiler
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: A Rails gem that provides helper methods to display time differences
|
|
13
|
+
in a human-readable format (e.g., '2 hours ago', '3 days later'). Integrates seamlessly
|
|
14
|
+
with Rails views and supports various time units from seconds to years.
|
|
15
|
+
email:
|
|
16
|
+
- dhq_boiler@live.jp
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".rspec"
|
|
22
|
+
- ".rubocop.yml"
|
|
23
|
+
- CHANGELOG.md
|
|
24
|
+
- LICENSE.txt
|
|
25
|
+
- README.md
|
|
26
|
+
- Rakefile
|
|
27
|
+
- app/views/rails-diff-time/_diff_time.html.erb
|
|
28
|
+
- lib/rails/diff/time.rb
|
|
29
|
+
- lib/rails/diff/time/helpers.rb
|
|
30
|
+
- lib/rails/diff/time/version.rb
|
|
31
|
+
- sig/rails/diff/time.rbs
|
|
32
|
+
homepage: https://github.com/dhq-boiler/rails-diff-time
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
homepage_uri: https://github.com/dhq-boiler/rails-diff-time
|
|
37
|
+
source_code_uri: https://github.com/dhq-boiler/rails-diff-time
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.4.5
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 3.6.9
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Rails helper for displaying human-readable time differences
|
|
56
|
+
test_files: []
|