local_time 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 +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +24 -0
- data/app/assets/javascripts/local_time.js.coffee +6 -4
- data/app/helpers/local_time_helper.rb +19 -2
- data/test/helpers/local_time_helper_test.rb +43 -1
- data/test/javascripts/index.js.coffee +1 -0
- data/test/javascripts/unit/public_api_test.js.coffee +21 -0
- data/test/javascripts/unit/time_ago_test.js.coffee +5 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84f0f9773b1e9bfac34c2e579af56d6c784a8d23
|
4
|
+
data.tar.gz: a1606c79f356e6e09b67f5b2adca382e38366a9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88db2bd3c1a77268ef0e8a59a18c81bed59c9f3b837bd3f9d2e555862228ded216dc7a2cf8e2d19a1c06fc262ada3ca319e4b2538eeae3ea826c3ecbe65c113f
|
7
|
+
data.tar.gz: 71e7a980c10839bc5e81dda212a9a99f1dd9a5ec68cbd3a082e754126de0dfccad334e3efc28bb01a1c416b71c31bc7bdece4a9c848a2de60e7d771513de9121
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -37,6 +37,13 @@ Alias for local_time with a month-formatted default
|
|
37
37
|
<%= local_date(time, format: '%B %e, %Y') %>
|
38
38
|
```
|
39
39
|
|
40
|
+
To use a strftime format already defined in your app, pass a symbol as the format.
|
41
|
+
```erb
|
42
|
+
<%= local_time(date, format: :long) %>
|
43
|
+
```
|
44
|
+
|
45
|
+
`I18n.t("time.formats.#{format}")`, `I18n.t("date.formats.#{format}")`, `Time::DATE_FORMATS[format]`, and `Date::DATE_FORMATS[format]` will be scanned (in that order) for your format.
|
46
|
+
|
40
47
|
Note: The included strftime JavaScript implementation is not 100% complete. It supports the following directives: `%a %A %b %B %c %d %e %H %I %l %m %M %p %P %S %w %y %Y`
|
41
48
|
|
42
49
|
#### Time ago helper
|
@@ -63,8 +70,25 @@ Displays the relative amount of time passed. With age, the descriptions transiti
|
|
63
70
|
|
64
71
|
The included JavaScript does not depend on any frameworks or libraries, and listens for a `DOMContentLoaded` event to run initially. It also listens on `document` for `page:update` if you're using Turbolinks and `ajaxSuccess` if you're using jQuery. This should catch most cases where new `<time>` elements have been added to the DOM and process them automatically. If you're adding new elements in another context, trigger `time:elapse` to process them.
|
65
72
|
|
73
|
+
#### JavaScript API
|
74
|
+
|
75
|
+
`strftime` and `relativeTimeAgo` are available via the global `LocalTime` object.
|
76
|
+
|
77
|
+
```js
|
78
|
+
> LocalTime.strftime(new Date, "%B %e, %Y %l:%M%P")
|
79
|
+
"February 9, 2014 12:55pm"
|
80
|
+
|
81
|
+
> LocalTime.relativeTimeAgo(new Date(new Date - 60 * 1000 * 5))
|
82
|
+
"5 minutes ago"
|
83
|
+
```
|
84
|
+
|
66
85
|
#### Version History
|
67
86
|
|
87
|
+
**0.3.0** (February 9, 2014)
|
88
|
+
|
89
|
+
* Allow :format option lookup in I18n or DATE_FORMATS hashes [Paul Dobbins]
|
90
|
+
* Expose public API to JavaScript helpers
|
91
|
+
|
68
92
|
**0.2.0** (December 10, 2013)
|
69
93
|
|
70
94
|
* Prefer `textContent` over `innerText` for Firefox compatibility
|
@@ -83,9 +83,6 @@ class CalendarDate
|
|
83
83
|
|
84
84
|
|
85
85
|
class RelativeTimeAgo
|
86
|
-
@generate: (date) ->
|
87
|
-
new this(date).toString()
|
88
|
-
|
89
86
|
constructor: (@date) ->
|
90
87
|
@calendarDate = CalendarDate.fromDate @date
|
91
88
|
|
@@ -146,6 +143,8 @@ class RelativeTimeAgo
|
|
146
143
|
formatTime: ->
|
147
144
|
strftime @date, '%l:%M%P'
|
148
145
|
|
146
|
+
relativeTimeAgo = (date) ->
|
147
|
+
new RelativeTimeAgo(date).toString()
|
149
148
|
|
150
149
|
domLoaded = false
|
151
150
|
|
@@ -187,10 +186,13 @@ document.addEventListener "DOMContentLoaded", ->
|
|
187
186
|
element.setAttribute "data-localized", true
|
188
187
|
strftime time, format
|
189
188
|
when "time-ago"
|
190
|
-
|
189
|
+
relativeTimeAgo time
|
191
190
|
|
192
191
|
setInterval ->
|
193
192
|
event = document.createEvent "Events"
|
194
193
|
event.initEvent "time:elapse", true, true
|
195
194
|
document.dispatchEvent event
|
196
195
|
, 60 * 1000
|
196
|
+
|
197
|
+
# Public API
|
198
|
+
@LocalTime = {strftime, relativeTimeAgo}
|
@@ -1,7 +1,9 @@
|
|
1
1
|
module LocalTimeHelper
|
2
|
+
DEFAULT_FORMAT = '%B %e, %Y %l:%M%P'
|
3
|
+
|
2
4
|
def local_time(time, options = {})
|
3
5
|
time = utc_time(time)
|
4
|
-
format = options.delete(:format)
|
6
|
+
format = time_format(options.delete(:format))
|
5
7
|
|
6
8
|
options[:data] ||= {}
|
7
9
|
options[:data].merge! local: :time, format: format
|
@@ -20,7 +22,7 @@ module LocalTimeHelper
|
|
20
22
|
options[:data] ||= {}
|
21
23
|
options[:data].merge! local: 'time-ago'
|
22
24
|
|
23
|
-
time_tag time, time.strftime(
|
25
|
+
time_tag time, time.strftime(DEFAULT_FORMAT), options
|
24
26
|
end
|
25
27
|
|
26
28
|
def utc_time(time_or_date)
|
@@ -30,4 +32,19 @@ module LocalTimeHelper
|
|
30
32
|
time_or_date.to_time.utc
|
31
33
|
end
|
32
34
|
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def time_format(format)
|
38
|
+
if format.is_a?(Symbol)
|
39
|
+
if (i18n_format = I18n.t("time.formats.#{format}", default: [:"date.formats.#{format}", ''])).present?
|
40
|
+
i18n_format
|
41
|
+
elsif (date_format = Time::DATE_FORMATS[format] || Date::DATE_FORMATS[format])
|
42
|
+
date_format.is_a?(Proc) ? DEFAULT_FORMAT : date_format
|
43
|
+
else
|
44
|
+
DEFAULT_FORMAT
|
45
|
+
end
|
46
|
+
else
|
47
|
+
format.presence || DEFAULT_FORMAT
|
48
|
+
end
|
49
|
+
end
|
33
50
|
end
|
@@ -3,6 +3,7 @@ require 'active_support/all'
|
|
3
3
|
require 'action_view'
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
+
I18n.enforce_available_locales = false
|
6
7
|
|
7
8
|
class LocalTimeHelperTest < MiniTest::Unit::TestCase
|
8
9
|
include ActionView::Helpers::DateHelper, ActionView::Helpers::TagHelper
|
@@ -11,6 +12,11 @@ class LocalTimeHelperTest < MiniTest::Unit::TestCase
|
|
11
12
|
def setup
|
12
13
|
@original_zone = Time.zone
|
13
14
|
Time.zone = ActiveSupport::TimeZone["Central Time (US & Canada)"]
|
15
|
+
I18n.backend.store_translations(:en, {
|
16
|
+
time: { formats: { simple_time: "%b %e" } },
|
17
|
+
date: { formats: { simple_date: "%b %e" } } })
|
18
|
+
Time::DATE_FORMATS[:time_formats_simple_time] = '%b %e'
|
19
|
+
Date::DATE_FORMATS[:date_formats_simple_date] = '%b %e'
|
14
20
|
|
15
21
|
@date = "2013-11-21"
|
16
22
|
@time = Time.zone.parse(@date)
|
@@ -45,6 +51,27 @@ class LocalTimeHelperTest < MiniTest::Unit::TestCase
|
|
45
51
|
assert_equal expected, local_time(@time, format: '%b %e')
|
46
52
|
end
|
47
53
|
|
54
|
+
def test_local_time_with_i18n_format
|
55
|
+
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}">Nov 21</time>)
|
56
|
+
assert_equal expected, local_time(@time, format: :simple_time)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_local_time_with_date_formats_format
|
60
|
+
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}">Nov 21</time>)
|
61
|
+
assert_equal expected, local_time(@time, format: :time_formats_simple_time)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_local_time_with_missing_i18n_and_date_formats_format
|
65
|
+
expected = %Q(<time data-format="%B %e, %Y %l:%M%P" data-local="time" datetime="#{@time_js}">November 21, 2013 6:00am</time>)
|
66
|
+
assert_equal expected, local_time(@time, format: :missing_format)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_local_time_with_date_formats_proc_format
|
70
|
+
Time::DATE_FORMATS[:proc] = proc { |time| "nope" }
|
71
|
+
expected = %Q(<time data-format="%B %e, %Y %l:%M%P" data-local="time" datetime="#{@time_js}">November 21, 2013 6:00am</time>)
|
72
|
+
assert_equal expected, local_time(@time, format: :proc)
|
73
|
+
end
|
74
|
+
|
48
75
|
def test_local_time_with_options
|
49
76
|
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}" style="display:none">Nov 21</time>)
|
50
77
|
assert_equal expected, local_time(@time, format: '%b %e', style: 'display:none')
|
@@ -58,7 +85,22 @@ class LocalTimeHelperTest < MiniTest::Unit::TestCase
|
|
58
85
|
|
59
86
|
def test_local_date_with_format
|
60
87
|
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}">Nov 21</time>)
|
61
|
-
assert_equal expected, local_date(@time, format: '%b %e')
|
88
|
+
assert_equal expected, local_date(@time.to_date, format: '%b %e')
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_local_date_with_i18n_format
|
92
|
+
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}">Nov 21</time>)
|
93
|
+
assert_equal expected, local_date(@time.to_date, format: :simple_date)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_local_date_with_date_formats_format
|
97
|
+
expected = %Q(<time data-format="%b %e" data-local="time" datetime="#{@time_js}">Nov 21</time>)
|
98
|
+
assert_equal expected, local_date(@time.to_date, format: :date_formats_simple_date)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_local_date_with_missing_i18n_and_date_formats_format
|
102
|
+
expected = %Q(<time data-format="%B %e, %Y %l:%M%P" data-local="time" datetime="#{@time_js}">November 21, 2013 6:00am</time>)
|
103
|
+
assert_equal expected, local_date(@time.to_date, format: :missing_date_format)
|
62
104
|
end
|
63
105
|
|
64
106
|
def test_local_time_ago
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module "public API"
|
2
|
+
|
3
|
+
test "strftime", ->
|
4
|
+
datetime = "2013-11-12T12:13:00Z"
|
5
|
+
time = new Date Date.parse datetime
|
6
|
+
format = "%B %e, %Y %l:%M%P"
|
7
|
+
results = LocalTime.strftime time, format
|
8
|
+
|
9
|
+
ok results
|
10
|
+
|
11
|
+
datetimeParsed = moment datetime
|
12
|
+
localParsed = moment results, "MMMM D, YYYY h:mma"
|
13
|
+
|
14
|
+
ok datetimeParsed.isValid()
|
15
|
+
ok localParsed.isValid()
|
16
|
+
equal datetimeParsed.toString(), localParsed.toString()
|
17
|
+
|
18
|
+
test "relativeTimeAgo", ->
|
19
|
+
time = moment().subtract("minutes", 42).toDate()
|
20
|
+
results = LocalTime.relativeTimeAgo(time)
|
21
|
+
equal results, "42 minutes ago"
|
@@ -30,12 +30,11 @@ test "last week", ->
|
|
30
30
|
assertTimeAgo "#{day} at #{time}", "days", 5
|
31
31
|
|
32
32
|
test "this year", ->
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
clock = sinon.useFakeTimers(new Date(2013,11,11,11,11).getTime(), "Date")
|
34
|
+
date = moment().subtract("days", 7).format "MMM D"
|
35
|
+
assertTimeAgo "on #{date}", "days", 7
|
36
|
+
clock.restore()
|
37
|
+
|
39
38
|
test "last year", ->
|
40
39
|
date = moment().subtract("days", 366).format "MMM D, YYYY"
|
41
40
|
assertTimeAgo "on #{date}", "days", 366
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: local_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javan Makhmali
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: coffee-rails
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- test/javascripts/index.js.coffee
|
57
57
|
- test/javascripts/unit/local_time_test.js.coffee
|
58
58
|
- test/javascripts/unit/page_events_test.js.coffee
|
59
|
+
- test/javascripts/unit/public_api_test.js.coffee
|
59
60
|
- test/javascripts/unit/strftime_test.js.coffee
|
60
61
|
- test/javascripts/unit/time_ago_test.js.coffee
|
61
62
|
homepage:
|
@@ -89,5 +90,6 @@ test_files:
|
|
89
90
|
- test/javascripts/index.js.coffee
|
90
91
|
- test/javascripts/unit/local_time_test.js.coffee
|
91
92
|
- test/javascripts/unit/page_events_test.js.coffee
|
93
|
+
- test/javascripts/unit/public_api_test.js.coffee
|
92
94
|
- test/javascripts/unit/strftime_test.js.coffee
|
93
95
|
- test/javascripts/unit/time_ago_test.js.coffee
|