theusual 0.0.7 → 0.0.8

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
  SHA1:
3
- metadata.gz: 6af68770c8a5f6f30a66ea5dc1288c437e82f39c
4
- data.tar.gz: 15e247e1587c85d4148d73ae4b7e9fe09d1dc627
3
+ metadata.gz: 7dacb5e7a014566aa0c349f8d1ebf456a7668657
4
+ data.tar.gz: b7ef75744482e6bffd73885d698337619e130ac4
5
5
  SHA512:
6
- metadata.gz: 54c0b663bbe157dc87899136c0fe69f41b048ca68ea1f1ffa069294724603542c5f4980eac736989647e1e1e8e8e4227dcccf29b472c716b9e546a3317d91727
7
- data.tar.gz: 2f2954952235ae2812cb04e51d45947a48e881a2aabd4a71b0feb72b8c42f1b5f4aaee7b8a13d0a8e88bbfff61d4ca0857ef97c5d20157dfd0693ba46874867f
6
+ metadata.gz: dc9842b4be2725073029d4fae4c424df5ea22cf7af53fa669c36cf8ab85fde7bdff882a5f05348b9b7385912e52a9cca759989be4715ee36922732eba7e82739
7
+ data.tar.gz: 65cf8436c7fa60ed4b15f32267c5d5a15a8432f965a87e724cc5ed9bdd85a0f322e23c05384a8d2a1398bcfcca8eb79f925fd5ca061e13d6ac8812d975846a19
data/lib/theusual/time.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  class Time
2
+ MINUTES_IN_THREE_QUARTERS_YEAR = 394200
3
+ MINUTES_IN_QUARTER_YEAR = 131400
4
+ MINUTES_IN_YEAR = 525600
5
+
2
6
 
3
7
  def self.at_ms ms
4
8
  # convert an integer representing Unix time in milliseconds
@@ -10,23 +14,78 @@ class Time
10
14
  (to_f * 1_000).to_i
11
15
  end
12
16
 
17
+ # inspired by actionview/lib/action_view/helpers/date_helper.rb
18
+ def humanize(to_time = nil, options = {})
19
+ from_time = self.utc
20
+ to_time ||= Time.now.utc
21
+ from_time, to_time = to_time, from_time if from_time > to_time
22
+
23
+ distance_in_minutes = ((to_time.to_i - from_time.to_i) / 60.0).to_i
24
+ distance_in_seconds = (to_time.to_i - from_time.to_i).to_i
13
25
 
