time_methods 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/time_methods.rb +103 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZGI0ODJmNjMzYjVhNzVlY2FlZGU4ZjU5MDZiNjU4MDk3NTExYzU0NQ==
5
+ data.tar.gz: !binary |-
6
+ ZjJmM2Q5YTY2ZmIzZTNiYmRiMGU3MGM4OTJjMjU5ZTM4ZDdlYjRkNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzRiMWM3Y2ZiNzk1YjA4ZTA2NzNhYmQ4ZWNjNDUwY2EzZGMzYzdkMTZkMWQ1
10
+ YmQzZTZiOTY4ODhmNmJhNjE2MTY4MmQ2ZDE4NTU2NTU5MjZiNGI5OWY2MmEz
11
+ NTk5ZmRkZmQ4NmZmNjk4NmNiMTViY2FhMjY3ZDMyZWU0Mjg3MTk=
12
+ data.tar.gz: !binary |-
13
+ NmVlZGFkYTdkMDA3NzE1NGI5ZWU5M2ViYmU0NTEwMGY3ZTg4NzUwYzEyZWYw
14
+ MGI0OGM5MzVmYWQ5ZDBkNGU0Y2E3ZmQxNTRiMDA0M2ZlMjc4MWNiOTc5OGI5
15
+ NjcwYjAzZDc0YjkzMjg1ZWY4M2ViYWVjYjUwZWJhZjAwZmI2ODg=
@@ -0,0 +1,103 @@
1
+ # coding: utf-8
2
+ # @author Jonathan Raiman
3
+
4
+ # perform relative time conversions really easily.
5
+ module TimeMethods
6
+
7
+ # Pluralizes a string by adding "s" to the element name when there are 0 or 2+ elements.
8
+ # @param number [FixNum] number of elements to pluralize
9
+ # @param text [String] the element being pluralized
10
+ # @return [String] the pluralized string.
11
+ def self.pluralize(number, text)
12
+ return "#{text}s" if number != 1
13
+ text
14
+ end
15
+
16
+ # convert time to non-relative human-readable format
17
+ # @param t [Time] the time to convert
18
+ # @return [String] the time in human-readable format (Not words per-se).
19
+ def self.factual_time(t)
20
+ msg = ""
21
+ if t.day == Time.now.day
22
+ msg += "Today"
23
+ elsif Time.now-3600*48 < t
24
+ msg += "Yesterday"
25
+ else
26
+ msg += t.strftime('%-d %B %Y')
27
+ end
28
+ msg += t.strftime(' %H:%M:%S')
29
+ # msg += " "
30
+ # msg += t.hour.to_s+":"+t.min.to_s+":"+t.sec.to_s
31
+ msg
32
+ end
33
+
34
+ # Converts time from an integer or float amount of seconds to words.
35
+ # @param diff_seconds [Fixnum, Float] time to convert to words
36
+ # @return [String] time in words elapsed
37
+ def self.relative_time(diff_seconds)
38
+ delta_t = (diff_seconds.class == Float) ? diff_seconds.to_i : diff_seconds
39
+ case delta_t
40
+ when 0 .. 5
41
+ if diff_seconds.class == Float
42
+ "#{sprintf('%.2f',diff_seconds)} "+pluralize(delta_t, 'second')
43
+ else
44
+ "#{delta_t} "+pluralize(delta_t, 'second')
45
+ end
46
+ when 6 .. 59
47
+ "#{delta_t} "+pluralize(delta_t, 'second')
48
+ when 60 .. (3600-1)
49
+ "#{delta_t/60} "+pluralize((delta_t/60), 'minute')
50
+ when 3600 .. (3600*24-1)
51
+ "#{delta_t/3600} "+pluralize((delta_t/3600), 'hour')
52
+ when (3600*24) .. (3600*24*7-1)
53
+ "#{delta_t/(3600*24)} "+pluralize((delta_t/(3600*24)), 'day')
54
+ when (3600*24*7) .. (3600*24*30-1)
55
+ "#{delta_t/(3600*24*7)} "+pluralize((delta_t/(3600*24*7)), 'week')
56
+ when (3600*24*30) .. (3600*24*365.25)
57
+ "#{delta_t/(3600*24*30)} "+pluralize((delta_t/(3600*24*30)), 'month')
58
+ when (3600*24*365.25) .. (3600*24*3652.5)
59
+ "#{delta_t/(3600*24*30)} "+pluralize((delta_t/(3600*24*365.25)), 'year')
60
+ else
61
+ "#{delta_t}s"
62
+ end
63
+ end
64
+
65
+ # Converts time into words with a Facebook like lingo ("just now", "an hour ago", etc...).
66
+ # @param start_time [Time] time to convert to words
67
+ # @return [String] time in words elapsed
68
+ def self.time_ago(start_time)
69
+ diff_seconds = Time.now.to_i - start_time.to_i
70
+ case diff_seconds
71
+ when 0 .. 59
72
+ "just now"
73
+ when 60 .. (3000-1)
74
+ "#{diff_seconds/60} "+pluralize((diff_seconds/60), 'minute')+" ago"
75
+ when 3000 .. (3500-1)
76
+ "almost an hour ago"
77
+ when 3500 .. (3700-1)
78
+ "an hour ago"
79
+ when 3700 .. (3900-1)
80
+ "over an hour ago"
81
+ when 3900 .. (3600*24-1)
82
+ "#{diff_seconds/3600} "+pluralize((diff_seconds/3600), 'hour')+" ago"
83
+ when (3600*24) .. (3600*24*7-1)
84
+ "#{diff_seconds/(3600*24)} "+pluralize((diff_seconds/(3600*24)), 'day')+" ago"
85
+ when (3600*24*7) .. (3600*24*30-1)
86
+ "#{diff_seconds/(3600*24*7)} "+pluralize((diff_seconds/(3600*24*7)), 'week')+" ago"
87
+ when (3600*24*30) .. (3600*24*365.25)
88
+ "#{diff_seconds/(3600*24*30)} "+pluralize((diff_seconds/(3600*24*30)), 'month')+" ago"
89
+ else
90
+ start_time.strftime('%-d %B %Y')
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ class Time
97
+ def ago
98
+ TimeMethods.time_ago(self)
99
+ end
100
+ def factual
101
+ TimeMethods.factual_time(self)
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_methods
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Raiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Converts time and time intervals to human readable forms.
14
+ email: jraiman@mit.edu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/time_methods.rb
20
+ homepage: http://github.org/JonathanRaiman/time_methods
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.1.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Converts time and time intervals to human readable forms.
44
+ test_files: []
45
+ has_rdoc: yard