humantime 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +5 -0
  2. data/README.md +22 -0
  3. data/lib/humantime.rb +137 -0
  4. metadata +111 -0
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Copyright (c) 2012 James Inman <james@jamesinman.co.uk>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ Gem to return human readable times.
2
+
3
+ #### Usage
4
+
5
+ Output a number of seconds in human readable form:
6
+
7
+ >> HumanTime.output 1
8
+ => "1 second"
9
+ >> HumanTime.output 45
10
+ => "45 seconds"
11
+ >> HumanTime.output 70
12
+ => "1 minute 10 seconds"
13
+ >> HumanTime.output 234
14
+ => "3 minutes 54 seconds"
15
+ >> HumanTime.output 53005
16
+ => "14 hours 43 minutes"
17
+ >> HumanTime.output 530052
18
+ => "6 days"
19
+ >> HumanTime.output 1209600
20
+ => "14 days"
21
+ >> HumanTime.output 5452302
22
+ => "2 months"
data/lib/humantime.rb ADDED
@@ -0,0 +1,137 @@
1
+ class HumanTime
2
+
3
+ SECOND = 1
4
+ MINUTE = 60
5
+ HOUR = 3600
6
+ DAY = 86400
7
+ MONTH = 2629743
8
+ YEAR = 31556926
9
+
10
+ # Parse an integer and return the string representation of a time
11
+ def self.output int
12
+ str = []
13
+ display = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
14
+
15
+ # Years
16
+ if int > ( YEAR - 1 ) && display.include?('years')
17
+ years = int / YEAR
18
+ int = int - ( years * YEAR )
19
+ display.delete 'minutes'
20
+ display.delete 'seconds'
21
+ display.delete 'hours'
22
+
23
+ if years.round == 1
24
+ str << "#{years.round} year"
25
+ else
26
+ str << "#{years.round} years"
27
+ display.delete('months')
28
+ end
29
+ end
30
+
31
+ # Months
32
+ if int < YEAR && int > ( MONTH - 1 ) && display.include?('months')
33
+ months = int / MONTH
34
+ int = int - ( months * MONTH )
35
+ display.delete 'minutes'
36
+ display.delete 'seconds'
37
+ display.delete 'hours'
38
+
39
+ if months.round == 1
40
+ str << "#{months.round} month"
41
+ elsif months.round == 12
42
+ str << "1 year"
43
+ else
44
+ str << "#{months.round} months"
45
+ display.delete 'days'
46
+ end
47
+ end
48
+
49
+ # Days
50
+ if int < MONTH && int > ( DAY - 1 ) && display.include?('hours')
51
+ days = int / DAY
52
+ int = int - ( days * DAY )
53
+ display.delete 'minutes'
54
+ display.delete 'seconds'
55
+
56
+ if days.round == 1
57
+ str << "#{days.round} day"
58
+ else
59
+ str << "#{days.round} days"
60
+ display.delete 'hours'
61
+ end
62
+ end
63
+
64
+ # Hours
65
+ if int < DAY && int > ( HOUR - 1 ) && display.include?('hours')
66
+ hours = int / HOUR
67
+ int = int - ( hours * HOUR )
68
+ display.delete 'seconds'
69
+
70
+ if hours.round == 1
71
+ str << "#{hours.round} hour"
72
+ else
73
+ str << "#{hours.round} hours"
74
+ end
75
+ end
76
+
77
+ # Minutes
78
+ if int < HOUR && int > ( MINUTE - 1 ) && display.include?('minutes')
79
+ mins = int / MINUTE
80
+ int = int - ( mins * MINUTE )
81
+
82
+ if mins.round == 1
83
+ str << "#{mins.round} minute"
84
+ else
85
+ str << "#{mins.round} minutes"
86
+ end
87
+ end
88
+
89
+ # Seconds
90
+ if int < MINUTE && int > 0 && display.include?('seconds')
91
+ if int.round == 1
92
+ str << "#{int.round} second"
93
+ else
94
+ str << "#{int.round} seconds"
95
+ end
96
+ end
97
+
98
+ str.join(' ')
99
+ end
100
+
101
+ # From https://github.com/hpoydar/chronic_duration
102
+ # For future parsing methods
103
+ def mappings
104
+ {
105
+ 'seconds' => 'seconds',
106
+ 'second' => 'seconds',
107
+ 'secs' => 'seconds',
108
+ 'sec' => 'seconds',
109
+ 's' => 'seconds',
110
+ 'minutes' => 'minutes',
111
+ 'minute' => 'minutes',
112
+ 'mins' => 'minutes',
113
+ 'min' => 'minutes',
114
+ 'm' => 'minutes',
115
+ 'hours' => 'hours',
116
+ 'hour' => 'hours',
117
+ 'hrs' => 'hours',
118
+ 'hr' => 'hours',
119
+ 'h' => 'hours',
120
+ 'days' => 'days',
121
+ 'day' => 'days',
122
+ 'dy' => 'days',
123
+ 'd' => 'days',
124
+ 'weeks' => 'weeks',
125
+ 'week' => 'weeks',
126
+ 'w' => 'weeks',
127
+ 'months' => 'months',
128
+ 'mos' => 'months',
129
+ 'month' => 'months',
130
+ 'years' => 'years',
131
+ 'year' => 'years',
132
+ 'yrs' => 'years',
133
+ 'y' => 'years'
134
+ }
135
+ end
136
+
137
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: humantime
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - James Inman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-25 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ name: rcov
31
+ type: :development
32
+ prerelease: false
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ name: bundler
45
+ type: :development
46
+ prerelease: false
47
+ requirement: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ version_requirements: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ hash: 49
55
+ segments:
56
+ - 1
57
+ - 8
58
+ - 3
59
+ version: 1.8.3
60
+ name: jeweler
61
+ type: :development
62
+ prerelease: false
63
+ requirement: *id003
64
+ description: Outputs human readable times
65
+ email: james@jamesinman.co.uk
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files:
71
+ - LICENSE
72
+ - README.md
73
+ files:
74
+ - LICENSE
75
+ - README.md
76
+ - lib/humantime.rb
77
+ homepage: http://github.com/jfi/humantime
78
+ licenses:
79
+ - ISC
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.17
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Outputs human readable times
110
+ test_files: []
111
+