pretty_time 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/pretty_time/pretty_time.rb +230 -0
- data/lib/pretty_time.rb +1 -1
- data/pretty_time.gemspec +5 -5
- data/spec/pretty_time/pretty_time_spec.rb +240 -0
- metadata +6 -6
- data/lib/pretty_time/core.rb +0 -135
- data/spec/pretty_time/core_spec.rb +0 -127
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,230 @@
|
|
1
|
+
#
|
2
|
+
# Simple time serializer with two main functionalities:
|
3
|
+
# 1. Takes a time in seconds and converts it into a pretty string
|
4
|
+
# 2. Takes a pretty string and converts it into time in seconds
|
5
|
+
#
|
6
|
+
# Dependencies:
|
7
|
+
# ActiveSupport
|
8
|
+
#
|
9
|
+
class PrettyTime
|
10
|
+
|
11
|
+
# Serializes(if you will) time in seconds supplied as integer to a
|
12
|
+
# pretty time string
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# PrettyTime.load(130) # 2 minutes 10 seconds
|
16
|
+
def self.load(time_in_sec)
|
17
|
+
pretty_time.load(time_in_sec)
|
18
|
+
end
|
19
|
+
|
20
|
+
# De-serializes(if you will) a pretty time into time in seconds
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# PrettyTime.dump('2 minutes 10 seconds') # 130
|
24
|
+
def self.dump(time_as_pretty_string)
|
25
|
+
pretty_time.dump(time_as_pretty_string)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Creates an instance of itself if one does not exist
|
29
|
+
def self.pretty_time
|
30
|
+
@pretty_time ||= Core.new
|
31
|
+
end
|
32
|
+
|
33
|
+
# Accepts a block and passes the configuration instance to the block
|
34
|
+
def self.configuration
|
35
|
+
yield config
|
36
|
+
end
|
37
|
+
|
38
|
+
# Creates a new configuration instance
|
39
|
+
def self.config
|
40
|
+
@config ||= PrettyTime::Configuration.new
|
41
|
+
end
|
42
|
+
|
43
|
+
# Parent Class for unit's of time
|
44
|
+
# for exaple: Hours, Minutes and Seconds
|
45
|
+
class UnitOfTime
|
46
|
+
attr_accessor :value
|
47
|
+
|
48
|
+
def initialize(value)
|
49
|
+
@value = value
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_seconds
|
53
|
+
@value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Subclass of UnitofTime
|
58
|
+
# converts hours into seconds
|
59
|
+
class Hours < UnitOfTime
|
60
|
+
def to_seconds
|
61
|
+
@value.hours
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Subclass of UnitofTime
|
66
|
+
# converts minutes into seconds
|
67
|
+
class Minutes < UnitOfTime
|
68
|
+
def to_seconds
|
69
|
+
@value.minutes
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Subclass of UnitofTime
|
74
|
+
class Seconds < UnitOfTime
|
75
|
+
end
|
76
|
+
|
77
|
+
# Various mappings to allow one to pass in strings like:
|
78
|
+
#
|
79
|
+
# 4 hours
|
80
|
+
# 4 hrs
|
81
|
+
# 4 h
|
82
|
+
# Same goes for minutes and seconds.
|
83
|
+
H = Hrs = Hr = Hour = Hours
|
84
|
+
M = Mins = Min = Minute = Minutes
|
85
|
+
S = Secs = Sec = Second = Seconds
|
86
|
+
|
87
|
+
# Configuration Class
|
88
|
+
# Defines the suffixes to be used
|
89
|
+
# when time in seconds is converted to
|
90
|
+
# pretty time.
|
91
|
+
#
|
92
|
+
# Example:
|
93
|
+
#
|
94
|
+
# PrettyTime.configuration do |config|
|
95
|
+
# config.hours_suffix = 'hrs'
|
96
|
+
# config.minutes_suffix = 'mins'
|
97
|
+
# config.seconds_suffix = 'secs'
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# PrettyTime.load(7805) # Returns 2 hrs 10 mins 5 secs
|
101
|
+
#
|
102
|
+
# Default values are:
|
103
|
+
# hours
|
104
|
+
# minutes
|
105
|
+
# seconds
|
106
|
+
#
|
107
|
+
class Configuration
|
108
|
+
|
109
|
+
DEFAULT_HOURS_SUFFIX = "hours"
|
110
|
+
DEFAULT_MINUTES_SUFFIX = "minutes"
|
111
|
+
DEFAULT_SECONDS_SUFFIX = "seconds"
|
112
|
+
|
113
|
+
attr_accessor :hours_suffix, :minutes_suffix, :seconds_suffix
|
114
|
+
|
115
|
+
def hours_suffix
|
116
|
+
@hours_suffix || DEFAULT_HOURS_SUFFIX
|
117
|
+
end
|
118
|
+
|
119
|
+
def minutes_suffix
|
120
|
+
@minutes_suffix || DEFAULT_MINUTES_SUFFIX
|
121
|
+
end
|
122
|
+
|
123
|
+
def seconds_suffix
|
124
|
+
@seconds_suffix || DEFAULT_SECONDS_SUFFIX
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
# Core class which provides the entry point to other useful methods
|
130
|
+
class Core
|
131
|
+
|
132
|
+
# Core method that does all the work
|
133
|
+
def load(time_in_secs)
|
134
|
+
hours, minutes, seconds = hours_and_minutes_and_seconds(time_in_secs)
|
135
|
+
|
136
|
+
time_as_pretty_string = ""
|
137
|
+
|
138
|
+
if has_hours?(hours)
|
139
|
+
time_as_pretty_string << "#{with_suffix(hours, PrettyTime.config.hours_suffix)}"
|
140
|
+
end
|
141
|
+
|
142
|
+
if has_minutes?(minutes)
|
143
|
+
if has_hours?(hours)
|
144
|
+
time_as_pretty_string << " "
|
145
|
+
end
|
146
|
+
time_as_pretty_string << "#{with_suffix(minutes, PrettyTime.config.minutes_suffix)}"
|
147
|
+
end
|
148
|
+
|
149
|
+
if has_seconds?(seconds)
|
150
|
+
if has_hours?(hours) || has_minutes?(minutes)
|
151
|
+
time_as_pretty_string << " "
|
152
|
+
end
|
153
|
+
time_as_pretty_string << "#{with_suffix(seconds, PrettyTime.config.seconds_suffix)}"
|
154
|
+
end
|
155
|
+
|
156
|
+
time_as_pretty_string
|
157
|
+
end
|
158
|
+
|
159
|
+
# Core method that does all the work
|
160
|
+
def dump(time_as_pretty_string)
|
161
|
+
time_in_secs = 0
|
162
|
+
match_data_as_array = match_it(time_as_pretty_string).to_a
|
163
|
+
match_data_as_array.slice(1..match_data_as_array.length).each_slice(2).each do |match_array|
|
164
|
+
time_in_secs += "PrettyTime::#{match_array[1].strip.classify}".constantize.new(match_array[0].to_i).to_seconds
|
165
|
+
end
|
166
|
+
|
167
|
+
time_in_secs
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
# Converts time in seconds to an array of hours, minutes and seconds
|
174
|
+
def hours_and_minutes_and_seconds(time_in_sec)
|
175
|
+
minutes, seconds = minutes_and_seconds(time_in_sec)
|
176
|
+
hours = 0
|
177
|
+
if minutes >= 60
|
178
|
+
hours = minutes / 60
|
179
|
+
minutes = minutes % 60
|
180
|
+
end
|
181
|
+
[hours, minutes, seconds]
|
182
|
+
end
|
183
|
+
|
184
|
+
# Converts time in seconds to an array of minutes and seconds
|
185
|
+
def minutes_and_seconds(time_in_sec)
|
186
|
+
[time_in_sec / 60, time_in_sec % 60]
|
187
|
+
end
|
188
|
+
|
189
|
+
# Checks if supplied value is zero
|
190
|
+
def non_zero?(value)
|
191
|
+
value != 0
|
192
|
+
end
|
193
|
+
|
194
|
+
# Adds the correct suffix
|
195
|
+
# Example:
|
196
|
+
#
|
197
|
+
# 1 hour # becuase it's == 1 so singular
|
198
|
+
# 3 hours # becuase it's > 1 so plural
|
199
|
+
def with_suffix(value, type)
|
200
|
+
if value > 1
|
201
|
+
return "#{value} #{type}"
|
202
|
+
end
|
203
|
+
|
204
|
+
return "#{value} #{type.gsub(/s$/, '')}"
|
205
|
+
end
|
206
|
+
|
207
|
+
# Takes a string (i.e. time as a string) and matches it against various regular expressions
|
208
|
+
# and returns the match as MatchData
|
209
|
+
# Example:
|
210
|
+
# match_it("4 hours 40 minutes")
|
211
|
+
# match_it("4 hrs 40 mins")
|
212
|
+
# match_it("4 hrs 1 min")
|
213
|
+
#
|
214
|
+
def match_it(time_as_pretty_string)
|
215
|
+
/^(\d+) (hours?) (\d+) (minutes?) (\d+) (seconds?)/.match(time_as_pretty_string) ||
|
216
|
+
/^(\d+) (hours?) (\d+) (minutes?)/.match(time_as_pretty_string) ||
|
217
|
+
/^(\d+) (minutes?) (\d+) (seconds?)/.match(time_as_pretty_string) ||
|
218
|
+
/^(\d+) (hours?) (\d+) (seconds?)/.match(time_as_pretty_string) ||
|
219
|
+
/^(\d+)( h| m| s)/.match(time_as_pretty_string) ||
|
220
|
+
/^(\d+)( hours?| minutes?| seconds?)/.match(time_as_pretty_string) ||
|
221
|
+
/^(\d+)( hrs?| mins?| secs?)/.match(time_as_pretty_string)
|
222
|
+
end
|
223
|
+
|
224
|
+
alias :has_hours? :non_zero?
|
225
|
+
alias :has_minutes? :non_zero?
|
226
|
+
alias :has_seconds? :non_zero?
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
data/lib/pretty_time.rb
CHANGED
data/pretty_time.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pretty_time}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Anuj Dutta"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-15}
|
13
13
|
s.description = %q{Serializes time to pretty time string and vica-versa}
|
14
14
|
s.email = %q{anuj@andhapp.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,9 +26,9 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"lib/pretty_time.rb",
|
29
|
-
"lib/pretty_time/
|
29
|
+
"lib/pretty_time/pretty_time.rb",
|
30
30
|
"pretty_time.gemspec",
|
31
|
-
"spec/pretty_time/
|
31
|
+
"spec/pretty_time/pretty_time_spec.rb",
|
32
32
|
"spec/spec_helper.rb"
|
33
33
|
]
|
34
34
|
s.homepage = %q{http://github.com/andhapp/pretty_time}
|
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.rubygems_version = %q{1.6.2}
|
38
38
|
s.summary = %q{Serialize time}
|
39
39
|
s.test_files = [
|
40
|
-
"spec/pretty_time/
|
40
|
+
"spec/pretty_time/pretty_time_spec.rb",
|
41
41
|
"spec/spec_helper.rb"
|
42
42
|
]
|
43
43
|
|
@@ -0,0 +1,240 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "PrettyTime" do
|
4
|
+
|
5
|
+
context "On request to #dump" do
|
6
|
+
|
7
|
+
it 'should convert A hours to X seconds' do
|
8
|
+
PrettyTime.dump("4 hours").should == 4.hours.to_i
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should convert B minutes to X seconds' do
|
12
|
+
PrettyTime.dump("40 minutes").should == 40.minutes.to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return X seconds as X seconds' do
|
16
|
+
PrettyTime.dump("40 seconds").should == 40
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should convert A hours B minutes to X seconds' do
|
20
|
+
PrettyTime.dump("4 hours 40 minutes").should == 4.hours.to_i + 40.minutes.to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should convert B minutes C seconds to X seconds' do
|
24
|
+
PrettyTime.dump("40 minutes 30 seconds").should == 40.minutes.to_i + 30
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should convert A hours C seconds to X seconds' do
|
28
|
+
PrettyTime.dump("4 hours 30 seconds").should == 4.hours.to_i + 30
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should convert A hours B minutes C seconds to X seconds' do
|
32
|
+
PrettyTime.dump("4 hours 40 minutes 30 seconds").should == 4.hours.to_i + 40.minutes.to_i + 30
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should convert A hrs to X seconds' do
|
36
|
+
PrettyTime.dump("4 hrs").should == 4.hours.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should convert B mins to X seconds' do
|
40
|
+
PrettyTime.dump("4 mins").should == 4.minutes.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should convert C secs to X seconds' do
|
44
|
+
PrettyTime.dump("4 secs").should == 4
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should convert A h to X seconds' do
|
48
|
+
PrettyTime.dump("4 h").should == 4.hours.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should convert B m to X seconds' do
|
52
|
+
PrettyTime.dump("4 m").should == 4.minutes.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should convert C s to X seconds' do
|
56
|
+
PrettyTime.dump("4 s").should == 4
|
57
|
+
end
|
58
|
+
|
59
|
+
context "with fence post cases" do
|
60
|
+
|
61
|
+
it 'should convert 1 hour to 3600 seconds' do
|
62
|
+
PrettyTime.dump("1 hour").should == 3600
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should convert 1 minute to 60 seconds' do
|
66
|
+
PrettyTime.dump("1 minute").should == 60
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should return 1 second as 1 second' do
|
70
|
+
PrettyTime.dump("1 second").should == 1
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should convert 1 hour 1 minute to 3660 seconds' do
|
74
|
+
PrettyTime.dump("1 hour 1 minute").should == 3660
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should convert 1 minute 1 second to 61 seconds' do
|
78
|
+
PrettyTime.dump("1 minute 1 second").should == 61
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should convert 1 hour 1 minute 1 second to 3661 seconds' do
|
82
|
+
PrettyTime.dump("1 hour 1 minute 1 second").should == 3661
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should convert 1 hr to 3600 seconds' do
|
86
|
+
PrettyTime.dump("1 hr").should == 1.hours.to_i
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should convert 1 min to 60 seconds' do
|
90
|
+
PrettyTime.dump("1 min").should == 1.minutes.to_i
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should convert 1 sec to 1 second' do
|
94
|
+
PrettyTime.dump("1 sec").should == 1
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should convert 1 hr to 3600 seconds' do
|
98
|
+
PrettyTime.dump("1 h").should == 1.hours.to_i
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should convert 1 min to 60 seconds' do
|
102
|
+
PrettyTime.dump("1 m").should == 1.minutes.to_i
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should convert 1 sec to 1 second' do
|
106
|
+
PrettyTime.dump("1 s").should == 1
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "On request to #load" do
|
113
|
+
|
114
|
+
it 'should convert 7200 seconds to 2 hours' do
|
115
|
+
PrettyTime.load(2.hours).should == "2 hours"
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should convert 600 seconds to 10 minutes' do
|
119
|
+
PrettyTime.load(10.minutes.to_i).should == "10 minutes"
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should return 59 seconds as 59 seconds' do
|
123
|
+
PrettyTime.load(59).should == "59 seconds"
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should convert 7800 seconds to 2 hours 10 minutes' do
|
127
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i).should == "2 hours 10 minutes"
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should convert 7210 seconds to 2 hours 10 seconds' do
|
131
|
+
PrettyTime.load(2.hours.to_i + 10).should == "2 hours 10 seconds"
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should convert 130 seconds to 2 minutes 10 seconds' do
|
135
|
+
PrettyTime.load(2.minutes.to_i + 10).should == "2 minutes 10 seconds"
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should convert 7810 seconds to 2 hours 10 minutes 10 seconds' do
|
139
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i + 10).should == "2 hours 10 minutes 10 seconds"
|
140
|
+
end
|
141
|
+
|
142
|
+
context "with configuration defined" do
|
143
|
+
|
144
|
+
it 'with hours_suffix as hrs and minutes and seconds as default should convert 7800 seconds to 2 hrs 10 minutes' do
|
145
|
+
PrettyTime.configuration do |config|
|
146
|
+
config.hours_suffix = "hrs"
|
147
|
+
end
|
148
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i).should == "2 hrs 10 minutes"
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'with minutes_suffix as mins and hours and seconds as default should convert 7800 seconds to 2 hours 10 mins' do
|
152
|
+
PrettyTime.configuration do |config|
|
153
|
+
config.hours_suffix = "hours"
|
154
|
+
config.minutes_suffix = "mins"
|
155
|
+
end
|
156
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i).should == "2 hours 10 mins"
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'with seconds_suffix as secs and minutes and hours as default should convert 7805 seconds to 2 hours 10 minutes 5 secs' do
|
160
|
+
PrettyTime.configuration do |config|
|
161
|
+
config.hours_suffix = "hours"
|
162
|
+
config.minutes_suffix = "minutes"
|
163
|
+
config.seconds_suffix = "secs"
|
164
|
+
end
|
165
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i + 5).should == "2 hours 10 minutes 5 secs"
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
context "with fence post cases" do
|
171
|
+
|
172
|
+
before(:each) do
|
173
|
+
PrettyTime.configuration do |config|
|
174
|
+
config.hours_suffix = "hours"
|
175
|
+
config.minutes_suffix = "minutes"
|
176
|
+
config.seconds_suffix = "seconds"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'should convert 3600 seconds to 1 hour' do
|
181
|
+
PrettyTime.load(1.hours).should == "1 hour"
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'should convert 60 seconds to 1 minute' do
|
185
|
+
PrettyTime.load(1.minute.to_i).should == "1 minute"
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'should return 1 second as 1 second' do
|
189
|
+
PrettyTime.load(1).should == "1 second"
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should convert 3660 seconds to 1 hour 1 minute' do
|
193
|
+
PrettyTime.load(1.hours.to_i + 1.minute.to_i).should == "1 hour 1 minute"
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'should convert 3601 seconds to 1 hour 1 second' do
|
197
|
+
PrettyTime.load(1.hours.to_i + 1).should == "1 hour 1 second"
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'should convert 61 seconds to 1 minute 1 second' do
|
201
|
+
PrettyTime.load(1.minutes.to_i + 1).should == "1 minute 1 second"
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'should convert 3661 seconds to 1 hour 1 minute 1 second' do
|
205
|
+
PrettyTime.load(1.hours.to_i + 1.minutes.to_i + 1).should == "1 hour 1 minute 1 second"
|
206
|
+
end
|
207
|
+
|
208
|
+
context "with configuration defined" do
|
209
|
+
|
210
|
+
it 'with hours_suffix as hrs and minutes and seconds as default should convert 3610 seconds to 1 hr 10 minutes' do
|
211
|
+
PrettyTime.configuration do |config|
|
212
|
+
config.hours_suffix = "hrs"
|
213
|
+
end
|
214
|
+
PrettyTime.load(1.hours.to_i + 10.minutes.to_i).should == "1 hr 10 minutes"
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'with minutes_suffix as mins and hours and seconds as default should convert 7260 seconds to 2 hours 1 min' do
|
218
|
+
PrettyTime.configuration do |config|
|
219
|
+
config.hours_suffix = "hours"
|
220
|
+
config.minutes_suffix = "mins"
|
221
|
+
end
|
222
|
+
PrettyTime.load(2.hours.to_i + 1.minutes.to_i).should == "2 hours 1 min"
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'with seconds_suffix as secs and minutes and hours as default should convert 7801 seconds to 2 hours 10 minutes 1 sec' do
|
226
|
+
PrettyTime.configuration do |config|
|
227
|
+
config.hours_suffix = "hours"
|
228
|
+
config.minutes_suffix = "minutes"
|
229
|
+
config.seconds_suffix = "secs"
|
230
|
+
end
|
231
|
+
PrettyTime.load(2.hours.to_i + 10.minutes.to_i + 1).should == "2 hours 10 minutes 1 sec"
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: pretty_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Anuj Dutta
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-15 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -98,9 +98,9 @@ files:
|
|
98
98
|
- Rakefile
|
99
99
|
- VERSION
|
100
100
|
- lib/pretty_time.rb
|
101
|
-
- lib/pretty_time/
|
101
|
+
- lib/pretty_time/pretty_time.rb
|
102
102
|
- pretty_time.gemspec
|
103
|
-
- spec/pretty_time/
|
103
|
+
- spec/pretty_time/pretty_time_spec.rb
|
104
104
|
- spec/spec_helper.rb
|
105
105
|
has_rdoc: true
|
106
106
|
homepage: http://github.com/andhapp/pretty_time
|
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
116
|
requirements:
|
117
117
|
- - ">="
|
118
118
|
- !ruby/object:Gem::Version
|
119
|
-
hash:
|
119
|
+
hash: 423849249
|
120
120
|
segments:
|
121
121
|
- 0
|
122
122
|
version: "0"
|
@@ -134,5 +134,5 @@ signing_key:
|
|
134
134
|
specification_version: 3
|
135
135
|
summary: Serialize time
|
136
136
|
test_files:
|
137
|
-
- spec/pretty_time/
|
137
|
+
- spec/pretty_time/pretty_time_spec.rb
|
138
138
|
- spec/spec_helper.rb
|
data/lib/pretty_time/core.rb
DELETED
@@ -1,135 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Simple time serializer with two main functionalities:
|
3
|
-
# 1. Takes a time in seconds and converts it into a pretty string
|
4
|
-
# 2. Takes a pretty string and converts it into time in seconds
|
5
|
-
#
|
6
|
-
# Dependencies:
|
7
|
-
# ActiveSupport
|
8
|
-
#
|
9
|
-
module PrettyTime
|
10
|
-
|
11
|
-
#
|
12
|
-
# Serializes(if you will) time in seconds supplied as integer to a
|
13
|
-
# pretty time string
|
14
|
-
# Example:
|
15
|
-
#
|
16
|
-
# PrettyTime.load(130) # 2 minutes 10 seconds
|
17
|
-
#
|
18
|
-
def self.load(time_in_sec)
|
19
|
-
pretty_time.load(time_in_sec)
|
20
|
-
end
|
21
|
-
|
22
|
-
#
|
23
|
-
# De-serializes(if you will) a pretty time into time in seconds
|
24
|
-
# Example:
|
25
|
-
#
|
26
|
-
# PrettyTime.dump('2 minutes 10 seconds') # 130
|
27
|
-
#
|
28
|
-
def self.dump(time_as_pretty_string)
|
29
|
-
pretty_time.dump(time_as_pretty_string)
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Creates an instance of itself if one does not exist
|
34
|
-
#
|
35
|
-
def self.pretty_time
|
36
|
-
@pretty_time ||= Core.new
|
37
|
-
end
|
38
|
-
|
39
|
-
#
|
40
|
-
# Core class which provides the entry point to other useful methods
|
41
|
-
#
|
42
|
-
class Core
|
43
|
-
|
44
|
-
HOURS = "hours"
|
45
|
-
MINUTES = "minutes"
|
46
|
-
SECONDS = "seconds"
|
47
|
-
|
48
|
-
#
|
49
|
-
# Core method that does all the work
|
50
|
-
#
|
51
|
-
def load(time_in_sec)
|
52
|
-
hours, minutes, seconds = hours_and_minutes_and_seconds(time_in_sec)
|
53
|
-
|
54
|
-
pretty_time_string = ""
|
55
|
-
|
56
|
-
if has_hours?(hours)
|
57
|
-
pretty_time_string << "#{with_suffix(hours, HOURS)}"
|
58
|
-
end
|
59
|
-
|
60
|
-
if has_minutes?(minutes)
|
61
|
-
if has_hours?(hours)
|
62
|
-
pretty_time_string << " "
|
63
|
-
end
|
64
|
-
pretty_time_string << "#{with_suffix(minutes, MINUTES)}"
|
65
|
-
end
|
66
|
-
|
67
|
-
if has_seconds?(seconds)
|
68
|
-
if has_hours?(hours) || has_minutes?(minutes)
|
69
|
-
pretty_time_string << " "
|
70
|
-
end
|
71
|
-
pretty_time_string << "#{with_suffix(seconds, SECONDS)}"
|
72
|
-
end
|
73
|
-
|
74
|
-
pretty_time_string
|
75
|
-
end
|
76
|
-
|
77
|
-
#
|
78
|
-
# Core method that does all the work
|
79
|
-
#
|
80
|
-
def dump(time_as_pretty_string)
|
81
|
-
if match = /^(\d+) (hours?) (\d+) (minutes?) (\d+) (seconds?)/.match(time_as_pretty_string)
|
82
|
-
return match[1].to_i.send(:"#{match[2]}").to_i + match[3].to_i.send("#{match[4]}").to_i + match[5].to_i
|
83
|
-
elsif match = /^(\d+) (hours?) (\d+) (minutes?)/.match(time_as_pretty_string)
|
84
|
-
return match[1].to_i.send(:"#{match[2]}").to_i + match[3].to_i.send("#{match[4]}").to_i
|
85
|
-
elsif match = /^(\d+) (minutes?) (\d+) (seconds?)/.match(time_as_pretty_string)
|
86
|
-
return match[1].to_i.send(:"#{match[2]}").to_i + match[3].to_i
|
87
|
-
elsif match = /^(\d+) (hours?) (\d+) (seconds?)/.match(time_as_pretty_string)
|
88
|
-
return match[1].to_i.send(:"#{match[2]}").to_i + match[3].to_i
|
89
|
-
end
|
90
|
-
time_as_pretty_string.gsub(/ hours?| minutes?| seconds?/, '').to_i.send(:"#{$&.strip}")
|
91
|
-
end
|
92
|
-
|
93
|
-
private
|
94
|
-
|
95
|
-
# Converts time in seconds to an array of hours, minutes and seconds
|
96
|
-
def hours_and_minutes_and_seconds(time_in_sec)
|
97
|
-
minutes, seconds = minutes_and_seconds(time_in_sec)
|
98
|
-
hours = 0
|
99
|
-
if minutes >= 60
|
100
|
-
hours = minutes / 60
|
101
|
-
minutes = minutes % 60
|
102
|
-
end
|
103
|
-
[hours, minutes, seconds]
|
104
|
-
end
|
105
|
-
|
106
|
-
# Converts time in seconds to an array of minutes and seconds
|
107
|
-
def minutes_and_seconds(time_in_sec)
|
108
|
-
[time_in_sec / 60, time_in_sec % 60]
|
109
|
-
end
|
110
|
-
|
111
|
-
# Checks if supplied value is zero
|
112
|
-
def non_zero?(value)
|
113
|
-
value != 0
|
114
|
-
end
|
115
|
-
|
116
|
-
# Adds the correct suffix
|
117
|
-
# Example:
|
118
|
-
#
|
119
|
-
# 1 hour
|
120
|
-
# 3 hours
|
121
|
-
def with_suffix(value, type)
|
122
|
-
if value > 1
|
123
|
-
return "#{value} #{type}"
|
124
|
-
end
|
125
|
-
|
126
|
-
return "#{value} #{type.gsub(/s$/, '')}"
|
127
|
-
end
|
128
|
-
|
129
|
-
alias :has_hours? :non_zero?
|
130
|
-
alias :has_minutes? :non_zero?
|
131
|
-
alias :has_seconds? :non_zero?
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
@@ -1,127 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "PrettyTime" do
|
4
|
-
|
5
|
-
context "On request to #dump" do
|
6
|
-
|
7
|
-
it 'should convert A hours to X seconds' do
|
8
|
-
PrettyTime.dump("4 hours").should == 4.hours.to_i
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'should convert B minutes to X seconds' do
|
12
|
-
PrettyTime.dump("40 minutes").should == 40.minutes.to_i
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should return X seconds as X seconds' do
|
16
|
-
PrettyTime.dump("40 seconds").should == 40
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should convert A hours B minutes to X seconds' do
|
20
|
-
PrettyTime.dump("4 hours 40 minutes").should == 4.hours.to_i + 40.minutes.to_i
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should convert B minutes C seconds to X seconds' do
|
24
|
-
PrettyTime.dump("40 minutes 30 seconds").should == 40.minutes.to_i + 30
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'should convert A hours C seconds to X seconds' do
|
28
|
-
PrettyTime.dump("4 hours 30 seconds").should == 4.hours.to_i + 30
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should convert A hours B minutes C seconds to X seconds' do
|
32
|
-
PrettyTime.dump("4 hours 40 minutes 30 seconds").should == 4.hours.to_i + 40.minutes.to_i + 30
|
33
|
-
end
|
34
|
-
|
35
|
-
context "with fence post cases" do
|
36
|
-
|
37
|
-
it 'should convert 1 hour to 3600 seconds' do
|
38
|
-
PrettyTime.dump("1 hour").should == 3600
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should convert 1 minute to 60 seconds' do
|
42
|
-
PrettyTime.dump("1 minute").should == 60
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should return 1 second as 1 second' do
|
46
|
-
PrettyTime.dump("1 second").should == 1
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should convert 1 hour 1 minute to 3660 seconds' do
|
50
|
-
PrettyTime.dump("1 hour 1 minute").should == 3660
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'should convert 1 minute 1 second to 61 seconds' do
|
54
|
-
PrettyTime.dump("1 minute 1 second").should == 61
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'should convert 1 hour 1 minute 1 second to 3661 seconds' do
|
58
|
-
PrettyTime.dump("1 hour 1 minute 1 second").should == 3661
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "On request to #load" do
|
64
|
-
|
65
|
-
it 'should convert 7200 seconds to 2 hours' do
|
66
|
-
PrettyTime.load(2.hours).should == "2 hours"
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should convert 600 seconds to 10 minutes' do
|
70
|
-
PrettyTime.load(10.minutes.to_i).should == "10 minutes"
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should return 59 seconds as 59 seconds' do
|
74
|
-
PrettyTime.load(59).should == "59 seconds"
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'should convert 7800 seconds to 2 hours 10 minutes' do
|
78
|
-
PrettyTime.load(2.hours.to_i + 10.minutes.to_i).should == "2 hours 10 minutes"
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'should convert 7210 seconds to 2 hours 10 seconds' do
|
82
|
-
PrettyTime.load(2.hours.to_i + 10).should == "2 hours 10 seconds"
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'should convert 130 seconds to 2 minutes 10 seconds' do
|
86
|
-
PrettyTime.load(2.minutes.to_i + 10).should == "2 minutes 10 seconds"
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'should convert 7810 seconds to 2 hours 10 minutes 10 seconds' do
|
90
|
-
PrettyTime.load(2.hours.to_i + 10.minutes.to_i + 10).should == "2 hours 10 minutes 10 seconds"
|
91
|
-
end
|
92
|
-
|
93
|
-
context "with fence post cases" do
|
94
|
-
|
95
|
-
it 'should convert 3600 seconds to 1 hour' do
|
96
|
-
PrettyTime.load(1.hours).should == "1 hour"
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'should convert 60 seconds to 1 minute' do
|
100
|
-
PrettyTime.load(1.minute.to_i).should == "1 minute"
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'should return 1 second as 1 second' do
|
104
|
-
PrettyTime.load(1).should == "1 second"
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'should convert 3660 seconds to 1 hour 1 minute' do
|
108
|
-
PrettyTime.load(1.hours.to_i + 1.minute.to_i).should == "1 hour 1 minute"
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should convert 3601 seconds to 1 hour 1 second' do
|
112
|
-
PrettyTime.load(1.hours.to_i + 1).should == "1 hour 1 second"
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should convert 61 seconds to 1 minute 1 second' do
|
116
|
-
PrettyTime.load(1.minutes.to_i + 1).should == "1 minute 1 second"
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'should convert 3661 seconds to 1 hour 1 minute 1 second' do
|
120
|
-
PrettyTime.load(1.hours.to_i + 1.minutes.to_i + 1).should == "1 hour 1 minute 1 second"
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
end
|