14
- def humanize
15
- a = (Time.now.utc - self.utc).to_i
16
-
17
- case a
18
- when 0 then 'just now'
19
- when 1 then 'a second ago'
20
- when 2..59 then a.to_s + ' seconds ago'
21
- when 60..119 then 'a minute ago' #120 = 2 minutes
22
- when 120..3540 then (a / 60).to_i.to_s + ' minutes ago'
23
- when 3541..7100 then 'an hour ago' # 3600 = 1 hour
24
- when 7101..82800 then ((a + 99) / 3600).to_i.to_s + ' hours ago'
25
- when 82801..172000 then 'a day ago' # 86400 = 1 day
26
- when 172001..518400 then ((a + 800) / (60 * 60 * 24)).to_i.to_s + ' days ago'
27
- when 518400..1036800 then 'a week ago'
28
- else ((a+180000)/(60*60*24*7)).to_i.to_s + ' weeks ago'
26
+ res = case distance_in_minutes
27
+ when 0
28
+ if options[:seconds]
29
+ case distance_in_seconds
30
+ when 1 then [ :a_second ]
31
+ when 2..4 then [ :less_than_x_seconds, 5 ]
32
+ when 5..9 then [ :less_than_x_seconds, 10 ]
33
+ when 10..29 then [ :less_than_x_seconds, 30 ]
34
+ when 30..39 then [ :half_a_minute ]
35
+ when 40..59 then [ :less_than_a_minute ]
36
+ else [ :a_minute ]
37
+ end
38
+ else
39
+ if distance_in_seconds == 0
40
+ [ :just_now ]
41
+ else
42
+ [ :less_than_a_minute ]
43
+ end
44
+ end
45
+ when 1 then [ :a_minute ]
46
+ when 2...45 then [ :x_minutes, distance_in_minutes ]
47
+ when 45...59 then [ :about_an_hour ]
48
+ when 60 then [ :an_hour ]
49
+ when 61...90 then [ :about_an_hour ]
50
+ # 90 mins up to 24 hours
51
+ when 90...1440 then [ :about_x_hours, (distance_in_minutes.to_f / 60.0).round ]
52
+ # 24 hours up to hours
53
+ when 1440...2160 then [ :a_day ]
54
+ # 36 hours up to 30 days
55
+ when 2160...43200 then [ :x_days, (distance_in_minutes.to_f / 1440.0).round ]
56
+ # 30 days up to 60 days
57
+ when 43200...86400 then [ :about_x_months, (distance_in_minutes.to_f / 43200.0).round ]
58
+ # 60 days up to 365 days
59
+ when 86400...525600 then [ :x_months, (distance_in_minutes.to_f / 43200.0).round ]
60
+ else
61
+ fyear = from_time.year
62
+ fyear += 1 if from_time.month >= 3
63
+ tyear = to_time.year
64
+ tyear -= 1 if to_time.month < 3
65
+ leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count{|x| Date.leap?(x)}
66
+ minute_offset_for_leap_year = leap_years * 1440
67
+ # Discount the leap year days when calculating year distance.
68
+ # e.g. if there are 20 leap year days between 2 dates having the same day
69
+ # and month then the based on 365 days calculation
70
+ # the distance in years will come out to over 80 years when in written
71
+ # English it would read better as about 80 years.
72
+ minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year
73
+ remainder = (minutes_with_offset % MINUTES_IN_YEAR)
74
+ distance_in_years = (minutes_with_offset.div MINUTES_IN_YEAR)
75
+ if remainder < MINUTES_IN_QUARTER_YEAR
76
+ [ :about_x_years, distance_in_years ]
77
+ elsif remainder < MINUTES_IN_THREE_QUARTERS_YEAR
78
+ [ :over_x_years, distance_in_years ]
79
+ else
80
+ [ :almost_x_years, distance_in_years + 1 ]
81
+ end
29
82
  end
83
+
84
+ if res.count > 1
85
+ res.first.to_s.sub 'x', res.last.to_s
86
+ else
87
+ res.first.to_s
88
+ end.gsub '_', ' '
30
89
  end
31
90
 
32
91
  end
@@ -1,3 +1,3 @@
1
1
  module TheUsual
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
data/test/test_time.rb CHANGED
@@ -5,26 +5,58 @@ TheUsual::load 'time'
5
5
 
6
6
  class TimeTest < Minitest::Test
7
7
 
8
- def test_all
8
+ def test_ms
9
+ ts = Time.now.utc
10
+
11
+ assert(ts.to_ms.class < Integer)
12
+
13
+ assert(
14
+ # to_ms will truncate, so allow for small error
15
+ ts - Time.at_ms(ts.to_ms) <= 0.001
16
+ )
17
+ end
18
+
19
+
20
+ def test_humanize
9
21
  assert_equal(
10
22
  'just now',
11
23
  Time.new.humanize
12
24
  )
13
25
 
14
26
  assert_equal(
15
- 'a second ago',
16
- (Time.new - 1).humanize
27
+ 'less than a minute',
28
+ (Time.new - 50).humanize
17
29
  )
18
30
 
19
31
  assert_equal(
20
- '2 seconds ago',
21
- (Time.new - 2).humanize
32
+ 'a second',
33
+ (Time.new - 1).humanize(nil, seconds: true)
22
34
  )
23
35
 
24
36
  assert_equal(
25
- 'a minute ago',
37
+ 'a minute',
26
38
  (Time.new - 60).humanize
27
39
  )
40
+
41
+ assert_equal(
42
+ 'an hour',
43
+ (Time.new - 60 * 60).humanize
44
+ )
45
+
46
+ assert_equal(
47
+ 'an hour',
48
+ (Time.new - 60 * 60 - 5).humanize
49
+ )
50
+
51
+ assert_equal(
52
+ 'about an hour',
53
+ (Time.new - 60 * 61).humanize
54
+ )
55
+
56
+ assert_equal(
57
+ 'a day',
58
+ (Time.new - 60 * 60 * 24).humanize
59
+ )
28
60
  end
29
61
 
30
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theusual
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-14 00:00:00.000000000 Z
11
+ date: 2017-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake